@@ -4,28 +4,26 @@ package dotty.tools
4
4
import scala .annotation .tailrec
5
5
import scala .io .Source
6
6
import scala .util .{ Try , Success , Failure }
7
- import java .net .URLClassLoader
8
- import sys .process ._
9
7
import java .io .File
10
8
import java .lang .Thread
11
9
import scala .annotation .internal .sharable
12
10
import dotty .tools .dotc .util .ClasspathFromClassloader
13
11
import dotty .tools .runner .ObjectRunner
14
12
import dotty .tools .dotc .config .Properties .envOrNone
15
13
import java .util .jar ._
16
- import java .util .jar .Attributes .Name
17
14
import dotty .tools .io .Jar
18
15
import dotty .tools .runner .ScalaClassLoader
19
16
import java .nio .file .{Files , Paths , Path }
20
- import scala .collection .JavaConverters ._
21
17
import dotty .tools .dotc .config .CommandLineParser
18
+ import dotty .tools .scripting .StringDriver
22
19
23
20
enum ExecuteMode :
24
21
case Guess
25
22
case Script
26
23
case Repl
27
24
case Run
28
25
case PossibleRun
26
+ case Expression
29
27
30
28
case class Settings (
31
29
verbose : Boolean = false ,
@@ -38,6 +36,7 @@ case class Settings(
38
36
possibleEntryPaths : List [String ] = List .empty,
39
37
scriptArgs : List [String ] = List .empty,
40
38
targetScript : String = " " ,
39
+ targetExpression : String = " " ,
41
40
targetToRun : String = " " ,
42
41
save : Boolean = false ,
43
42
modeShouldBePossibleRun : Boolean = false ,
@@ -78,6 +77,9 @@ case class Settings(
78
77
def withTargetToRun (targetToRun : String ): Settings =
79
78
this .copy(targetToRun = targetToRun)
80
79
80
+ def withExpression (scalaSource : String ): Settings =
81
+ this .copy(targetExpression = scalaSource)
82
+
81
83
def withSave : Settings =
82
84
this .copy(save = true )
83
85
@@ -150,12 +152,12 @@ object MainGenericRunner {
150
152
case (o @ colorOption(_* )) :: tail =>
151
153
process(tail, settings.withScalaArgs(o))
152
154
case " -e" :: expression :: tail =>
153
- val tempScript = writeFile( s " @main def main(args: String *): Unit = \n ${expression}" )
155
+ val mainSource = s " @main def main(args: String *): Unit = \n ${expression}"
154
156
settings
155
- .withExecuteMode(ExecuteMode .Script )
156
- .withTargetScript(tempScript )
157
+ .withExecuteMode(ExecuteMode .Expression )
158
+ .withExpression(mainSource )
157
159
.withScriptArgs(tail* )
158
- .noSave // useless
160
+ .noSave // -save not useful here
159
161
case arg :: tail =>
160
162
val line = Try (Source .fromFile(arg).getLines.toList).toOption.flatMap(_.headOption)
161
163
lazy val hasScalaHashbang = { val s = line.getOrElse(" " ) ; s.startsWith(" #!" ) && s.contains(" scala" ) }
@@ -243,6 +245,16 @@ object MainGenericRunner {
243
245
++ List (" -script" , settings.targetScript)
244
246
++ settings.scriptArgs
245
247
scripting.Main .main(properArgs.toArray)
248
+ case ExecuteMode .Expression =>
249
+ val cp = settings.classPath match {
250
+ case Nil => " "
251
+ case list => list.mkString(classpathSeparator)
252
+ }
253
+ val cpArgs = if cp.isEmpty then Nil else List (" -classpath" , cp)
254
+ val properArgs = cpArgs ++ settings.residualArgs ++ settings.scalaArgs
255
+ val driver = StringDriver (properArgs.toArray, settings.targetExpression)
256
+ driver.compileAndRun(settings.classPath)
257
+
246
258
case ExecuteMode .Guess =>
247
259
if settings.modeShouldBePossibleRun then
248
260
run(settings.withExecuteMode(ExecuteMode .PossibleRun ))
@@ -259,11 +271,4 @@ object MainGenericRunner {
259
271
e.foreach(_.printStackTrace())
260
272
! isFailure
261
273
}
262
- def writeFile (contents : String ): String = {
263
- val file = Files .createTempFile(" exec" , " .scala" )
264
- file.toFile.deleteOnExit()
265
- Files .write(file, contents.getBytes)
266
- file.toString.replace('\\ ' , '/' )
267
- }
268
-
269
274
}
0 commit comments