@@ -8,6 +8,7 @@ import java.net.URLClassLoader
8
8
import sys .process ._
9
9
import java .io .File
10
10
import java .lang .Thread
11
+ import scala .annotation .internal .sharable
11
12
12
13
enum ExecuteMode :
13
14
case Guess
@@ -20,10 +21,12 @@ case class Settings(
20
21
classPath : List [String ] = List .empty,
21
22
executeMode : ExecuteMode = ExecuteMode .Guess ,
22
23
exitCode : Int = 0 ,
24
+ javaArgs : List [String ] = List .empty,
25
+ scalaArgs : List [String ] = List .empty,
23
26
residualArgs : List [String ] = List .empty,
24
27
scriptArgs : List [String ] = List .empty,
25
28
targetScript : String = " " ,
26
- areWithCompiler : Boolean = false ,
29
+ save : Boolean = false ,
27
30
) {
28
31
def withExecuteMode (em : ExecuteMode ): Settings = this .executeMode match
29
32
case ExecuteMode .Guess =>
@@ -33,6 +36,12 @@ case class Settings(
33
36
this .copy(exitCode = 1 )
34
37
end withExecuteMode
35
38
39
+ def withScalaArgs (args : String * ): Settings =
40
+ this .copy(scalaArgs = scalaArgs.appendedAll(args.toList))
41
+
42
+ def withJavaArgs (args : String * ): Settings =
43
+ this .copy(javaArgs = javaArgs.appendedAll(args.toList))
44
+
36
45
def withResidualArgs (args : String * ): Settings =
37
46
this .copy(residualArgs = residualArgs.appendedAll(args.toList))
38
47
@@ -47,23 +56,23 @@ case class Settings(
47
56
this .copy(exitCode = 2 )
48
57
end withTargetScript
49
58
50
- def withCompiler : Settings =
51
- this .copy(areWithCompiler = true )
59
+ def withSave : Settings =
60
+ this .copy(save = true )
52
61
}
53
62
54
63
object MainGenericRunner {
55
64
56
- final val classpathSeparator = " :"
65
+ val classpathSeparator = File .pathSeparator
66
+
67
+ @ sharable val javaOption = raw """ -J(.*) """ .r
57
68
58
69
@ tailrec
59
70
def process (args : List [String ], settings : Settings ): Settings = args match
60
71
case Nil =>
61
72
settings
62
- case " -repl" :: tail =>
63
- process(tail, settings.withExecuteMode(ExecuteMode .Repl ))
64
73
case " -run" :: tail =>
65
74
process(tail, settings.withExecuteMode(ExecuteMode .Run ))
66
- case (" -cp" | " -classpath" | " --classpath " ) :: cp :: tail =>
75
+ case (" -cp" | " -classpath" | " --class-path " ) :: cp :: tail =>
67
76
process(tail, settings.copy(classPath = settings.classPath.appended(cp)))
68
77
case (" -version" | " --version" ) :: _ =>
69
78
settings.copy(
@@ -78,8 +87,10 @@ object MainGenericRunner {
78
87
residualArgs = settings.residualArgs :+ " -verbose"
79
88
)
80
89
)
81
- case " -with-compiler" :: tail =>
82
- process(tail, settings.withCompiler)
90
+ case " -save" :: tail =>
91
+ process(tail, settings.withSave)
92
+ case (o @ javaOption(striped)) :: tail =>
93
+ process(tail, settings.withJavaArgs(striped).withScalaArgs(o))
83
94
case arg :: tail =>
84
95
val line = Try (Source .fromFile(arg).getLines.toList).toOption.flatMap(_.headOption)
85
96
if arg.endsWith(" .scala" ) || arg.endsWith(" .sc" ) || (line.nonEmpty && raw " #!.*scala " .r.matches(line.get)) then
@@ -93,7 +104,8 @@ object MainGenericRunner {
93
104
def main (args : Array [String ]): Unit =
94
105
val settings = process(args.toList, Settings ())
95
106
if settings.exitCode != 0 then System .exit(settings.exitCode)
96
- settings.executeMode match
107
+
108
+ def run (mode : ExecuteMode ): Unit = mode match
97
109
case ExecuteMode .Repl =>
98
110
val properArgs =
99
111
List (" -classpath" , settings.classPath.mkString(classpathSeparator)).filter(Function .const(settings.classPath.nonEmpty))
@@ -104,20 +116,23 @@ object MainGenericRunner {
104
116
val newClasspath = settings.classPath ++ getClasspath :+ " ."
105
117
List (" -classpath" , newClasspath.mkString(classpathSeparator)).filter(Function .const(newClasspath.nonEmpty))
106
118
++ settings.residualArgs
107
- s " java ${properArgs.mkString(" " )}" .! // For now we collect classpath that coursier provides for convenience
119
+ s " java ${settings.javaArgs.mkString( " " )} ${ properArgs.mkString(" " )}" .! // For now we collect classpath that coursier provides for convenience
108
120
case ExecuteMode .Script =>
109
121
val properArgs =
110
122
List (" -classpath" , settings.classPath.mkString(classpathSeparator)).filter(Function .const(settings.classPath.nonEmpty))
111
123
++ settings.residualArgs
124
+ ++ (if settings.save then List (" -save" ) else Nil )
112
125
++ List (" -script" , settings.targetScript)
126
+ ++ settings.scalaArgs
113
127
++ settings.scriptArgs
114
128
scripting.Main .main(properArgs.toArray)
115
129
case ExecuteMode .Guess =>
116
- val properArgs =
117
- List ( " -classpath " , settings.classPath.mkString(classpathSeparator)).filter( Function .const(settings.classPath.nonEmpty) )
118
- ++ settings.residualArgs
119
- repl. Main .main(properArgs.toArray )
130
+ if args.toList.forall(_.startsWith( " - " )) then // all are options
131
+ run( ExecuteMode . Repl )
132
+ else
133
+ run( ExecuteMode . Run )
120
134
135
+ run(settings.executeMode)
121
136
122
137
private def getClasspath (cl : ClassLoader ): Array [String ] = cl match
123
138
case null => Array ()
0 commit comments