@@ -15,6 +15,7 @@ import dotty.tools.dotc.config.Properties.envOrNone
15
15
import java .util .jar ._
16
16
import java .util .jar .Attributes .Name
17
17
import dotty .tools .io .Jar
18
+ import dotty .tools .runner .ScalaClassLoader
18
19
19
20
enum ExecuteMode :
20
21
case Guess
@@ -34,19 +35,19 @@ case class Settings(
34
35
possibleEntryPaths : List [String ] = List .empty,
35
36
scriptArgs : List [String ] = List .empty,
36
37
targetScript : String = " " ,
37
- targetFqName : String = " " ,
38
+ targetToRun : String = " " ,
38
39
save : Boolean = false ,
39
40
modeShouldBePossibleRun : Boolean = false ,
40
41
modeShouldBeRun : Boolean = false ,
41
42
compiler : Boolean = false ,
42
43
) {
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
50
51
51
52
def withScalaArgs (args : String * ): Settings =
52
53
this .copy(scalaArgs = scalaArgs.appendedAll(args.toList))
@@ -71,8 +72,8 @@ case class Settings(
71
72
this .copy(exitCode = 2 )
72
73
end withTargetScript
73
74
74
- def withTargetFqName ( targetFqName : String ): Settings =
75
- this .copy(targetFqName = targetFqName )
75
+ def withTargetToRun ( targetToRun : String ): Settings =
76
+ this .copy(targetToRun = targetToRun )
76
77
77
78
def withSave : Settings =
78
79
this .copy(save = true )
@@ -99,7 +100,7 @@ object MainGenericRunner {
99
100
case Nil =>
100
101
settings
101
102
case " -run" :: fqName :: tail =>
102
- process(tail, settings.withExecuteMode(ExecuteMode .Run ).withTargetFqName (fqName))
103
+ process(tail, settings.withExecuteMode(ExecuteMode .Run ).withTargetToRun (fqName))
103
104
case (" -cp" | " -classpath" | " --class-path" ) :: cp :: tail =>
104
105
process(tail, settings.copy(classPath = settings.classPath.appended(cp)))
105
106
case (" -version" | " --version" ) :: _ =>
@@ -150,14 +151,17 @@ object MainGenericRunner {
150
151
repl.Main .main(properArgs.toArray)
151
152
152
153
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
157
161
}
158
- targetFqName match
162
+ targetToRun match
159
163
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 ))
161
165
case None =>
162
166
run(settings.withExecuteMode(ExecuteMode .Repl ))
163
167
case ExecuteMode .Run =>
@@ -171,9 +175,9 @@ object MainGenericRunner {
171
175
cp
172
176
val newClasspath = (settings.classPath ++ removeCompiler(scalaClasspath) :+ " ." ).map(File (_).toURI.toURL)
173
177
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
177
181
Jar (file).mainClass match
178
182
case Some (mc) =>
179
183
ObjectRunner .runAndCatch(newClasspath :+ File (file).toURI.toURL, mc, settings.residualArgs)
0 commit comments