Skip to content

Commit bf8a57b

Browse files
committed
Groovy REPL: script file detection/execution fails, fixes jline#1139
1 parent 8087827 commit bf8a57b

File tree

2 files changed

+61
-33
lines changed

2 files changed

+61
-33
lines changed

Diff for: console/src/main/java/org/jline/console/ScriptEngine.java

+21
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,16 @@ default Object deserialize(String value) {
154154
*/
155155
Object execute(String statement) throws Exception;
156156

157+
/**
158+
* Executes scriptEngine script
159+
* @param script the script
160+
* @return result
161+
* @throws Exception in case of error
162+
*/
163+
default Object execute(Path script) throws Exception {
164+
return execute(script.toFile(), null);
165+
}
166+
157167
/**
158168
* Executes scriptEngine script
159169
* @param script the script
@@ -164,6 +174,17 @@ default Object execute(File script) throws Exception {
164174
return execute(script, null);
165175
}
166176

177+
/**
178+
* Executes scriptEngine script
179+
* @param script the script
180+
* @param args arguments
181+
* @return result
182+
* @throws Exception in case of error
183+
*/
184+
default Object execute(Path script, Object[] args) throws Exception {
185+
return execute(script.toFile(), args);
186+
}
187+
167188
/**
168189
* Executes scriptEngine script
169190
* @param script the script

Diff for: console/src/main/java/org/jline/console/impl/ConsoleEngineImpl.java

+40-33
Original file line numberDiff line numberDiff line change
@@ -381,35 +381,47 @@ private class ScriptFile {
381381

382382
@SuppressWarnings("unchecked")
383383
public ScriptFile(String command, String cmdLine, String[] args) {
384-
if (!parser().validCommandName(command)) {
385-
return;
386-
}
384+
this.cmdLine = cmdLine;
387385
try {
388-
this.script = Paths.get(command);
389-
this.cmdLine = cmdLine;
390-
if (Files.exists(script)) {
391-
scriptExtension(command);
392-
} else if (engine.hasVariable(VAR_PATH)) {
393-
boolean found = false;
394-
for (String p : (List<String>) engine.get(VAR_PATH)) {
395-
for (String e : scriptExtensions()) {
396-
String file = command + "." + e;
397-
Path path = Paths.get(p, file);
398-
if (path.toFile().exists()) {
399-
script = path;
400-
scriptExtension(command);
401-
found = true;
386+
if (!parser().validCommandName(command)) {
387+
// DefaultParser not necessarily parse script file from command line.
388+
// As an example for Groovy REPL demo '/tmp/script.jline' is not a valid command i.e.
389+
// prompt> /tmp/script.jline arg1 arg2
390+
command = cmdLine.split("\\s+")[0];
391+
this.extension = fileExtension(command);
392+
if (isScript()) {
393+
this.extension = "";
394+
this.script = Paths.get(command);
395+
if (Files.exists(script)) {
396+
scriptExtension(command);
397+
}
398+
}
399+
} else {
400+
this.script = Paths.get(command);
401+
if (Files.exists(script)) {
402+
scriptExtension(command);
403+
} else if (engine.hasVariable(VAR_PATH)) {
404+
boolean found = false;
405+
for (String p : (List<String>) engine.get(VAR_PATH)) {
406+
for (String e : scriptExtensions()) {
407+
String file = command + "." + e;
408+
Path path = Paths.get(p, file);
409+
if (Files.exists(path)) {
410+
script = path;
411+
this.extension = e;
412+
found = true;
413+
break;
414+
}
415+
}
416+
if (found) {
402417
break;
403418
}
404419
}
405-
if (found) {
406-
break;
407-
}
408420
}
409421
}
410422
doArgs(args);
411423
} catch (Exception e) {
412-
// ignore
424+
Log.trace("Not a script file: " + command);
413425
}
414426
}
415427

@@ -423,9 +435,12 @@ public ScriptFile(Path script, String cmdLine, String[] args) {
423435
doArgs(args);
424436
}
425437

438+
private String fileExtension(String fileName) {
439+
return fileName.contains(".") ? fileName.substring(fileName.lastIndexOf(".") + 1) : "";
440+
}
441+
426442
private void scriptExtension(String command) {
427-
String name = script.getFileName().toString();
428-
this.extension = name.contains(".") ? name.substring(name.lastIndexOf(".") + 1) : "";
443+
this.extension = fileExtension(script.getFileName().toString());
429444
if (!isEngineScript() && !isConsoleScript()) {
430445
throw new IllegalArgumentException("Command not found: " + command);
431446
}
@@ -665,16 +680,8 @@ public Object execute(String cmd, String line, String[] args) throws Exception {
665680
return null;
666681
}
667682
Object out = null;
668-
ScriptFile file = null;
669-
if (parser().validCommandName(cmd)) {
670-
file = new ScriptFile(cmd, line, args);
671-
} else {
672-
Path f = Paths.get(line.split("\\s+")[0]);
673-
if (Files.exists(f)) {
674-
file = new ScriptFile(f, line, args);
675-
}
676-
}
677-
if (file != null && file.execute()) {
683+
ScriptFile file = new ScriptFile(cmd, line, args);
684+
if (file.execute()) {
678685
out = file.getResult();
679686
} else {
680687
line = line.trim();

0 commit comments

Comments
 (0)