diff options
Diffstat (limited to 'xtask/src/main.rs')
-rw-r--r-- | xtask/src/main.rs | 134 |
1 files changed, 127 insertions, 7 deletions
diff --git a/xtask/src/main.rs b/xtask/src/main.rs index cbb9b315e..48c0d9920 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs | |||
@@ -7,22 +7,35 @@ | |||
7 | //! | 7 | //! |
8 | //! This binary is integrated into the `cargo` command line by using an alias in | 8 | //! This binary is integrated into the `cargo` command line by using an alias in |
9 | //! `.cargo/config`. | 9 | //! `.cargo/config`. |
10 | mod codegen; | ||
11 | mod ast_src; | ||
12 | #[cfg(test)] | ||
13 | mod tidy; | ||
10 | 14 | ||
11 | use std::env; | 15 | mod install; |
16 | mod release; | ||
17 | mod dist; | ||
18 | mod pre_commit; | ||
19 | mod metrics; | ||
20 | mod pre_cache; | ||
12 | 21 | ||
13 | use anyhow::bail; | 22 | use anyhow::{bail, Result}; |
14 | use codegen::CodegenCmd; | 23 | use codegen::CodegenCmd; |
15 | use pico_args::Arguments; | 24 | use pico_args::Arguments; |
16 | use xshell::{cmd, cp, pushd}; | 25 | use std::{ |
17 | use xtask::{ | 26 | env, |
18 | codegen::{self, Mode}, | 27 | path::{Path, PathBuf}, |
28 | }; | ||
29 | use walkdir::{DirEntry, WalkDir}; | ||
30 | use xshell::{cmd, cp, pushd, pushenv}; | ||
31 | |||
32 | use crate::{ | ||
33 | codegen::Mode, | ||
19 | dist::DistCmd, | 34 | dist::DistCmd, |
20 | install::{InstallCmd, Malloc, ServerOpt}, | 35 | install::{InstallCmd, Malloc, ServerOpt}, |
21 | metrics::MetricsCmd, | 36 | metrics::MetricsCmd, |
22 | pre_cache::PreCacheCmd, | 37 | pre_cache::PreCacheCmd, |
23 | pre_commit, project_root, | ||
24 | release::{PromoteCmd, ReleaseCmd}, | 38 | release::{PromoteCmd, ReleaseCmd}, |
25 | run_clippy, run_fuzzer, run_rustfmt, Result, | ||
26 | }; | 39 | }; |
27 | 40 | ||
28 | fn main() -> Result<()> { | 41 | fn main() -> Result<()> { |
@@ -172,3 +185,110 @@ fn finish_args(args: Arguments) -> Result<()> { | |||
172 | } | 185 | } |
173 | Ok(()) | 186 | Ok(()) |
174 | } | 187 | } |
188 | |||
189 | fn project_root() -> PathBuf { | ||
190 | Path::new( | ||
191 | &env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| env!("CARGO_MANIFEST_DIR").to_owned()), | ||
192 | ) | ||
193 | .ancestors() | ||
194 | .nth(1) | ||
195 | .unwrap() | ||
196 | .to_path_buf() | ||
197 | } | ||
198 | |||
199 | fn rust_files() -> impl Iterator<Item = PathBuf> { | ||
200 | rust_files_in(&project_root().join("crates")) | ||
201 | } | ||
202 | |||
203 | #[cfg(test)] | ||
204 | fn cargo_files() -> impl Iterator<Item = PathBuf> { | ||
205 | files_in(&project_root(), "toml") | ||
206 | .filter(|path| path.file_name().map(|it| it == "Cargo.toml").unwrap_or(false)) | ||
207 | } | ||
208 | |||
209 | fn rust_files_in(path: &Path) -> impl Iterator<Item = PathBuf> { | ||
210 | files_in(path, "rs") | ||
211 | } | ||
212 | |||
213 | fn run_rustfmt(mode: Mode) -> Result<()> { | ||
214 | let _dir = pushd(project_root())?; | ||
215 | let _e = pushenv("RUSTUP_TOOLCHAIN", "stable"); | ||
216 | ensure_rustfmt()?; | ||
217 | let check = match mode { | ||
218 | Mode::Overwrite => &[][..], | ||
219 | Mode::Verify => &["--", "--check"], | ||
220 | }; | ||
221 | cmd!("cargo fmt {check...}").run()?; | ||
222 | Ok(()) | ||
223 | } | ||
224 | |||
225 | fn ensure_rustfmt() -> Result<()> { | ||
226 | let out = cmd!("rustfmt --version").read()?; | ||
227 | if !out.contains("stable") { | ||
228 | bail!( | ||
229 | "Failed to run rustfmt from toolchain 'stable'. \ | ||
230 | Please run `rustup component add rustfmt --toolchain stable` to install it.", | ||
231 | ) | ||
232 | } | ||
233 | Ok(()) | ||
234 | } | ||
235 | |||
236 | fn run_clippy() -> Result<()> { | ||
237 | if cmd!("cargo clippy --version").read().is_err() { | ||
238 | bail!( | ||
239 | "Failed run cargo clippy. \ | ||
240 | Please run `rustup component add clippy` to install it.", | ||
241 | ) | ||
242 | } | ||
243 | |||
244 | let allowed_lints = " | ||
245 | -A clippy::collapsible_if | ||
246 | -A clippy::needless_pass_by_value | ||
247 | -A clippy::nonminimal_bool | ||
248 | -A clippy::redundant_pattern_matching | ||
249 | " | ||
250 | .split_ascii_whitespace(); | ||
251 | cmd!("cargo clippy --all-features --all-targets -- {allowed_lints...}").run()?; | ||
252 | Ok(()) | ||
253 | } | ||
254 | |||
255 | fn run_fuzzer() -> Result<()> { | ||
256 | let _d = pushd("./crates/syntax")?; | ||
257 | let _e = pushenv("RUSTUP_TOOLCHAIN", "nightly"); | ||
258 | if cmd!("cargo fuzz --help").read().is_err() { | ||
259 | cmd!("cargo install cargo-fuzz").run()?; | ||
260 | }; | ||
261 | |||
262 | // Expecting nightly rustc | ||
263 | let out = cmd!("rustc --version").read()?; | ||
264 | if !out.contains("nightly") { | ||
265 | bail!("fuzz tests require nightly rustc") | ||
266 | } | ||
267 | |||
268 | cmd!("cargo fuzz run parser").run()?; | ||
269 | Ok(()) | ||
270 | } | ||
271 | |||
272 | fn date_iso() -> Result<String> { | ||
273 | let res = cmd!("date --iso --utc").read()?; | ||
274 | Ok(res) | ||
275 | } | ||
276 | |||
277 | fn is_release_tag(tag: &str) -> bool { | ||
278 | tag.len() == "2020-02-24".len() && tag.starts_with(|c: char| c.is_ascii_digit()) | ||
279 | } | ||
280 | |||
281 | fn files_in(path: &Path, ext: &'static str) -> impl Iterator<Item = PathBuf> { | ||
282 | let iter = WalkDir::new(path); | ||
283 | return iter | ||
284 | .into_iter() | ||
285 | .filter_entry(|e| !is_hidden(e)) | ||
286 | .map(|e| e.unwrap()) | ||
287 | .filter(|e| !e.file_type().is_dir()) | ||
288 | .map(|e| e.into_path()) | ||
289 | .filter(move |path| path.extension().map(|it| it == ext).unwrap_or(false)); | ||
290 | |||
291 | fn is_hidden(entry: &DirEntry) -> bool { | ||
292 | entry.file_name().to_str().map(|s| s.starts_with('.')).unwrap_or(false) | ||
293 | } | ||
294 | } | ||