From 1b6acc391aee6a2c2868f537786f1dfa4ff774a2 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 10 Feb 2020 15:16:07 +0100 Subject: More convenient run_with_output --- xtask/src/cmd.rs | 8 +++++--- xtask/src/install.rs | 25 ++++++++++--------------- xtask/src/pre_commit.rs | 2 +- 3 files changed, 16 insertions(+), 19 deletions(-) (limited to 'xtask/src') diff --git a/xtask/src/cmd.rs b/xtask/src/cmd.rs index 2027f4893..8e08a929b 100644 --- a/xtask/src/cmd.rs +++ b/xtask/src/cmd.rs @@ -18,7 +18,7 @@ impl Cmd<'_> { run(self.unix, self.work_dir) } } - pub fn run_with_output(self) -> Result { + pub fn run_with_output(self) -> Result { if cfg!(windows) { run_with_output(self.windows, self.work_dir) } else { @@ -34,8 +34,10 @@ pub fn run(cmdline: &str, dir: &str) -> Result<()> { .map(|_| ()) } -pub fn run_with_output(cmdline: &str, dir: &str) -> Result { - do_run(cmdline, dir, &mut |_| {}) +pub fn run_with_output(cmdline: &str, dir: &str) -> Result { + let output = do_run(cmdline, dir, &mut |_| {})?; + let stdout = String::from_utf8(output.stdout)?; + Ok(stdout) } fn do_run(cmdline: &str, dir: &str, f: &mut dyn FnMut(&mut Command)) -> Result { diff --git a/xtask/src/install.rs b/xtask/src/install.rs index 0e7cda3fa..99e1eddb1 100644 --- a/xtask/src/install.rs +++ b/xtask/src/install.rs @@ -127,15 +127,12 @@ fn install_client(ClientOpt::VsCode: ClientOpt) -> Result<()> { } .run()?; - let installed_extensions = { - let output = Cmd { - unix: &format!(r"{} --list-extensions", code_binary), - windows: &format!(r"cmd.exe /c {}.cmd --list-extensions", code_binary), - work_dir: ".", - } - .run_with_output()?; - String::from_utf8(output.stdout)? - }; + let installed_extensions = Cmd { + unix: &format!(r"{} --list-extensions", code_binary), + windows: &format!(r"cmd.exe /c {}.cmd --list-extensions", code_binary), + work_dir: ".", + } + .run_with_output()?; if !installed_extensions.contains("rust-analyzer") { anyhow::bail!( @@ -161,12 +158,10 @@ fn install_client(ClientOpt::VsCode: ClientOpt) -> Result<()> { fn install_server(opts: ServerOpt) -> Result<()> { let mut old_rust = false; - if let Ok(output) = run_with_output("cargo --version", ".") { - if let Ok(stdout) = String::from_utf8(output.stdout) { - println!("{}", stdout); - if !check_version(&stdout, REQUIRED_RUST_VERSION) { - old_rust = true; - } + if let Ok(stdout) = run_with_output("cargo --version", ".") { + println!("{}", stdout); + if !check_version(&stdout, REQUIRED_RUST_VERSION) { + old_rust = true; } } diff --git a/xtask/src/pre_commit.rs b/xtask/src/pre_commit.rs index 88e868ca6..1533f64dc 100644 --- a/xtask/src/pre_commit.rs +++ b/xtask/src/pre_commit.rs @@ -14,7 +14,7 @@ pub fn run_hook() -> Result<()> { let diff = run_with_output("git diff --diff-filter=MAR --name-only --cached", ".")?; let root = project_root(); - for line in String::from_utf8(diff.stdout)?.lines() { + for line in diff.lines() { run(&format!("git update-index --add {}", root.join(line).to_string_lossy()), ".")?; } -- cgit v1.2.3