enso/project/Cargo.scala

84 lines
2.2 KiB
Scala
Raw Normal View History

2020-08-14 12:10:52 +03:00
import sbt.Keys._
import sbt._
import sbt.internal.util.ManagedLogger
import scala.sys.process._
/** A wrapper for executing the command `cargo`. */
object Cargo {
private val cargoCmd = "cargo"
private val rustUpCmd = "rustup"
private var wasCargoOk: Boolean = false
2020-08-14 12:10:52 +03:00
/** Executes the command `cargo $args`. */
def apply(args: Seq[String]): Def.Initialize[Task[Unit]] =
2020-09-09 16:37:26 +03:00
Def.task {
run(args, state.value.log)
2020-09-09 16:37:26 +03:00
}
2020-08-14 12:10:52 +03:00
/** Executes the command `cargo $args`.
2020-09-09 16:37:26 +03:00
*
* @param args arguments to pass to cargo
* @param log a logger instance for diagnostics
* @param extraEnv additional environment variables that should be set for
* the cargo process
*/
def run(
args: Seq[String],
2020-09-09 16:37:26 +03:00
log: ManagedLogger,
extraEnv: Seq[(String, String)] = Seq()
): Unit = {
val cmd: Seq[String] = Seq(cargoCmd) ++ args
2020-08-14 12:10:52 +03:00
if (!cargoOk(log))
throw new RuntimeException("Cargo isn't installed!")
log.info(cmd.toString())
2020-08-14 12:10:52 +03:00
2020-09-09 16:37:26 +03:00
val exitCode =
try Process(cmd, None, extraEnv: _*).!
catch {
case _: RuntimeException =>
throw new RuntimeException(s"`$cargoCmd` command failed to run.")
2020-09-09 16:37:26 +03:00
}
if (exitCode != 0) {
throw new RuntimeException(
s"`$cargoCmd` command returned a non-zero exit code: $exitCode."
2020-09-09 16:37:26 +03:00
)
2020-08-14 12:10:52 +03:00
}
}
def rustUp(target: String, log: ManagedLogger): Unit = {
val cmd: Seq[String] = Seq(rustUpCmd) ++ Seq("target", "add", target)
log.info(cmd.toString())
val exitCode =
try Process(cmd, None).!
catch {
case _: RuntimeException =>
throw new RuntimeException(s"`$rustUpCmd` command failed to run.")
}
if (exitCode != 0) {
throw new RuntimeException(
s"`$rustUpCmd` command returned a non-zero exit code: $exitCode."
)
}
}
2020-08-14 12:10:52 +03:00
/** Checks that cargo is installed. Logs an error and returns false if not. */
def cargoOk(log: ManagedLogger): Boolean = if (wasCargoOk) true
else {
try {
s"$cargoCmd version".!!
wasCargoOk = true
true
} catch {
2020-08-14 12:10:52 +03:00
case _: RuntimeException =>
log.error(s"The command `cargo` isn't on path. Did you install cargo?")
false
2020-08-14 12:10:52 +03:00
}
}
}