@@ -158,6 +158,13 @@ object MainGenericRunner {
158158 val settings = process(allArgs.toList, Settings ())
159159 if settings.exitCode != 0 then System .exit(settings.exitCode)
160160
161+ def removeCompiler (cp : Array [String ]) =
162+ if (! settings.compiler) then // Let's remove compiler from the classpath
163+ val compilerLibs = Seq (" scala3-compiler" , " scala3-interfaces" , " tasty-core" , " scala-asm" , " scala3-staging" , " scala3-tasty-inspector" )
164+ cp.filterNot(c => compilerLibs.exists(c.contains))
165+ else
166+ cp
167+
161168 def run (settings : Settings ): Unit = settings.executeMode match
162169 case ExecuteMode .Repl =>
163170 val properArgs =
@@ -181,13 +188,6 @@ object MainGenericRunner {
181188 run(settings.withExecuteMode(ExecuteMode .Repl ))
182189 case ExecuteMode .Run =>
183190 val scalaClasspath = ClasspathFromClassloader (Thread .currentThread().getContextClassLoader).split(classpathSeparator)
184-
185- def removeCompiler (cp : Array [String ]) =
186- if (! settings.compiler) then // Let's remove compiler from the classpath
187- val compilerLibs = Seq (" scala3-compiler" , " scala3-interfaces" , " tasty-core" , " scala-asm" , " scala3-staging" , " scala3-tasty-inspector" )
188- cp.filterNot(c => compilerLibs.exists(c.contains))
189- else
190- cp
191191 val newClasspath = (settings.classPath.flatMap(_.split(classpathSeparator).filter(_.nonEmpty)) ++ removeCompiler(scalaClasspath) :+ " ." ).map(File (_).toURI.toURL)
192192 val res = ObjectRunner .runAndCatch(newClasspath, settings.targetToRun, settings.residualArgs).flatMap {
193193 case ex : ClassNotFoundException if ex.getMessage == settings.targetToRun =>
@@ -201,14 +201,33 @@ object MainGenericRunner {
201201 }
202202 errorFn(" " , res)
203203 case ExecuteMode .Script =>
204- val properArgs =
205- List (" -classpath" , settings.classPath.mkString(classpathSeparator)).filter(Function .const(settings.classPath.nonEmpty))
206- ++ settings.residualArgs
207- ++ (if settings.save then List (" -save" ) else Nil )
208- ++ settings.scalaArgs
209- ++ List (" -script" , settings.targetScript)
210- ++ settings.scriptArgs
211- scripting.Main .main(properArgs.toArray)
204+ val targetScript = Paths .get(settings.targetScript)
205+ val targetJar = settings.targetScript.replaceAll(" [.][^\\ /]*$" ," " )+ " .jar"
206+ val precompiledJar = Paths .get(targetJar)
207+ def mainClass = Jar (targetJar).mainClass match
208+ case Some (mc) =>
209+ mc
210+ case None =>
211+ " "
212+ if precompiledJar.toFile.isFile && mainClass.nonEmpty && precompiledJar.toFile.lastModified >= targetScript.toFile.lastModified then
213+ // precompiledJar is newer than targetScript
214+ sys.props(" script.path" ) = targetScript.toAbsolutePath.normalize.toString
215+ val scalaClasspath = ClasspathFromClassloader (Thread .currentThread().getContextClassLoader).split(classpathSeparator)
216+ val newClasspath = (settings.classPath.flatMap(_.split(classpathSeparator).filter(_.nonEmpty)) ++ removeCompiler(scalaClasspath) :+ " ." ).map(File (_).toURI.toURL)
217+ Jar (targetJar).mainClass match
218+ case Some (mc) =>
219+ ObjectRunner .runAndCatch(newClasspath :+ File (targetJar).toURI.toURL, mc, settings.residualArgs)
220+ case None =>
221+ Some (IllegalArgumentException (s " No main class defined in manifest in jar: $precompiledJar" ))
222+ else
223+ val properArgs =
224+ List (" -classpath" , settings.classPath.mkString(classpathSeparator)).filter(Function .const(settings.classPath.nonEmpty))
225+ ++ settings.residualArgs
226+ ++ (if settings.save then List (" -save" ) else Nil )
227+ ++ settings.scalaArgs
228+ ++ List (" -script" , settings.targetScript)
229+ ++ settings.scriptArgs
230+ scripting.Main .main(properArgs.toArray)
212231 case ExecuteMode .Guess =>
213232 if settings.modeShouldBePossibleRun then
214233 run(settings.withExecuteMode(ExecuteMode .PossibleRun ))
0 commit comments