From 5f0008040e9a168e5eaec0bbd754a6f14d12896a Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Apr 2020 15:14:55 +0200 Subject: Allow to set env vars and pipe stdin via not_bash --- xtask/src/lib.rs | 111 +++++++++++++++---------------------------------------- 1 file changed, 30 insertions(+), 81 deletions(-) (limited to 'xtask/src/lib.rs') diff --git a/xtask/src/lib.rs b/xtask/src/lib.rs index bf4d85dcb..2b7a461e5 100644 --- a/xtask/src/lib.rs +++ b/xtask/src/lib.rs @@ -12,21 +12,18 @@ mod ast_src; use std::{ env, - io::Write, path::{Path, PathBuf}, - process::{Command, Stdio}, }; + use walkdir::{DirEntry, WalkDir}; use crate::{ codegen::Mode, - not_bash::{date_iso, fs2, pushd, rm_rf, run}, + not_bash::{date_iso, fs2, pushd, pushenv, rm_rf, run}, }; pub use anyhow::{bail, Context as _, Result}; -const RUSTFMT_TOOLCHAIN: &str = "stable"; - pub fn project_root() -> PathBuf { Path::new( &env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| env!("CARGO_MANIFEST_DIR").to_owned()), @@ -54,77 +51,44 @@ pub fn rust_files(path: &Path) -> impl Iterator { pub fn run_rustfmt(mode: Mode) -> Result<()> { let _dir = pushd(project_root()); + let _e = pushenv("RUSTUP_TOOLCHAIN", "stable"); ensure_rustfmt()?; - - if Command::new("cargo") - .env("RUSTUP_TOOLCHAIN", RUSTFMT_TOOLCHAIN) - .args(&["fmt", "--"]) - .args(if mode == Mode::Verify { &["--check"][..] } else { &[] }) - .stderr(Stdio::inherit()) - .status()? - .success() - { - Ok(()) - } else { - bail!("Rustfmt failed"); - } + match mode { + Mode::Overwrite => run!("cargo fmt"), + Mode::Verify => run!("cargo fmt -- --check"), + }?; + Ok(()) } fn reformat(text: impl std::fmt::Display) -> Result { + let _e = pushenv("RUSTUP_TOOLCHAIN", "stable"); ensure_rustfmt()?; - let mut rustfmt = Command::new("rustfmt") - .env("RUSTUP_TOOLCHAIN", RUSTFMT_TOOLCHAIN) - .args(&["--config-path"]) - .arg(project_root().join("rustfmt.toml")) - .args(&["--config", "fn_single_line=true"]) - .stdin(Stdio::piped()) - .stdout(Stdio::piped()) - .spawn()?; - write!(rustfmt.stdin.take().unwrap(), "{}", text)?; - let output = rustfmt.wait_with_output()?; - let stdout = String::from_utf8(output.stdout)?; + let stdout = run!( + "rustfmt --config-path {} --config fn_single_line=true", project_root().join("rustfmt.toml").display(); + Result<()> { - match Command::new("rustfmt") - .args(&["--version"]) - .env("RUSTUP_TOOLCHAIN", RUSTFMT_TOOLCHAIN) - .stdout(Stdio::piped()) - .stderr(Stdio::null()) - .spawn() - .and_then(|child| child.wait_with_output()) - { - Ok(output) - if output.status.success() - && std::str::from_utf8(&output.stdout)?.contains(RUSTFMT_TOOLCHAIN) => - { - Ok(()) - } - _ => { - bail!( - "Failed to run rustfmt from toolchain '{0}'. \ - Please run `rustup component add rustfmt --toolchain {0}` to install it.", - RUSTFMT_TOOLCHAIN, - ); - } + let out = run!("rustfmt --version")?; + if !out.contains("stable") { + bail!( + "Failed to run rustfmt from toolchain 'stable'. \ + Please run `rustup component add rustfmt --toolchain stable` to install it.", + ) } + Ok(()) } pub fn run_clippy() -> Result<()> { - match Command::new("cargo") - .args(&["clippy", "--version"]) - .stderr(Stdio::null()) - .stdout(Stdio::null()) - .status() - { - Ok(status) if status.success() => (), - _ => bail!( + if run!("cargo clippy --version").is_err() { + bail!( "Failed run cargo clippy. \ Please run `rustup component add clippy` to install it.", - ), - }; + ) + } let allowed_lints = [ "clippy::collapsible_if", @@ -138,33 +102,18 @@ pub fn run_clippy() -> Result<()> { pub fn run_fuzzer() -> Result<()> { let _d = pushd("./crates/ra_syntax"); + let _e = pushenv("RUSTUP_TOOLCHAIN", "nightly"); if run!("cargo fuzz --help").is_err() { run!("cargo install cargo-fuzz")?; }; // Expecting nightly rustc - match Command::new("rustc") - .args(&["--version"]) - .env("RUSTUP_TOOLCHAIN", "nightly") - .stdout(Stdio::piped()) - .stderr(Stdio::null()) - .spawn() - .and_then(|child| child.wait_with_output()) - { - Ok(output) - if output.status.success() - && std::str::from_utf8(&output.stdout)?.contains("nightly") => {} - _ => bail!("fuzz tests require nightly rustc"), + let out = run!("rustc --version")?; + if !out.contains("nightly") { + bail!("fuzz tests require nightly rustc") } - let status = Command::new("cargo") - .env("RUSTUP_TOOLCHAIN", "nightly") - .args(&["fuzz", "run", "parser"]) - .stderr(Stdio::inherit()) - .status()?; - if !status.success() { - bail!("{}", status); - } + run!("cargo fuzz run parser")?; Ok(()) } -- cgit v1.2.3