From ac029a5873b25e806b8309872621bae2acd6f9f8 Mon Sep 17 00:00:00 2001 From: Christopher Brickley Date: Mon, 17 Aug 2015 20:47:31 -0400 Subject: [PATCH] feat(raw): #11 support running any cargo command MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add a "raw" option to support running arbitrary cargo commands. For example, `cargo watch raw "test ex_ —release"` will run `cargo test ex_ —release` Also supports multiple arguments: `cargo watch raw "build" "test"` will run `cargo build` followed by `cargo test`. --- src/cargo.rs | 14 ++++++++++++++ src/compile.rs | 5 ++++- src/main.rs | 16 ++++++++++++---- 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/cargo.rs b/src/cargo.rs index e6fd2da..e0e352b 100644 --- a/src/cargo.rs +++ b/src/cargo.rs @@ -54,3 +54,17 @@ pub fn run(cmd: &str) { Err(e) => println!("Failed to execute 'cargo {}': {}", cmd, e) }; } + +/// Runs cargo by passing through a set of multiple commands and displays the output. +pub fn run_raw(cmds: &str) { + let cmds_vec: Vec<&str> = cmds.split_whitespace().collect(); + println!("\n$ cargo {}", cmds); + match Command::new("cargo") + .stderr(Stdio::inherit()) + .stdout(Stdio::inherit()) + .args(&cmds_vec) + .output() { + Ok(o) => println!("-> {}", o.status), + Err(e) => println!("Failed to execute 'cargo {}': {}", cmds, e) + }; +} diff --git a/src/compile.rs b/src/compile.rs index be3476f..a2f6c84 100644 --- a/src/compile.rs +++ b/src/compile.rs @@ -11,13 +11,16 @@ macro_rules! run_if_set( fn compile(t: Arc, c: Arc) { let Config { - build, doc, test, bench + build, doc, test, bench, ref raw } = *c; debug!("Starting a compile"); run_if_set!(build); run_if_set!(doc); run_if_set!(test); run_if_set!(bench); + if raw.len() > 0 { + raw.iter().map(|v| cargo::run_raw(v)).last(); + } timelock::update(&t); debug!("Compile done"); } diff --git a/src/main.rs b/src/main.rs index 096e230..69b9aed 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,6 +21,7 @@ mod timelock; static USAGE: &'static str = " Usage: cargo-watch [watch] [options] cargo watch [options] + cargo-watch [watch] raw ... Options: -h, --help Display this message @@ -29,6 +30,9 @@ Options: -t, --test Run `cargo test` when a file is modified -n, --bench Run `cargo bench` when a file is modified +`cargo watch raw` takes one or more quoted arguments to pass to cargo. For example, +`cargo watch raw \"test ex_ --release\"` will run `cargo test ex_ --release` + Default options are `build` and `test` "; @@ -38,14 +42,16 @@ struct Args { flag_doc: bool, flag_test: bool, flag_bench: bool, + arg_raw: Vec, } -#[derive(Clone,Copy)] +#[derive(Clone)] pub struct Config { build: bool, doc: bool, test: bool, - bench: bool + bench: bool, + raw: Vec, } impl Config { @@ -56,10 +62,11 @@ impl Config { flag_doc: doc, flag_test: mut test, flag_bench: bench, + arg_raw: raw, } = Docopt::new(USAGE).and_then(|d| d.decode()).unwrap_or_else(|e| e.exit()); if !build && !doc && - !test && !bench { + !test && !bench && raw.len() == 0 { // Default to build & doc build = true; test = true; @@ -69,7 +76,8 @@ impl Config { build: build, doc: doc, test: test, - bench: bench + bench: bench, + raw: raw, } } }