@@ -15,7 +15,7 @@ import dotty.tools.io.Jar
1515import dotty .tools .runner .ScalaClassLoader
1616import java .nio .file .Paths
1717import dotty .tools .dotc .config .CommandLineParser
18- import dotty .tools .scripting .StringDriver
18+ import dotty .tools .scripting .{ StringDriver , StringDriverException , ScriptingException }
1919
2020enum ExecuteMode :
2121 case Guess
@@ -118,40 +118,40 @@ object MainGenericRunner {
118118 @ sharable val scalaOption = raw """ @.* """ .r
119119 @ sharable val colorOption = raw """ -color:.* """ .r
120120 @ tailrec
121- def process (args : List [String ], settings : Settings ): Settings = args match
121+ def processArgs (args : List [String ], settings : Settings ): Settings = args match
122122 case Nil =>
123123 settings
124124 case " -run" :: fqName :: tail =>
125- process (tail, settings.withExecuteMode(ExecuteMode .Run ).withTargetToRun(fqName))
125+ processArgs (tail, settings.withExecuteMode(ExecuteMode .Run ).withTargetToRun(fqName))
126126 case (" -cp" | " -classpath" | " --class-path" ) :: cp :: tail =>
127127 val (tailargs, newEntries) = processClasspath(cp, tail)
128- process (tailargs, settings.copy(classPath = settings.classPath ++ newEntries.filter(_.nonEmpty)))
128+ processArgs (tailargs, settings.copy(classPath = settings.classPath ++ newEntries.filter(_.nonEmpty)))
129129 case (" -version" | " --version" ) :: _ =>
130130 settings.copy(
131131 executeMode = ExecuteMode .Repl ,
132132 residualArgs = List (" -version" )
133133 )
134134 case (" -v" | " -verbose" | " --verbose" ) :: tail =>
135- process (
135+ processArgs (
136136 tail,
137137 settings.copy(
138138 verbose = true ,
139139 residualArgs = settings.residualArgs :+ " -verbose"
140140 )
141141 )
142142 case " -save" :: tail =>
143- process (tail, settings.withSave)
143+ processArgs (tail, settings.withSave)
144144 case " -nosave" :: tail =>
145- process (tail, settings.noSave)
145+ processArgs (tail, settings.noSave)
146146 case " -with-compiler" :: tail =>
147- process (tail, settings.withCompiler)
147+ processArgs (tail, settings.withCompiler)
148148 case (o @ javaOption(striped)) :: tail =>
149- process (tail, settings.withJavaArgs(striped).withScalaArgs(o))
149+ processArgs (tail, settings.withJavaArgs(striped).withScalaArgs(o))
150150 case (o @ scalaOption(_* )) :: tail =>
151151 val remainingArgs = (CommandLineParser .expandArg(o) ++ tail).toList
152- process (remainingArgs, settings)
152+ processArgs (remainingArgs, settings)
153153 case (o @ colorOption(_* )) :: tail =>
154- process (tail, settings.withScalaArgs(o))
154+ processArgs (tail, settings.withScalaArgs(o))
155155 case " -e" :: expression :: tail =>
156156 val mainSource = s " @main def main(args: String *): Unit = \n ${expression}"
157157 settings
@@ -169,13 +169,13 @@ object MainGenericRunner {
169169 .withScriptArgs(tail* )
170170 else
171171 val newSettings = if arg.startsWith(" -" ) then settings else settings.withPossibleEntryPaths(arg).withModeShouldBePossibleRun
172- process (tail, newSettings.withResidualArgs(arg))
173- end process
172+ processArgs (tail, newSettings.withResidualArgs(arg))
173+ end processArgs
174174
175- def main (args : Array [String ]): Unit =
175+ def process (args : Array [String ]): Boolean =
176176 val scalaOpts = envOrNone(" SCALA_OPTS" ).toArray.flatMap(_.split(" " )).filter(_.nonEmpty)
177177 val allArgs = scalaOpts ++ args
178- val settings = process (allArgs.toList, Settings ())
178+ val settings = processArgs (allArgs.toList, Settings ())
179179 if settings.exitCode != 0 then System .exit(settings.exitCode)
180180
181181 def removeCompiler (cp : Array [String ]) =
@@ -185,12 +185,13 @@ object MainGenericRunner {
185185 else
186186 cp
187187
188- def run (settings : Settings ): Unit = settings.executeMode match
188+ def run (settings : Settings ): Option [ Throwable ] = settings.executeMode match
189189 case ExecuteMode .Repl =>
190190 val properArgs =
191191 List (" -classpath" , settings.classPath.mkString(classpathSeparator)).filter(Function .const(settings.classPath.nonEmpty))
192192 ++ settings.residualArgs
193193 repl.Main .main(properArgs.toArray)
194+ None
194195
195196 case ExecuteMode .PossibleRun =>
196197 val newClasspath = (settings.classPath :+ " ." ).flatMap(_.split(classpathSeparator).filter(_.nonEmpty)).map(File (_).toURI.toURL)
@@ -207,10 +208,11 @@ object MainGenericRunner {
207208 case None =>
208209 settings.withExecuteMode(ExecuteMode .Repl )
209210 run(newSettings)
211+
210212 case ExecuteMode .Run =>
211213 val scalaClasspath = ClasspathFromClassloader (Thread .currentThread().getContextClassLoader).split(classpathSeparator)
212214 val newClasspath = (settings.classPath.flatMap(_.split(classpathSeparator).filter(_.nonEmpty)) ++ removeCompiler(scalaClasspath) :+ " ." ).map(File (_).toURI.toURL)
213- val res = ObjectRunner .runAndCatch(newClasspath, settings.targetToRun, settings.residualArgs).flatMap {
215+ ObjectRunner .runAndCatch(newClasspath, settings.targetToRun, settings.residualArgs).flatMap {
214216 case ex : ClassNotFoundException if ex.getMessage == settings.targetToRun =>
215217 val file = settings.targetToRun
216218 Jar (file).mainClass match
@@ -220,7 +222,7 @@ object MainGenericRunner {
220222 Some (IllegalArgumentException (s " No main class defined in manifest in jar: $file" ))
221223 case ex => Some (ex)
222224 }
223- errorFn( " " , res)
225+
224226 case ExecuteMode .Script =>
225227 val targetScript = Paths .get(settings.targetScript).toFile
226228 val targetJar = settings.targetScript.replaceAll(" [.][^\\ /]*$" , " " )+ " .jar"
@@ -232,11 +234,10 @@ object MainGenericRunner {
232234 sys.props(" script.path" ) = targetScript.toPath.toAbsolutePath.normalize.toString
233235 val scalaClasspath = ClasspathFromClassloader (Thread .currentThread().getContextClassLoader).split(classpathSeparator)
234236 val newClasspath = (settings.classPath.flatMap(_.split(classpathSeparator).filter(_.nonEmpty)) ++ removeCompiler(scalaClasspath) :+ " ." ).map(File (_).toURI.toURL)
235- val res = if mainClass.nonEmpty then
237+ if mainClass.nonEmpty then
236238 ObjectRunner .runAndCatch(newClasspath :+ File (targetJar).toURI.toURL, mainClass, settings.scriptArgs)
237239 else
238240 Some (IllegalArgumentException (s " No main class defined in manifest in jar: $precompiledJar" ))
239- errorFn(" " , res)
240241
241242 else
242243 val properArgs =
@@ -246,7 +247,8 @@ object MainGenericRunner {
246247 ++ settings.scalaArgs
247248 ++ List (" -script" , settings.targetScript)
248249 ++ settings.scriptArgs
249- scripting.Main .main(properArgs.toArray)
250+ scripting.Main .process(properArgs.toArray)
251+
250252 case ExecuteMode .Expression =>
251253 val cp = settings.classPath match {
252254 case Nil => " "
@@ -265,12 +267,17 @@ object MainGenericRunner {
265267 else
266268 run(settings.withExecuteMode(ExecuteMode .Repl ))
267269
268- run(settings)
270+ run(settings) match
271+ case Some (ex : (StringDriverException | ScriptingException )) => errorFn(ex.getMessage)
272+ case e @ Some (ex) => errorFn(" " , e)
273+ case _ => true
269274
270-
271- def errorFn (str : String , e : Option [Throwable ] = None , isFailure : Boolean = true ): Boolean = {
275+ def errorFn (str : String , e : Option [Throwable ] = None ): Boolean =
272276 if (str.nonEmpty) Console .err.println(str)
273277 e.foreach(_.printStackTrace())
274- ! isFailure
275- }
278+ false
279+
280+ def main (args : Array [String ]): Unit =
281+ if (! process(args)) System .exit(1 )
282+
276283}
0 commit comments