Skip to content

Commit

Permalink
Groovy REPL: script file detection/execution fails, fixes #1139
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Feb 1, 2025
1 parent 8087827 commit bf8a57b
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 33 deletions.
21 changes: 21 additions & 0 deletions console/src/main/java/org/jline/console/ScriptEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
73 changes: 40 additions & 33 deletions console/src/main/java/org/jline/console/impl/ConsoleEngineImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>) 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<String>) 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);
}
}

Expand All @@ -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);
}
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit bf8a57b

Please sign in to comment.