11package dotty .tools .scripting
22
3- import java .nio .file .{ Files , Path }
3+ import java .nio .file .{ Files , Paths , Path }
44import java .io .File
55import java .net .{ URL , URLClassLoader }
66import java .lang .reflect .{ Modifier , Method }
@@ -10,14 +10,14 @@ import scala.jdk.CollectionConverters._
1010import dotty .tools .dotc .{ Driver , Compiler }
1111import dotty .tools .dotc .core .Contexts , Contexts .{ Context , ContextBase , ctx }
1212import dotty .tools .dotc .config .CompilerCommand
13- import dotty .tools .io .{ PlainDirectory , Directory }
13+ import dotty .tools .io .{ PlainDirectory , Directory , ClassPath }
1414import dotty .tools .dotc .reporting .Reporter
1515import dotty .tools .dotc .config .Settings .Setting ._
1616
1717import sys .process ._
1818
1919class 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 =
2121 val outDir = Files .createTempDirectory(" scala3-scripting" )
2222 setup(compilerArgs :+ scriptFile.getAbsolutePath, initCtx.fresh) match
2323 case Some ((toCompile, rootCtx)) =>
@@ -28,11 +28,13 @@ class ScriptingDriver(compilerArgs: Array[String], scriptFile: File, scriptArgs:
2828 throw ScriptingException (" Errors encountered during compilation" )
2929
3030 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 ] = ClassPath .expandPath(classpath, expandStar= true ).map { Paths .get(_) }
33+ val (mainClass, mainMethod) = detectMainClassAndMethod(outDir, classpathEntries, scriptFile)
3234 val invokeMain : Boolean =
3335 Option (pack) match
3436 case Some (func) =>
35- func(outDir, ctx.settings.classpath.value , mainClass)
37+ func(outDir, classpathEntries , mainClass)
3638 case None =>
3739 true
3840 end match
@@ -52,11 +54,12 @@ class ScriptingDriver(compilerArgs: Array[String], scriptFile: File, scriptArgs:
5254 target.delete()
5355 end deleteFile
5456
55- private def detectMainClassAndMethod (outDir : Path , classpath : String ,
57+ private def detectMainClassAndMethod (outDir : Path , classpathEntries : Seq [ Path ] ,
5658 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)
59+
60+ val classpathUrls = (classpathEntries :+ outDir).map { _.toUri.toURL }
61+
62+ val cl = URLClassLoader (classpathUrls.toArray)
6063
6164 def collectMainMethods (target : File , path : String ): List [(String , Method )] =
6265 val nameWithoutExtension = target.getName.takeWhile(_ != '.' )
0 commit comments