aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--xtask/src/lib.rs100
1 files changed, 67 insertions, 33 deletions
diff --git a/xtask/src/lib.rs b/xtask/src/lib.rs
index ec824a518..bf4d85dcb 100644
--- a/xtask/src/lib.rs
+++ b/xtask/src/lib.rs
@@ -10,7 +10,6 @@ pub mod pre_commit;
10pub mod codegen; 10pub mod codegen;
11mod ast_src; 11mod ast_src;
12 12
13use anyhow::Context;
14use std::{ 13use std::{
15 env, 14 env,
16 io::Write, 15 io::Write,
@@ -24,9 +23,9 @@ use crate::{
24 not_bash::{date_iso, fs2, pushd, rm_rf, run}, 23 not_bash::{date_iso, fs2, pushd, rm_rf, run},
25}; 24};
26 25
27pub use anyhow::Result; 26pub use anyhow::{bail, Context as _, Result};
28 27
29const TOOLCHAIN: &str = "stable"; 28const RUSTFMT_TOOLCHAIN: &str = "stable";
30 29
31pub fn project_root() -> PathBuf { 30pub fn project_root() -> PathBuf {
32 Path::new( 31 Path::new(
@@ -57,15 +56,25 @@ pub fn run_rustfmt(mode: Mode) -> Result<()> {
57 let _dir = pushd(project_root()); 56 let _dir = pushd(project_root());
58 ensure_rustfmt()?; 57 ensure_rustfmt()?;
59 58
60 let check = if mode == Mode::Verify { "--check" } else { "" }; 59 if Command::new("cargo")
61 run!("rustup run {} -- cargo fmt -- {}", TOOLCHAIN, check)?; 60 .env("RUSTUP_TOOLCHAIN", RUSTFMT_TOOLCHAIN)
62 Ok(()) 61 .args(&["fmt", "--"])
62 .args(if mode == Mode::Verify { &["--check"][..] } else { &[] })
63 .stderr(Stdio::inherit())
64 .status()?
65 .success()
66 {
67 Ok(())
68 } else {
69 bail!("Rustfmt failed");
70 }
63} 71}
64 72
65fn reformat(text: impl std::fmt::Display) -> Result<String> { 73fn reformat(text: impl std::fmt::Display) -> Result<String> {
66 ensure_rustfmt()?; 74 ensure_rustfmt()?;
67 let mut rustfmt = Command::new("rustup") 75 let mut rustfmt = Command::new("rustfmt")
68 .args(&["run", TOOLCHAIN, "--", "rustfmt", "--config-path"]) 76 .env("RUSTUP_TOOLCHAIN", RUSTFMT_TOOLCHAIN)
77 .args(&["--config-path"])
69 .arg(project_root().join("rustfmt.toml")) 78 .arg(project_root().join("rustfmt.toml"))
70 .args(&["--config", "fn_single_line=true"]) 79 .args(&["--config", "fn_single_line=true"])
71 .stdin(Stdio::piped()) 80 .stdin(Stdio::piped())
@@ -79,29 +88,42 @@ fn reformat(text: impl std::fmt::Display) -> Result<String> {
79} 88}
80 89
81fn ensure_rustfmt() -> Result<()> { 90fn ensure_rustfmt() -> Result<()> {
82 match Command::new("rustup") 91 match Command::new("rustfmt")
83 .args(&["run", TOOLCHAIN, "--", "cargo", "fmt", "--version"]) 92 .args(&["--version"])
93 .env("RUSTUP_TOOLCHAIN", RUSTFMT_TOOLCHAIN)
94 .stdout(Stdio::piped())
84 .stderr(Stdio::null()) 95 .stderr(Stdio::null())
85 .stdout(Stdio::null()) 96 .spawn()
86 .status() 97 .and_then(|child| child.wait_with_output())
87 { 98 {
88 Ok(status) if status.success() => return Ok(()), 99 Ok(output)
89 _ => (), 100 if output.status.success()
90 }; 101 && std::str::from_utf8(&output.stdout)?.contains(RUSTFMT_TOOLCHAIN) =>
91 run!("rustup toolchain install {}", TOOLCHAIN)?; 102 {
92 run!("rustup component add rustfmt --toolchain {}", TOOLCHAIN)?; 103 Ok(())
93 Ok(()) 104 }
105 _ => {
106 bail!(
107 "Failed to run rustfmt from toolchain '{0}'. \
108 Please run `rustup component add rustfmt --toolchain {0}` to install it.",
109 RUSTFMT_TOOLCHAIN,
110 );
111 }
112 }
94} 113}
95 114
96pub fn run_clippy() -> Result<()> { 115pub fn run_clippy() -> Result<()> {
97 match Command::new("rustup") 116 match Command::new("cargo")
98 .args(&["run", TOOLCHAIN, "--", "cargo", "clippy", "--version"]) 117 .args(&["clippy", "--version"])
99 .stderr(Stdio::null()) 118 .stderr(Stdio::null())
100 .stdout(Stdio::null()) 119 .stdout(Stdio::null())
101 .status() 120 .status()
102 { 121 {
103 Ok(status) if status.success() => (), 122 Ok(status) if status.success() => (),
104 _ => install_clippy().context("install clippy")?, 123 _ => bail!(
124 "Failed run cargo clippy. \
125 Please run `rustup component add clippy` to install it.",
126 ),
105 }; 127 };
106 128
107 let allowed_lints = [ 129 let allowed_lints = [
@@ -110,17 +132,7 @@ pub fn run_clippy() -> Result<()> {
110 "clippy::nonminimal_bool", 132 "clippy::nonminimal_bool",
111 "clippy::redundant_pattern_matching", 133 "clippy::redundant_pattern_matching",
112 ]; 134 ];
113 run!( 135 run!("cargo clippy --all-features --all-targets -- -A {}", allowed_lints.join(" -A "))?;
114 "rustup run {} -- cargo clippy --all-features --all-targets -- -A {}",
115 TOOLCHAIN,
116 allowed_lints.join(" -A ")
117 )?;
118 Ok(())
119}
120
121fn install_clippy() -> Result<()> {
122 run!("rustup toolchain install {}", TOOLCHAIN)?;
123 run!("rustup component add clippy --toolchain {}", TOOLCHAIN)?;
124 Ok(()) 136 Ok(())
125} 137}
126 138
@@ -130,7 +142,29 @@ pub fn run_fuzzer() -> Result<()> {
130 run!("cargo install cargo-fuzz")?; 142 run!("cargo install cargo-fuzz")?;
131 }; 143 };
132 144
133 run!("rustup run nightly -- cargo fuzz run parser")?; 145 // Expecting nightly rustc
146 match Command::new("rustc")
147 .args(&["--version"])
148 .env("RUSTUP_TOOLCHAIN", "nightly")
149 .stdout(Stdio::piped())
150 .stderr(Stdio::null())
151 .spawn()
152 .and_then(|child| child.wait_with_output())
153 {
154 Ok(output)
155 if output.status.success()
156 && std::str::from_utf8(&output.stdout)?.contains("nightly") => {}
157 _ => bail!("fuzz tests require nightly rustc"),
158 }
159
160 let status = Command::new("cargo")
161 .env("RUSTUP_TOOLCHAIN", "nightly")
162 .args(&["fuzz", "run", "parser"])
163 .stderr(Stdio::inherit())
164 .status()?;
165 if !status.success() {
166 bail!("{}", status);
167 }
134 Ok(()) 168 Ok(())
135} 169}
136 170