Skip to content

Commit 4b2ac53

Browse files
KacperFKorbanBarkingBad
authored andcommitted
MainGenericRunner should work almost
1 parent 27e221b commit 4b2ac53

File tree

2 files changed

+114
-2
lines changed

2 files changed

+114
-2
lines changed

compiler/src/MainGenericRunner.scala

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package dotty.tools
2+
3+
import scala.annotation.tailrec
4+
import scala.io.Source
5+
import scala.util.Try
6+
import scala.util.matching.Regex
7+
import java.net.URLClassLoader
8+
9+
enum ExecuteMode:
10+
case Guess
11+
case Script
12+
case Repl
13+
case Run
14+
15+
case class Settings(
16+
verbose: Boolean = false,
17+
classPath: List[String] = List.empty,
18+
executeMode: ExecuteMode = ExecuteMode.Guess,
19+
exitCode: Int = 0,
20+
residualArgs: List[String] = List.empty,
21+
scriptArgs: List[String] = List.empty,
22+
targetScript: String = "",
23+
) {
24+
def withExecuteMode(em: ExecuteMode): Settings = this.executeMode match
25+
case ExecuteMode.Guess =>
26+
this.copy(executeMode = em)
27+
case _ =>
28+
println(s"execute_mode==[$executeMode], attempted overwrite by [$em]")
29+
this.copy(exitCode = 1)
30+
end withExecuteMode
31+
32+
def withResidualArgs(args: String*): Settings =
33+
this.copy(residualArgs = residualArgs.appendedAll(args.toList))
34+
35+
def withScriptArgs(args: String*): Settings =
36+
this.copy(scriptArgs = scriptArgs.appendedAll(args.toList))
37+
38+
def withTargetScript(file: String): Settings =
39+
Try(Source.fromFile(file)).toOption match
40+
case Some(_) => this.copy(targetScript = file)
41+
case None =>
42+
println(s"not found $file")
43+
this.copy(exitCode = 2)
44+
end withTargetScript
45+
}
46+
47+
object MainGenericRunner {
48+
49+
val shebangscala: Regex = """#!.*scala""".r
50+
51+
@tailrec
52+
def process(args: List[String], settings: Settings): Settings = args match
53+
case Nil =>
54+
settings
55+
case "-repl" :: tail =>
56+
process(tail, settings.withExecuteMode(ExecuteMode.Repl))
57+
case "-run" :: tail =>
58+
process(tail, settings.withExecuteMode(ExecuteMode.Run))
59+
case ("-cp" | "-classpath" | "--classpath") :: cp :: tail =>
60+
process(tail, settings.copy(classPath = settings.classPath.appended(cp)))
61+
case ("-version" | "--version") :: _ =>
62+
settings.copy(
63+
executeMode = ExecuteMode.Repl,
64+
residualArgs = List("-version")
65+
)
66+
case ("-v" | "-verbose" | "--verbose") :: tail =>
67+
process(
68+
tail,
69+
settings.copy(
70+
verbose = true,
71+
residualArgs = settings.residualArgs :+ "-verbose"
72+
)
73+
)
74+
case arg :: tail =>
75+
val line = Try(Source.fromFile(arg).getLines.toList).toOption.flatMap(_.headOption)
76+
val newSettings = if arg.endsWith(".scala") || arg.endsWith(".sc") || (line.nonEmpty && shebangscala.matches(line.get))
77+
then {
78+
settings
79+
.withExecuteMode(ExecuteMode.Script)
80+
.withTargetScript(arg)
81+
.withScriptArgs(tail*)
82+
} else
83+
settings.withResidualArgs(arg)
84+
process(tail, newSettings.withResidualArgs(arg))
85+
86+
def main(args: Array[String]): Unit =
87+
val settings = process(args.toList, Settings())
88+
if settings.exitCode != 0 then System.exit(settings.exitCode)
89+
settings.executeMode match
90+
case ExecuteMode.Repl =>
91+
val properArgs =
92+
List("-classpath", settings.classPath.mkString(";")).filter(Function.const(settings.classPath.nonEmpty))
93+
++ settings.residualArgs
94+
repl.Main.main(properArgs.toArray)
95+
case ExecuteMode.Run =>
96+
val properArgs =
97+
List("-classpath", settings.classPath.mkString(";")).filter(Function.const(settings.classPath.nonEmpty))
98+
++ settings.residualArgs
99+
//TODO this is just a java proxy?
100+
case ExecuteMode.Script =>
101+
val properArgs =
102+
List("classpath", settings.classPath.mkString(";")).filter(Function.const(settings.classPath.nonEmpty))
103+
++ settings.residualArgs
104+
++ List("-script", settings.targetScript)
105+
++ settings.scriptArgs
106+
scripting.Main.main(properArgs.toArray)
107+
case ExecuteMode.Guess =>
108+
val properArgs =
109+
List("-classpath", settings.classPath.mkString(";")).filter(Function.const(settings.classPath.nonEmpty))
110+
++ settings.residualArgs
111+
repl.Main.main(properArgs.toArray)
112+
}

compiler/src/dotty/tools/scripting/Main.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package dotty.tools.scripting
22

33
import java.io.File
44
import java.nio.file.{Path, Paths}
5-
import dotty.tools.dotc.config.Properties.isWin
5+
import dotty.tools.dotc.config.Properties.isWin
66

77
/** Main entry point to the Scripting execution engine */
88
object Main:
@@ -87,7 +87,7 @@ object Main:
8787
case s if s.startsWith("./") => s.drop(2)
8888
case s => s
8989
}
90-
90+
9191
// convert to absolute path relative to cwd.
9292
def absPath: String = norm match
9393
case str if str.isAbsolute => norm

0 commit comments

Comments
 (0)