From 64f0510d18bc62c6bbe6f3c7d4b0d43665ccc1eb Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 7 Jan 2020 15:33:09 +0100 Subject: Move cmd to a separate dir --- xtask/src/cmd.rs | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 xtask/src/cmd.rs (limited to 'xtask/src/cmd.rs') 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 @@ +use std::process::{Command, Output, Stdio}; + +use anyhow::{Context, Result}; + +use crate::project_root; + +pub struct Cmd<'a> { + pub unix: &'a str, + pub windows: &'a str, + pub work_dir: &'a str, +} + +impl Cmd<'_> { + pub fn run(self) -> Result<()> { + if cfg!(windows) { + run(self.windows, self.work_dir) + } else { + run(self.unix, self.work_dir) + } + } + pub fn run_with_output(self) -> Result { + if cfg!(windows) { + run_with_output(self.windows, self.work_dir) + } else { + run_with_output(self.unix, self.work_dir) + } + } +} + +pub fn run(cmdline: &str, dir: &str) -> Result<()> { + do_run(cmdline, dir, &mut |c| { + c.stdout(Stdio::inherit()); + }) + .map(|_| ()) +} + +pub fn run_with_output(cmdline: &str, dir: &str) -> Result { + do_run(cmdline, dir, &mut |_| {}) +} + +fn do_run(cmdline: &str, dir: &str, f: &mut dyn FnMut(&mut Command)) -> Result { + eprintln!("\nwill run: {}", cmdline); + let proj_dir = project_root().join(dir); + let mut args = cmdline.split_whitespace(); + let exec = args.next().unwrap(); + let mut cmd = Command::new(exec); + f(cmd.args(args).current_dir(proj_dir).stderr(Stdio::inherit())); + let output = cmd.output().with_context(|| format!("running `{}`", cmdline))?; + if !output.status.success() { + anyhow::bail!("`{}` exited with {}", cmdline, output.status); + } + Ok(output) +} -- cgit v1.2.3