Skip to content

add eval (-e) expression evaluation to command line #14263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 20, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions compiler/src/dotty/tools/MainGenericRunner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,13 @@ object MainGenericRunner {
process(remainingArgs, settings)
case (o @ colorOption(_*)) :: tail =>
process(tail, settings.withScalaArgs(o))
case "-e" :: expression :: tail =>
val tempScript = writeFile(s"@main def main(args: String *): Unit =\n ${expression}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would omit the newline character as a needless complication.

I don't remember whether scala 2 has to write a file, but I bet it doesn't. Is it necessary to write a file?

Tired recently of deleting scala temp files from my WSL /tmp due to running scala and dotty tests.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, possibly newline and indent matters for braceless syntax.

Copy link
Contributor Author

@philwalk philwalk Jan 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would omit the newline character as a needless complication.

Sounds good.

Tired recently of deleting scala temp files from my WSL /tmp due to running scala and dotty tests.

Totally agree. I reviewed the scala2 code, and it doesn't create a file, although it's has different Runners available.
I will poke around a bi more and see if I can find something.

settings
.withExecuteMode(ExecuteMode.Script)
.withTargetScript(tempScript)
.withScriptArgs(tail*)
.noSave // useless
case arg :: tail =>
val line = Try(Source.fromFile(arg).getLines.toList).toOption.flatMap(_.headOption)
lazy val hasScalaHashbang = { val s = line.getOrElse("") ; s.startsWith("#!") && s.contains("scala") }
Expand All @@ -161,6 +168,7 @@ object MainGenericRunner {
val newSettings = if arg.startsWith("-") then settings else settings.withPossibleEntryPaths(arg).withModeShouldBePossibleRun
process(tail, newSettings.withResidualArgs(arg))


def main(args: Array[String]): Unit =
val scalaOpts = envOrNone("SCALA_OPTS").toArray.flatMap(_.split(" ")).filter(_.nonEmpty)
val allArgs = scalaOpts ++ args
Expand Down Expand Up @@ -251,4 +259,11 @@ object MainGenericRunner {
e.foreach(_.printStackTrace())
!isFailure
}
def writeFile(contents: String): String = {
val file = Files.createTempFile("exec", ".scala")
file.toFile.deleteOnExit()
Files.write(file, contents.getBytes)
file.toString.replace('\\', '/')
}

}
15 changes: 15 additions & 0 deletions compiler/test/dotty/tools/scripting/BashScriptsTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,18 @@ class BashScriptsTests:
assert(valid, s"script ${scriptFile.absPath} reported unexpected value for java.sql.Date ${stdout.mkString("\n")}")
assert(!testJar.exists,s"unexpected, jar file [$testJar] was created")


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