Skip to content

Commit 216b70b

Browse files
committed
Handle run setting in scala script
1 parent 630579b commit 216b70b

File tree

6 files changed

+37
-5
lines changed

6 files changed

+37
-5
lines changed

compiler/src/dotty/tools/MainGenericRunner.scala

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import scala.annotation.tailrec
55
import scala.io.Source
66
import scala.util.Try
77
import java.net.URLClassLoader
8+
import sys.process._
9+
import java.io.File
10+
import java.lang.Thread
811

912
enum ExecuteMode:
1013
case Guess
@@ -20,6 +23,7 @@ case class Settings(
2023
residualArgs: List[String] = List.empty,
2124
scriptArgs: List[String] = List.empty,
2225
targetScript: String = "",
26+
areWithCompiler: Boolean = false,
2327
) {
2428
def withExecuteMode(em: ExecuteMode): Settings = this.executeMode match
2529
case ExecuteMode.Guess =>
@@ -42,10 +46,15 @@ case class Settings(
4246
println(s"not found $file")
4347
this.copy(exitCode = 2)
4448
end withTargetScript
49+
50+
def withCompiler: Settings =
51+
this.copy(areWithCompiler = true)
4552
}
4653

4754
object MainGenericRunner {
4855

56+
final val classpathSeparator = ":"
57+
4958
@tailrec
5059
def process(args: List[String], settings: Settings): Settings = args match
5160
case Nil =>
@@ -69,6 +78,8 @@ object MainGenericRunner {
6978
residualArgs = settings.residualArgs :+ "-verbose"
7079
)
7180
)
81+
case "-with-compiler" :: tail =>
82+
process(tail, settings.withCompiler)
7283
case arg :: tail =>
7384
val line = Try(Source.fromFile(arg).getLines.toList).toOption.flatMap(_.headOption)
7485
if arg.endsWith(".scala") || arg.endsWith(".sc") || (line.nonEmpty && raw"#!.*scala".r.matches(line.get)) then
@@ -85,24 +96,39 @@ object MainGenericRunner {
8596
settings.executeMode match
8697
case ExecuteMode.Repl =>
8798
val properArgs =
88-
List("-classpath", settings.classPath.mkString(";")).filter(Function.const(settings.classPath.nonEmpty))
99+
List("-classpath", settings.classPath.mkString(classpathSeparator)).filter(Function.const(settings.classPath.nonEmpty))
89100
++ settings.residualArgs
90101
repl.Main.main(properArgs.toArray)
91102
case ExecuteMode.Run =>
92103
val properArgs =
93-
List("-classpath", settings.classPath.mkString(";")).filter(Function.const(settings.classPath.nonEmpty))
104+
val newClasspath = settings.classPath ++ getClasspath :+ "."
105+
List("-classpath", newClasspath.mkString(classpathSeparator)).filter(Function.const(newClasspath.nonEmpty))
94106
++ settings.residualArgs
95-
//TODO this is just a java proxy?
107+
s"java ${properArgs.mkString(" ")}".! // For now we collect classpath that coursier provides for convenience
96108
case ExecuteMode.Script =>
97109
val properArgs =
98-
List("classpath", settings.classPath.mkString(";")).filter(Function.const(settings.classPath.nonEmpty))
110+
List("-classpath", settings.classPath.mkString(classpathSeparator)).filter(Function.const(settings.classPath.nonEmpty))
99111
++ settings.residualArgs
100112
++ List("-script", settings.targetScript)
101113
++ settings.scriptArgs
102114
scripting.Main.main(properArgs.toArray)
103115
case ExecuteMode.Guess =>
104116
val properArgs =
105-
List("-classpath", settings.classPath.mkString(";")).filter(Function.const(settings.classPath.nonEmpty))
117+
List("-classpath", settings.classPath.mkString(classpathSeparator)).filter(Function.const(settings.classPath.nonEmpty))
106118
++ settings.residualArgs
107119
repl.Main.main(properArgs.toArray)
120+
121+
122+
private def getClasspath(cl: ClassLoader): Array[String] = cl match
123+
case null => Array()
124+
case u: URLClassLoader => u.getURLs.map(_.toURI.toString) ++ getClasspath(cl.getParent)
125+
case cl if cl.getClass.getName == "jdk.internal.loader.ClassLoaders$AppClassLoader" =>
126+
// Required with JDK >= 9
127+
sys.props.getOrElse("java.class.path", "")
128+
.split(File.pathSeparator)
129+
.filter(_.nonEmpty)
130+
case _ => getClasspath(cl.getParent)
131+
132+
private def getClasspath: List[String] =
133+
getClasspath(Thread.currentThread().getContextClassLoader).toList
108134
}
2.16 KB
Binary file not shown.
664 Bytes
Binary file not shown.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
object myfile extends App:
2+
println("Hello")
514 Bytes
Binary file not shown.

compiler/test/dotty/tools/coursier/CoursierScalaTests.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ class CoursierScalaTests:
5656
assert(output.mkString("\n").contains("Unable to create a system terminal")) // Scala attempted to create REPL so we can assume it is working
5757
repl()
5858

59+
def run() =
60+
val output = CoursierScalaTests.csCmd("-run", "-classpath", scripts("/run").head.getParent, "myfile")
61+
assert(output.mkString("\n") == "Hello")
62+
run()
5963

6064
object CoursierScalaTests:
6165

0 commit comments

Comments
 (0)