Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Groovy REPL: script file detection/execution fails, fixes #1139 #1140

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
mattirn marked this conversation as resolved.
Show resolved Hide resolved
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