mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 06:11:35 +00:00
Allow to set env vars and pipe stdin via not_bash
This commit is contained in:
parent
36775ef0d0
commit
5f0008040e
2 changed files with 79 additions and 92 deletions
111
xtask/src/lib.rs
111
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<Item = PathBuf> {
|
|||
|
||||
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<String> {
|
||||
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();
|
||||
<text.to_string().as_bytes()
|
||||
)?;
|
||||
let preamble = "Generated file, do not edit by hand, see `xtask/src/codegen`";
|
||||
Ok(format!("//! {}\n\n{}", preamble, stdout))
|
||||
Ok(format!("//! {}\n\n{}\n", preamble, stdout))
|
||||
}
|
||||
|
||||
fn ensure_rustfmt() -> 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(())
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue