@@ -15,6 +15,7 @@ import dotty.tools.dotc.config.Properties.envOrNone
1515import java .util .jar ._
1616import java .util .jar .Attributes .Name
1717import dotty .tools .io .Jar
18+ import dotty .tools .runner .ScalaClassLoader
1819
1920enum ExecuteMode :
2021 case Guess
@@ -34,19 +35,19 @@ case class Settings(
3435 possibleEntryPaths : List [String ] = List .empty,
3536 scriptArgs : List [String ] = List .empty,
3637 targetScript : String = " " ,
37- targetFqName : String = " " ,
38+ targetToRun : String = " " ,
3839 save : Boolean = false ,
3940 modeShouldBePossibleRun : Boolean = false ,
4041 modeShouldBeRun : Boolean = false ,
4142 compiler : Boolean = false ,
4243) {
43- def withExecuteMode (em : ExecuteMode ): Settings = // this.executeMode match
44- // case ExecuteMode.Guess =>
45- this .copy(executeMode = em)
46- // case _ =>
47- // println(s"execute_mode==[$executeMode], attempted overwrite by [$em]")
48- // this.copy(exitCode = 1)
49- // end withExecuteMode
44+ def withExecuteMode (em : ExecuteMode ): Settings = this .executeMode match
45+ case ExecuteMode .Guess | ExecuteMode . PossibleRun =>
46+ this .copy(executeMode = em)
47+ case _ =>
48+ println(s " execute_mode==[ $executeMode], attempted overwrite by [ $em] " )
49+ this .copy(exitCode = 1 )
50+ end withExecuteMode
5051
5152 def withScalaArgs (args : String * ): Settings =
5253 this .copy(scalaArgs = scalaArgs.appendedAll(args.toList))
@@ -71,8 +72,8 @@ case class Settings(
7172 this .copy(exitCode = 2 )
7273 end withTargetScript
7374
74- def withTargetFqName ( targetFqName : String ): Settings =
75- this .copy(targetFqName = targetFqName )
75+ def withTargetToRun ( targetToRun : String ): Settings =
76+ this .copy(targetToRun = targetToRun )
7677
7778 def withSave : Settings =
7879 this .copy(save = true )
@@ -99,7 +100,7 @@ object MainGenericRunner {
99100 case Nil =>
100101 settings
101102 case " -run" :: fqName :: tail =>
102- process(tail, settings.withExecuteMode(ExecuteMode .Run ).withTargetFqName (fqName))
103+ process(tail, settings.withExecuteMode(ExecuteMode .Run ).withTargetToRun (fqName))
103104 case (" -cp" | " -classpath" | " --class-path" ) :: cp :: tail =>
104105 process(tail, settings.copy(classPath = settings.classPath.appended(cp)))
105106 case (" -version" | " --version" ) :: _ =>
@@ -150,14 +151,17 @@ object MainGenericRunner {
150151 repl.Main .main(properArgs.toArray)
151152
152153 case ExecuteMode .PossibleRun =>
153- val targetFqName = settings.possibleEntryPaths.find { entryPath =>
154- Try (Thread .currentThread().getContextClassLoader.loadClass(entryPath)) match
155- case Failure (_) => false
156- case Success (_) => true
154+ val newClasspath = (settings.classPath :+ " ." ).map(File (_).toURI.toURL)
155+ import dotty .tools .runner .RichClassLoader ._
156+ val newClassLoader = ScalaClassLoader .fromURLsParallelCapable(newClasspath)
157+ val targetToRun = settings.possibleEntryPaths.to(LazyList ).find { entryPath =>
158+ newClassLoader.tryToLoadClass(entryPath).orElse {
159+ Option .when(Jar .isJarOrZip(dotty.tools.io.Path (entryPath)))(Jar (entryPath).mainClass).flatten
160+ }.isDefined
157161 }
158- targetFqName match
162+ targetToRun match
159163 case Some (fqName) =>
160- run(settings.withTargetFqName (fqName).withResidualArgs(settings.residualArgs.filter { _ != fqName }* ).withExecuteMode(ExecuteMode .Run ))
164+ run(settings.withTargetToRun (fqName).withResidualArgs(settings.residualArgs.filter { _ != fqName }* ).withExecuteMode(ExecuteMode .Run ))
161165 case None =>
162166 run(settings.withExecuteMode(ExecuteMode .Repl ))
163167 case ExecuteMode .Run =>
@@ -171,9 +175,9 @@ object MainGenericRunner {
171175 cp
172176 val newClasspath = (settings.classPath ++ removeCompiler(scalaClasspath) :+ " ." ).map(File (_).toURI.toURL)
173177
174- val res = ObjectRunner .runAndCatch(newClasspath, settings.targetFqName , settings.residualArgs).flatMap {
175- case ex : ClassNotFoundException if ex.getMessage == settings.residualArgs.head =>
176- val file = settings.residualArgs.head
178+ val res = ObjectRunner .runAndCatch(newClasspath, settings.targetToRun , settings.residualArgs).flatMap {
179+ case ex : ClassNotFoundException if ex.getMessage == settings.targetToRun =>
180+ val file = settings.targetToRun
177181 Jar (file).mainClass match
178182 case Some (mc) =>
179183 ObjectRunner .runAndCatch(newClasspath :+ File (file).toURI.toURL, mc, settings.residualArgs)
0 commit comments