diff options
Diffstat (limited to 'xtask/src/cmd.rs')
-rw-r--r-- | xtask/src/cmd.rs | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/xtask/src/cmd.rs b/xtask/src/cmd.rs new file mode 100644 index 000000000..2027f4893 --- /dev/null +++ b/xtask/src/cmd.rs | |||
@@ -0,0 +1,53 @@ | |||
1 | use std::process::{Command, Output, Stdio}; | ||
2 | |||
3 | use anyhow::{Context, Result}; | ||
4 | |||
5 | use crate::project_root; | ||
6 | |||
7 | pub struct Cmd<'a> { | ||
8 | pub unix: &'a str, | ||
9 | pub windows: &'a str, | ||
10 | pub work_dir: &'a str, | ||
11 | } | ||
12 | |||
13 | impl Cmd<'_> { | ||
14 | pub fn run(self) -> Result<()> { | ||
15 | if cfg!(windows) { | ||
16 | run(self.windows, self.work_dir) | ||
17 | } else { | ||
18 | run(self.unix, self.work_dir) | ||
19 | } | ||
20 | } | ||
21 | pub fn run_with_output(self) -> Result<Output> { | ||
22 | if cfg!(windows) { | ||
23 | run_with_output(self.windows, self.work_dir) | ||
24 | } else { | ||
25 | run_with_output(self.unix, self.work_dir) | ||
26 | } | ||
27 | } | ||
28 | } | ||
29 | |||
30 | pub fn run(cmdline: &str, dir: &str) -> Result<()> { | ||
31 | do_run(cmdline, dir, &mut |c| { | ||
32 | c.stdout(Stdio::inherit()); | ||
33 | }) | ||
34 | .map(|_| ()) | ||
35 | } | ||
36 | |||
37 | pub fn run_with_output(cmdline: &str, dir: &str) -> Result<Output> { | ||
38 | do_run(cmdline, dir, &mut |_| {}) | ||
39 | } | ||
40 | |||
41 | fn do_run(cmdline: &str, dir: &str, f: &mut dyn FnMut(&mut Command)) -> Result<Output> { | ||
42 | eprintln!("\nwill run: {}", cmdline); | ||
43 | let proj_dir = project_root().join(dir); | ||
44 | let mut args = cmdline.split_whitespace(); | ||
45 | let exec = args.next().unwrap(); | ||
46 | let mut cmd = Command::new(exec); | ||
47 | f(cmd.args(args).current_dir(proj_dir).stderr(Stdio::inherit())); | ||
48 | let output = cmd.output().with_context(|| format!("running `{}`", cmdline))?; | ||
49 | if !output.status.success() { | ||
50 | anyhow::bail!("`{}` exited with {}", cmdline, output.status); | ||
51 | } | ||
52 | Ok(output) | ||
53 | } | ||