@@ -158,6 +158,13 @@ object MainGenericRunner {
158
158
val settings = process(allArgs.toList, Settings ())
159
159
if settings.exitCode != 0 then System .exit(settings.exitCode)
160
160
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
+
161
168
def run (settings : Settings ): Unit = settings.executeMode match
162
169
case ExecuteMode .Repl =>
163
170
val properArgs =
@@ -181,13 +188,6 @@ object MainGenericRunner {
181
188
run(settings.withExecuteMode(ExecuteMode .Repl ))
182
189
case ExecuteMode .Run =>
183
190
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
191
191
val newClasspath = (settings.classPath.flatMap(_.split(classpathSeparator).filter(_.nonEmpty)) ++ removeCompiler(scalaClasspath) :+ " ." ).map(File (_).toURI.toURL)
192
192
val res = ObjectRunner .runAndCatch(newClasspath, settings.targetToRun, settings.residualArgs).flatMap {
193
193
case ex : ClassNotFoundException if ex.getMessage == settings.targetToRun =>
@@ -201,14 +201,33 @@ object MainGenericRunner {
201
201
}
202
202
errorFn(" " , res)
203
203
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)
212
231
case ExecuteMode .Guess =>
213
232
if settings.modeShouldBePossibleRun then
214
233
run(settings.withExecuteMode(ExecuteMode .PossibleRun ))
0 commit comments