From 4fdc7ca4144541983522d03d2c37cd288d2bd0dc Mon Sep 17 00:00:00 2001 From: Ashkan Kiani Date: Sun, 28 Jul 2019 17:31:24 -0700 Subject: [PATCH] Add release, cached, and arg flags (#10) `--release` compiles in release mode `--cached` checks for an existing executable before executing `-a, --arg` allows forward arguments to the program --- src/main.rs | 40 +++++++++++++++++++++++++++++++++++----- src/opt.rs | 6 ++++++ 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index e5fb1f9..a8b8618 100644 --- a/src/main.rs +++ b/src/main.rs @@ -120,6 +120,7 @@ fn copy_sources(temp: &PathBuf, sources: &Vec) -> Result<(), CargoPlayE fn run_cargo_build( toolchain: Option, project: &PathBuf, + release: bool, ) -> Result { let mut cargo = Command::new("cargo"); @@ -127,10 +128,16 @@ fn run_cargo_build( cargo.arg(format!("+{}", toolchain)); } - cargo + let cargo = cargo .arg("run") .arg("--manifest-path") - .arg(project.join("Cargo.toml")) + .arg(project.join("Cargo.toml")); + + if release { + cargo.arg("--release"); + } + + cargo .stderr(Stdio::inherit()) .stdout(Stdio::inherit()) .status() @@ -145,18 +152,41 @@ fn main() -> Result<(), CargoPlayError> { } let opt = opt.unwrap(); + let src_hash = opt.src_hash(); + let temp = temp_dir(opt.temp_dirname()); + + if opt.cached && temp.exists() { + let mut bin_path = temp.join("target"); + if opt.release { + bin_path.push("release"); + } else { + bin_path.push("debug"); + } + // TODO reuse logic to formulate package name, i.e. to_lowercase + bin_path.push(&src_hash.to_lowercase()); + if bin_path.exists() { + let mut cmd = Command::new(bin_path); + return cmd + .args(opt.args) + .stderr(Stdio::inherit()) + .stdout(Stdio::inherit()) + .status() + .map(|_| ()) + .map_err(CargoPlayError::from); + } + } + let files = parse_inputs(&opt.src)?; let dependencies = extract_headers(&files); - let temp = temp_dir(opt.temp_dirname()); if opt.clean { rmtemp(&temp)?; } mktemp(&temp); - write_cargo_toml(&temp, opt.src_hash(), dependencies, opt.edition)?; + write_cargo_toml(&temp, src_hash.clone(), dependencies, opt.edition)?; copy_sources(&temp, &opt.src)?; - match run_cargo_build(opt.toolchain, &temp)?.code() { + match run_cargo_build(opt.toolchain, &temp, opt.release)?.code() { Some(code) => std::process::exit(code), None => std::process::exit(-1), } diff --git a/src/opt.rs b/src/opt.rs index 83d26bc..db8165c 100644 --- a/src/opt.rs +++ b/src/opt.rs @@ -57,6 +57,12 @@ pub(crate) struct Opt { raw(possible_values = r#"&["2015", "2018"]"#) )] pub edition: RustEdition, + #[structopt(long = "release")] + pub release: bool, + #[structopt(long = "cached")] + pub cached: bool, + #[structopt(short = "a", long = "arg", multiple = true)] + pub args: Vec, } impl Opt {