Skip to content

Commit 9bce142

Browse files
committed
add -e <expression> eval to command line
1 parent 1292246 commit 9bce142

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

compiler/src/dotty/tools/MainGenericRunner.scala

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ case class Settings(
8181
def withSave: Settings =
8282
this.copy(save = true)
8383

84+
def noSave: Settings =
85+
this.copy(save = false)
86+
8487
def withModeShouldBePossibleRun: Settings =
8588
this.copy(modeShouldBePossibleRun = true)
8689

@@ -144,6 +147,13 @@ object MainGenericRunner {
144147
process(remainingArgs, settings)
145148
case (o @ colorOption(_*)) :: tail =>
146149
process(tail, settings.withScalaArgs(o))
150+
case "-e" :: expression :: tail =>
151+
val tempScript = writeFile(s"@main def main(args: String *): Unit =\n ${expression}")
152+
settings
153+
.withExecuteMode(ExecuteMode.Script)
154+
.withTargetScript(tempScript)
155+
.withScriptArgs(tail*)
156+
.noSave // useless
147157
case arg :: tail =>
148158
val line = Try(Source.fromFile(arg).getLines.toList).toOption.flatMap(_.headOption)
149159
lazy val hasScalaHashbang = { val s = line.getOrElse("") ; s.startsWith("#!") && s.contains("scala") }
@@ -156,6 +166,7 @@ object MainGenericRunner {
156166
val newSettings = if arg.startsWith("-") then settings else settings.withPossibleEntryPaths(arg).withModeShouldBePossibleRun
157167
process(tail, newSettings.withResidualArgs(arg))
158168

169+
159170
def main(args: Array[String]): Unit =
160171
val scalaOpts = envOrNone("SCALA_OPTS").toArray.flatMap(_.split(" ")).filter(_.nonEmpty)
161172
val allArgs = scalaOpts ++ args
@@ -244,4 +255,11 @@ object MainGenericRunner {
244255
e.foreach(_.printStackTrace())
245256
!isFailure
246257
}
258+
def writeFile(contents: String): String = {
259+
val file = Files.createTempFile("exec", ".scala")
260+
file.toFile.deleteOnExit()
261+
Files.write(file, contents.getBytes)
262+
file.toString.replace('\\', '/')
263+
}
264+
247265
}

compiler/test/dotty/tools/scripting/BashScriptsTests.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,18 @@ class BashScriptsTests:
188188
if valid then printf(s"\n===> success: classpath begins with %s, as reported by [%s]\n", workingDirectory, scriptFile.getName)
189189
assert(valid, s"script ${scriptFile.absPath} did not report valid java.class.path first entry")
190190

191+
/*
192+
* verify -e println("yo!") works.
193+
*/
194+
@Test def verifyCommandLineExpression =
195+
printf("===> verify -e <expression> is properly handled by `dist/bin/scala`\n")
196+
val expected = "9"
197+
val expression = s"println(3*3)"
198+
val cmd = s"bin/scala -e $expression"
199+
val (validTest, exitCode, stdout, stderr) = bashCommand(s"""bin/scala -e '$expression'""")
200+
val result = stdout.filter(_.nonEmpty).mkString("")
201+
printf("stdout: %s\n", result)
202+
printf("stderr: %s\n", stderr.mkString("\n","\n",""))
203+
if verifyValid(validTest) then
204+
assert(result.contains(expected), s"expression [$expression] did not send [$expected] to stdout")
205+

0 commit comments

Comments
 (0)