Skip to content

Commit

Permalink
feat(raw): #11 support running any cargo command
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
compressed committed Aug 18, 2015
1 parent c5db835 commit ac029a5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
14 changes: 14 additions & 0 deletions src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
};
}
5 changes: 4 additions & 1 deletion src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ macro_rules! run_if_set(

fn compile(t: Arc<AtomicIsize>, c: Arc<Config>) {
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");
}
Expand Down
16 changes: 12 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ mod timelock;
static USAGE: &'static str = "
Usage: cargo-watch [watch] [options]
cargo watch [options]
cargo-watch [watch] raw <raw>...
Options:
-h, --help Display this message
Expand All @@ -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`
";

Expand All @@ -38,14 +42,16 @@ struct Args {
flag_doc: bool,
flag_test: bool,
flag_bench: bool,
arg_raw: Vec<String>,
}

#[derive(Clone,Copy)]
#[derive(Clone)]
pub struct Config {
build: bool,
doc: bool,
test: bool,
bench: bool
bench: bool,
raw: Vec<String>,
}

impl Config {
Expand All @@ -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;
Expand All @@ -69,7 +76,8 @@ impl Config {
build: build,
doc: doc,
test: test,
bench: bench
bench: bench,
raw: raw,
}
}
}
Expand Down

0 comments on commit ac029a5

Please sign in to comment.