Skip to content

Commit

Permalink
Add --command and --tmp-dir to run command
Browse files Browse the repository at this point in the history
  • Loading branch information
alexarchambault committed Jul 8, 2022
1 parent a0ab00f commit 94e4929
Show file tree
Hide file tree
Showing 8 changed files with 310 additions and 91 deletions.
97 changes: 69 additions & 28 deletions modules/build/src/main/scala/scala/build/internal/Runner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,20 @@ object Runner {
}
}

def runJvm(
private def envCommand(env: Map[String, String]): Seq[String] =
env.toVector.sortBy(_._1).map {
case (k, v) =>
s"$k=$v"
}

def jvmCommand(
javaCommand: String,
javaArgs: Seq[String],
classPath: Seq[File],
mainClass: String,
args: Seq[String],
logger: Logger,
allowExecve: Boolean = false,
cwd: Option[os.Path] = None
): Process = {
extraEnv: Map[String, String] = Map.empty
): Seq[String] = {

val command =
Seq(javaCommand) ++
Expand All @@ -120,10 +124,27 @@ object Runner {
) ++
args

envCommand(extraEnv) ++ command
}

def runJvm(
javaCommand: String,
javaArgs: Seq[String],
classPath: Seq[File],
mainClass: String,
args: Seq[String],
logger: Logger,
allowExecve: Boolean = false,
cwd: Option[os.Path] = None,
extraEnv: Map[String, String] = Map.empty
): Process = {

val command = jvmCommand(javaCommand, javaArgs, classPath, mainClass, args, Map.empty)

if (allowExecve)
maybeExec("java", command, logger, cwd = cwd)
maybeExec("java", command, logger, cwd = cwd, extraEnv = extraEnv)
else
run(command, logger, cwd = cwd)
run(command, logger, cwd = cwd, extraEnv = extraEnv)
}

private def endsWithCaseInsensitive(s: String, suffix: String): Boolean =
Expand Down Expand Up @@ -155,6 +176,23 @@ object Runner {
}
}

def jsCommand(
entrypoint: File,
args: Seq[String],
jsDom: Boolean = false
): Seq[String] = {

val nodePath = findInPath("node").fold("node")(_.toString)
val command = Seq(nodePath, entrypoint.getAbsolutePath) ++ args

if (jsDom)
// FIXME We'd need to replicate what JSDOMNodeJSEnv does under-the-hood to get the command in that case.
// --command is mostly for debugging purposes, so I'm not sure it matters much here…
sys.error("Cannot get command when JSDOM is enabled.")
else
"node" +: command.tail
}

def runJs(
entrypoint: File,
args: Seq[String],
Expand All @@ -168,31 +206,17 @@ object Runner {
import logger.{log, debug}

val nodePath = findInPath("node").fold("node")(_.toString)
val command = Seq(nodePath, entrypoint.getAbsolutePath) ++ args

log(
s"Running ${command.mkString(" ")}",
" Running" + System.lineSeparator() +
command.iterator.map(_ + System.lineSeparator()).mkString
)
if (!jsDom && allowExecve && Execve.available()) {

lazy val envJs =
if (jsDom)
new JSDOMNodeJSEnv(
JSDOMNodeJSEnv.Config()
.withExecutable(nodePath)
.withArgs(Nil)
.withEnv(Map.empty)
)
else new NodeJSEnv(
NodeJSEnv.Config()
.withExecutable(nodePath)
.withArgs(Nil)
.withEnv(Map.empty)
.withSourceMap(sourceMap)
val command = Seq(nodePath, entrypoint.getAbsolutePath) ++ args

log(
s"Running ${command.mkString(" ")}",
" Running" + System.lineSeparator() +
command.iterator.map(_ + System.lineSeparator()).mkString
)

if (!jsDom && allowExecve && Execve.available()) {
debug("execve available")
Execve.execve(
command.head,
Expand All @@ -202,6 +226,23 @@ object Runner {
sys.error("should not happen")
}
else {

val envJs =
if (jsDom)
new JSDOMNodeJSEnv(
JSDOMNodeJSEnv.Config()
.withExecutable(nodePath)
.withArgs(Nil)
.withEnv(Map.empty)
)
else new NodeJSEnv(
NodeJSEnv.Config()
.withExecutable(nodePath)
.withArgs(Nil)
.withEnv(Map.empty)
.withSourceMap(sourceMap)
)

val inputs = Seq(
if (esModule) Input.ESModule(entrypoint.toPath)
else Input.Script(entrypoint.toPath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,14 @@ final case class RunOptions(
@Recurse
compileCross: CompileCrossOptions = CompileCrossOptions(),
@Recurse
mainClass: MainClassOptions = MainClassOptions()
)
mainClass: MainClassOptions = MainClassOptions(),
@Group("Run")
@HelpMessage("Print the command that would have been run (one argument per line), rather than running it")
command: Boolean = false,
@Group("Run")
@HelpMessage("Temporary directory where to write generated launchers")
tmpDir: Option[String] = None
)
// format: on

object RunOptions {
Expand Down
13 changes: 9 additions & 4 deletions modules/cli/src/main/scala/scala/cli/commands/Package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -674,11 +674,15 @@ object Package extends ScalaCommand[PackageOptions] {
config: ScalaJsLinkerConfig,
fullOpt: Boolean,
noOpt: Boolean,
logger: Logger
logger: Logger,
tmpDirOpt: Option[os.Path] = None
): Either[BuildException, os.Path] =
Library.withLibraryJar(build, dest.last.stripSuffix(".jar")) { mainJar =>
val classPath = mainJar +: build.artifacts.classPath
val linkingDir = os.temp.dir(prefix = "scala-cli-js-linking")
val classPath = mainJar +: build.artifacts.classPath
val delete = tmpDirOpt.isEmpty
tmpDirOpt.foreach(os.makeDir.all(_))
val linkingDir =
os.temp.dir(dir = tmpDirOpt.orNull, prefix = "scala-cli-js-linking", deleteOnExit = delete)
either {
value {
ScalaJsLinker.link(
Expand Down Expand Up @@ -729,7 +733,8 @@ object Package extends ScalaCommand[PackageOptions] {
os.copy(sourceMapJs, sourceMapDest, replaceExisting = true)
logger.message(s"Emitted js source maps to: $sourceMapDest")
}
os.remove.all(linkingDir)
if (delete)
os.remove.all(linkingDir)
dest
}
else {
Expand Down
Loading

0 comments on commit 94e4929

Please sign in to comment.