1
1
package dotty .tools .scripting
2
2
3
- import java .nio .file .{ Files , Path }
3
+ import java .nio .file .{ Files , Paths , Path }
4
4
import java .io .File
5
5
import java .net .{ URL , URLClassLoader }
6
6
import java .lang .reflect .{ Modifier , Method }
@@ -17,7 +17,7 @@ import dotty.tools.dotc.config.Settings.Setting._
17
17
import sys .process ._
18
18
19
19
class ScriptingDriver (compilerArgs : Array [String ], scriptFile : File , scriptArgs : Array [String ]) extends Driver :
20
- def compileAndRun (pack: (Path , String , String ) => Boolean = null ): Unit =
20
+ def compileAndRun (pack: (Path , Seq [ Path ] , String ) => Boolean = null ): Unit =
21
21
val outDir = Files .createTempDirectory(" scala3-scripting" )
22
22
setup(compilerArgs :+ scriptFile.getAbsolutePath, initCtx.fresh) match
23
23
case Some ((toCompile, rootCtx)) =>
@@ -28,11 +28,23 @@ class ScriptingDriver(compilerArgs: Array[String], scriptFile: File, scriptArgs:
28
28
throw ScriptingException (" Errors encountered during compilation" )
29
29
30
30
try
31
- val (mainClass, mainMethod) = detectMainClassAndMethod(outDir, ctx.settings.classpath.value, scriptFile)
31
+ val classpath = s " ${ctx.settings.classpath.value}${pathsep}${sys.props(" java.class.path" )}"
32
+ val classpathEntries : Seq [Path ] =
33
+ classpath.split(pathsep).toIndexedSeq.flatMap { entry =>
34
+ val f = Paths .get(entry).toAbsolutePath.normalize.toFile
35
+ // expand wildcard classpath entries
36
+ if (f.getName == " *" && f.getParentFile.isDirectory){
37
+ f.getParentFile.listFiles.filter { _.getName.toLowerCase.endsWith(" .jar" ) }.map { _.toPath }.toSeq
38
+ } else {
39
+ Seq (f.toPath)
40
+ }
41
+ }.toIndexedSeq
42
+
43
+ val (mainClass, mainMethod) = detectMainClassAndMethod(outDir, classpathEntries, scriptFile)
32
44
val invokeMain : Boolean =
33
45
Option (pack) match
34
46
case Some (func) =>
35
- func(outDir, ctx.settings.classpath.value , mainClass)
47
+ func(outDir, classpathEntries , mainClass)
36
48
case None =>
37
49
true
38
50
end match
@@ -52,11 +64,12 @@ class ScriptingDriver(compilerArgs: Array[String], scriptFile: File, scriptArgs:
52
64
target.delete()
53
65
end deleteFile
54
66
55
- private def detectMainClassAndMethod (outDir : Path , classpath : String ,
67
+ private def detectMainClassAndMethod (outDir : Path , classpathEntries : Seq [ Path ] ,
56
68
scriptFile : File ): (String , Method ) =
57
- val outDirURL = outDir.toUri.toURL
58
- val classpathUrls = classpath.split(pathsep).map(File (_).toURI.toURL)
59
- val cl = URLClassLoader (classpathUrls :+ outDirURL)
69
+
70
+ val classpathUrls = (classpathEntries :+ outDir).map { _.toUri.toURL }
71
+
72
+ val cl = URLClassLoader (classpathUrls.toArray)
60
73
61
74
def collectMainMethods (target : File , path : String ): List [(String , Method )] =
62
75
val nameWithoutExtension = target.getName.takeWhile(_ != '.' )
0 commit comments