-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
anatoliykmetyuk
merged 3 commits into
scala:master
from
philwalk:cli-add-expression-eval-12648
Jan 20, 2022
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package dotty.tools.scripting | ||
|
||
import java.nio.file.{ Files, Paths, Path } | ||
|
||
import dotty.tools.dotc.Driver | ||
import dotty.tools.dotc.core.Contexts, Contexts.{ Context, ctx } | ||
import dotty.tools.io.{ PlainDirectory, Directory, ClassPath } | ||
import Util.* | ||
|
||
class StringDriver(compilerArgs: Array[String], scalaSource: String) extends Driver: | ||
override def sourcesRequired: Boolean = false | ||
|
||
def compileAndRun(classpath: List[String] = Nil): Unit = | ||
val outDir = Files.createTempDirectory("scala3-expression") | ||
outDir.toFile.deleteOnExit() | ||
|
||
setup(compilerArgs, initCtx.fresh) match | ||
case Some((toCompile, rootCtx)) => | ||
given Context = rootCtx.fresh.setSetting(rootCtx.settings.outputDir, | ||
new PlainDirectory(Directory(outDir))) | ||
|
||
val compiler = newCompiler | ||
compiler.newRun.compileFromStrings(List(scalaSource)) | ||
|
||
val output = ctx.settings.outputDir.value | ||
if ctx.reporter.hasErrors then | ||
throw StringDriverException("Errors encountered during compilation") | ||
|
||
try | ||
val classpath = s"${ctx.settings.classpath.value}${pathsep}${sys.props("java.class.path")}" | ||
val classpathEntries: Seq[Path] = ClassPath.expandPath(classpath, expandStar=true).map { Paths.get(_) } | ||
sys.props("java.class.path") = classpathEntries.map(_.toString).mkString(pathsep) | ||
val (mainClass, mainMethod) = detectMainClassAndMethod(outDir, classpathEntries, scalaSource) | ||
mainMethod.invoke(null, Array.empty[String]) | ||
catch | ||
case e: java.lang.reflect.InvocationTargetException => | ||
throw e.getCause | ||
finally | ||
deleteFile(outDir.toFile) | ||
case None => | ||
end compileAndRun | ||
|
||
end StringDriver | ||
|
||
case class StringDriverException(msg: String) extends RuntimeException(msg) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package dotty.tools.scripting | ||
|
||
import java.nio.file.{ Path } | ||
import java.io.File | ||
import java.net.{ URLClassLoader } | ||
import java.lang.reflect.{ Modifier, Method } | ||
|
||
object Util: | ||
|
||
def deleteFile(target: File): Unit = | ||
if target.isDirectory then | ||
for member <- target.listFiles.toList | ||
do deleteFile(member) | ||
target.delete() | ||
end deleteFile | ||
|
||
def detectMainClassAndMethod(outDir: Path, classpathEntries: Seq[Path], srcFile: String): (String, Method) = | ||
val classpathUrls = (classpathEntries :+ outDir).map { _.toUri.toURL } | ||
val cl = URLClassLoader(classpathUrls.toArray) | ||
|
||
def collectMainMethods(target: File, path: String): List[(String, Method)] = | ||
val nameWithoutExtension = target.getName.takeWhile(_ != '.') | ||
val targetPath = | ||
if path.nonEmpty then s"${path}.${nameWithoutExtension}" | ||
else nameWithoutExtension | ||
|
||
if target.isDirectory then | ||
for | ||
packageMember <- target.listFiles.toList | ||
membersMainMethod <- collectMainMethods(packageMember, targetPath) | ||
yield membersMainMethod | ||
else if target.getName.endsWith(".class") then | ||
val cls = cl.loadClass(targetPath) | ||
try | ||
val method = cls.getMethod("main", classOf[Array[String]]) | ||
if Modifier.isStatic(method.getModifiers) then List((cls.getName, method)) else Nil | ||
catch | ||
case _: java.lang.NoSuchMethodException => Nil | ||
else Nil | ||
end collectMainMethods | ||
|
||
val mains = for | ||
file <- outDir.toFile.listFiles.toList | ||
method <- collectMainMethods(file, "") | ||
yield method | ||
|
||
mains match | ||
case Nil => | ||
throw StringDriverException(s"No main methods detected for [${srcFile}]") | ||
case _ :: _ :: _ => | ||
throw StringDriverException( | ||
s"internal error: Detected the following main methods:\n${mains.mkString("\n")}") | ||
case m :: Nil => m | ||
end match | ||
end detectMainClassAndMethod | ||
|
||
def pathsep = sys.props("path.separator") | ||
|
||
end Util | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package dotty | ||
package tools | ||
package scripting | ||
|
||
import java.nio.file.Paths | ||
import org.junit.{Test, AfterClass} | ||
import org.junit.Assert.assertEquals | ||
|
||
import vulpix.TestConfiguration | ||
|
||
import ScriptTestEnv.* | ||
|
||
/** | ||
* +. test scala -e <expression> | ||
*/ | ||
class ExpressionTest: | ||
/* | ||
* verify -e <expression> 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 result = getResult(expression) | ||
assert(result.contains(expected), s"expression [$expression] did not send [$expected] to stdout") | ||
|
||
@Test def verifyImports: Unit = | ||
val expressionLines = List( | ||
"import java.nio.file.Paths", | ||
"""val cwd = Paths.get(""."")""", | ||
"""println(cwd.toFile.listFiles.toList.filter(_.isDirectory).size)""", | ||
) | ||
val expression = expressionLines.mkString(";") | ||
testExpression(expression){ result => | ||
result.matches("[0-9]+") && result.toInt > 0 | ||
} | ||
|
||
def getResult(expression: String): String = | ||
val cmd = s"bin/scala -e $expression" | ||
val (_, _, stdout, stderr) = bashCommand(s"""bin/scala -e '$expression'""") | ||
printf("stdout: %s\n", stdout.mkString("|")) | ||
printf("stderr: %s\n", stderr.mkString("\n","\n","")) | ||
stdout.filter(_.nonEmpty).mkString("") | ||
|
||
def testExpression(expression: String)(check: (result: String) => Boolean) = { | ||
val result = getResult(expression) | ||
check(result) | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a previous effort to avoid dependencies on packages that might become modules in 2.13, which did not happen. I'm conflicted about
sys.props
because OT1H it's just aMap
interface forSystem.getProperty
ories
, but OTOH it is not so trivial that it didn't harbor a couple of bugs.I guess the convenience here is
java.io.File.pathSeparator
. No big deal, I just happened to have recently seen the Scala 2 commit about not usingsys.props
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wasn't aware of the discussion around packages that might become modules, but
java.io.File.pathSeparator
is a good alternative in any case.It turns out that
pathsep
is defined identically in dotty.tools.scripting.Main, so the definition maybe belongs in dotty.tools.scripting.Util.