OSDN Git Service

[#32549] executing a script directly inputted t32549
authorargius <argius.net@gmail.com>
Fri, 13 Dec 2013 13:16:26 +0000 (22:16 +0900)
committerargius <argius.net@gmail.com>
Fri, 13 Dec 2013 13:16:26 +0000 (22:16 +0900)
src/net/argius/stew/CommandProcessor.java
src/net/argius/stew/messages.u8p
src/net/argius/stew/messages_ja.u8p

index 9547df1..9bc5def 100644 (file)
@@ -127,13 +127,29 @@ final class CommandProcessor {
         }
         // script
         if (commandName.equals("-s")) {
-            final File file = Path.resolve(env.getCurrentDirectory(), p.at(1));
-            if (!file.isFile()) {
+            if (!p.has(1)) {
                 throw new UsageException(res.get("usage.-s"));
             }
-            log.debug("-s %s", file.getAbsolutePath());
-            ScriptEngineManager factory = new ScriptEngineManager();
-            ScriptEngine engine = factory.getEngineByName("JavaScript");
+            final String p1 = p.at(1);
+            final File file;
+            if (p1.contains(".")) { // by extension
+                file = Path.resolve(env.getCurrentDirectory(), p1);
+                if (!file.exists() || !file.isFile()) {
+                    outputMessage("e.file-not-exists", p1);
+                    return true;
+                }
+                log.debug("script file: %s", file.getAbsolutePath());
+            } else { // by name
+                file = null;
+                log.debug("script name: %s", p1);
+            }
+            ScriptEngine engine = (file == null)
+                ? new ScriptEngineManager().getEngineByName(p1)
+                : new ScriptEngineManager().getEngineByExtension(Path.getExtension(file));
+            if (engine == null) {
+                outputMessage("e.unsupported", p1);
+                return true;
+            }
             engine.put("connection", env.getCurrentConnection());
             engine.put("conn", env.getCurrentConnection());
             engine.put("patameter", p);
@@ -141,12 +157,17 @@ final class CommandProcessor {
             engine.put("outputProcessor", op);
             engine.put("op", op);
             try {
-                Reader r = new FileReader(file);
-                try {
-                    engine.eval("function using(o, f) { f(o); o.close() }");
-                    engine.eval(r);
-                } finally {
-                    r.close();
+                if (file == null) {
+                    engine.put(ScriptEngine.FILENAME, null);
+                    engine.eval(p.after(2));
+                } else {
+                    engine.put(ScriptEngine.FILENAME, file.getAbsolutePath());
+                    Reader r = new FileReader(file);
+                    try {
+                        engine.eval(r);
+                    } finally {
+                        r.close();
+                    }
                 }
             } catch (Exception ex) {
                 throw new CommandException(ex);
index 504ab69..feb2c63 100644 (file)
@@ -27,6 +27,7 @@ e.command=Error: {0}
 e.database=Database Error: {0}
 e.dir-not-exists=Directory[{0}] does not exist.
 e.fatal=Fatal Error: {0}
+e.file-not-exists=File[{0}] does not exist.
 e.no-connector=Connector [{0}] does not exist.
 e.not-connect=Not connected.
 e.not-found=Error: Command [{0}] was not found.
index cb4d180..ed2178f 100644 (file)
@@ -26,6 +26,7 @@ e.command=エラー: {0}
 e.database=データベースエラー: {0}
 e.dir-not-exists=ディレクトリ[{0}]は存在しません。
 e.fatal=致命的なエラー: {0}
+e.file-not-exists=ファイル[{0}]は存在しません。
 e.no-connector=コネクタ [{0}] はありません。
 e.not-connect=接続されていません。
 e.not-found=エラー: コマンド [{0}] は見つかりませんでした。