@@ -381,35 +381,47 @@ private class ScriptFile {
381
381
382
382
@ SuppressWarnings ("unchecked" )
383
383
public ScriptFile (String command , String cmdLine , String [] args ) {
384
- if (!parser ().validCommandName (command )) {
385
- return ;
386
- }
384
+ this .cmdLine = cmdLine ;
387
385
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 ) {
402
417
break ;
403
418
}
404
419
}
405
- if (found ) {
406
- break ;
407
- }
408
420
}
409
421
}
410
422
doArgs (args );
411
423
} catch (Exception e ) {
412
- // ignore
424
+ Log . trace ( "Not a script file: " + command );
413
425
}
414
426
}
415
427
@@ -423,9 +435,12 @@ public ScriptFile(Path script, String cmdLine, String[] args) {
423
435
doArgs (args );
424
436
}
425
437
438
+ private String fileExtension (String fileName ) {
439
+ return fileName .contains ("." ) ? fileName .substring (fileName .lastIndexOf ("." ) + 1 ) : "" ;
440
+ }
441
+
426
442
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 ());
429
444
if (!isEngineScript () && !isConsoleScript ()) {
430
445
throw new IllegalArgumentException ("Command not found: " + command );
431
446
}
@@ -665,16 +680,8 @@ public Object execute(String cmd, String line, String[] args) throws Exception {
665
680
return null ;
666
681
}
667
682
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 ()) {
678
685
out = file .getResult ();
679
686
} else {
680
687
line = line .trim ();
0 commit comments