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 {
|
2023-03-11 03:15:58 +03:00
|
|
|
private val cargoCmd = "cargo"
|
2024-04-29 16:22:41 +03:00
|
|
|
private val rustUpCmd = "rustup"
|
2023-02-08 13:46:40 +03:00
|
|
|
private var wasCargoOk: Boolean = false
|
2020-08-14 12:10:52 +03:00
|
|
|
|
2021-12-27 19:56:35 +03:00
|
|
|
/** Executes the command `cargo $args`. */
|
2023-02-08 13:46:40 +03:00
|
|
|
def apply(args: Seq[String]): Def.Initialize[Task[Unit]] =
|
2020-09-09 16:37:26 +03:00
|
|
|
Def.task {
|
2021-12-27 19:56:35 +03:00
|
|
|
run(args, state.value.log)
|
2020-09-09 16:37:26 +03:00
|
|
|
}
|
2020-08-14 12:10:52 +03:00
|
|
|
|
2021-12-27 19:56:35 +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(
|
2023-02-08 13:46:40 +03:00
|
|
|
args: Seq[String],
|
2020-09-09 16:37:26 +03:00
|
|
|
log: ManagedLogger,
|
|
|
|
extraEnv: Seq[(String, String)] = Seq()
|
|
|
|
): Unit = {
|
2023-02-08 13:46:40 +03:00
|
|
|
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!")
|
|
|
|
|
2023-02-08 13:46:40 +03:00
|
|
|
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 =>
|
2024-04-29 16:22:41 +03:00
|
|
|
throw new RuntimeException(s"`$cargoCmd` command failed to run.")
|
2020-09-09 16:37:26 +03:00
|
|
|
}
|
|
|
|
if (exitCode != 0) {
|
|
|
|
throw new RuntimeException(
|
2024-04-29 16:22:41 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-29 16:22:41 +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. */
|
2023-03-11 03:15:58 +03:00
|
|
|
def cargoOk(log: ManagedLogger): Boolean = if (wasCargoOk) true
|
|
|
|
else {
|
2023-02-08 13:46:40 +03:00
|
|
|
try {
|
|
|
|
s"$cargoCmd version".!!
|
|
|
|
wasCargoOk = true
|
|
|
|
true
|
2023-03-11 03:15:58 +03:00
|
|
|
} 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?")
|
2023-02-08 13:46:40 +03:00
|
|
|
false
|
2020-08-14 12:10:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|