diff --git a/console/src/main/java/org/jline/console/ScriptEngine.java b/console/src/main/java/org/jline/console/ScriptEngine.java index 2490d5d5..d23bc667 100644 --- a/console/src/main/java/org/jline/console/ScriptEngine.java +++ b/console/src/main/java/org/jline/console/ScriptEngine.java @@ -154,6 +154,16 @@ default Object deserialize(String value) { */ Object execute(String statement) throws Exception; + /** + * Executes scriptEngine script + * @param script the script + * @return result + * @throws Exception in case of error + */ + default Object execute(Path script) throws Exception { + return execute(script.toFile(), null); + } + /** * Executes scriptEngine script * @param script the script @@ -164,6 +174,17 @@ default Object execute(File script) throws Exception { return execute(script, null); } + /** + * Executes scriptEngine script + * @param script the script + * @param args arguments + * @return result + * @throws Exception in case of error + */ + default Object execute(Path script, Object[] args) throws Exception { + return execute(script.toFile(), args); + } + /** * Executes scriptEngine script * @param script the script diff --git a/console/src/main/java/org/jline/console/impl/ConsoleEngineImpl.java b/console/src/main/java/org/jline/console/impl/ConsoleEngineImpl.java index d8e1a094..76041059 100644 --- a/console/src/main/java/org/jline/console/impl/ConsoleEngineImpl.java +++ b/console/src/main/java/org/jline/console/impl/ConsoleEngineImpl.java @@ -381,35 +381,47 @@ private class ScriptFile { @SuppressWarnings("unchecked") public ScriptFile(String command, String cmdLine, String[] args) { - if (!parser().validCommandName(command)) { - return; - } + this.cmdLine = cmdLine; try { - this.script = Paths.get(command); - this.cmdLine = cmdLine; - if (Files.exists(script)) { - scriptExtension(command); - } else if (engine.hasVariable(VAR_PATH)) { - boolean found = false; - for (String p : (List) engine.get(VAR_PATH)) { - for (String e : scriptExtensions()) { - String file = command + "." + e; - Path path = Paths.get(p, file); - if (path.toFile().exists()) { - script = path; - scriptExtension(command); - found = true; + if (!parser().validCommandName(command)) { + // DefaultParser not necessarily parse script file from command line. + // As an example for Groovy REPL demo '/tmp/script.jline' is not a valid command i.e. + // prompt> /tmp/script.jline arg1 arg2 + command = cmdLine.split("\\s+")[0]; + this.extension = fileExtension(command); + if (isScript()) { + this.extension = ""; + this.script = Paths.get(command); + if (Files.exists(script)) { + scriptExtension(command); + } + } + } else { + this.script = Paths.get(command); + if (Files.exists(script)) { + scriptExtension(command); + } else if (engine.hasVariable(VAR_PATH)) { + boolean found = false; + for (String p : (List) engine.get(VAR_PATH)) { + for (String e : scriptExtensions()) { + String file = command + "." + e; + Path path = Paths.get(p, file); + if (Files.exists(path)) { + script = path; + this.extension = e; + found = true; + break; + } + } + if (found) { break; } } - if (found) { - break; - } } } doArgs(args); } catch (Exception e) { - // ignore + Log.trace("Not a script file: " + command); } } @@ -423,9 +435,12 @@ public ScriptFile(Path script, String cmdLine, String[] args) { doArgs(args); } + private String fileExtension(String fileName) { + return fileName.contains(".") ? fileName.substring(fileName.lastIndexOf(".") + 1) : ""; + } + private void scriptExtension(String command) { - String name = script.getFileName().toString(); - this.extension = name.contains(".") ? name.substring(name.lastIndexOf(".") + 1) : ""; + this.extension = fileExtension(script.getFileName().toString()); if (!isEngineScript() && !isConsoleScript()) { throw new IllegalArgumentException("Command not found: " + command); } @@ -665,16 +680,8 @@ public Object execute(String cmd, String line, String[] args) throws Exception { return null; } Object out = null; - ScriptFile file = null; - if (parser().validCommandName(cmd)) { - file = new ScriptFile(cmd, line, args); - } else { - Path f = Paths.get(line.split("\\s+")[0]); - if (Files.exists(f)) { - file = new ScriptFile(f, line, args); - } - } - if (file != null && file.execute()) { + ScriptFile file = new ScriptFile(cmd, line, args); + if (file.execute()) { out = file.getResult(); } else { line = line.trim();