aboutsummaryrefslogtreecommitdiff
path: root/xtask/src/main.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2021-03-01 17:16:23 +0000
committerAleksey Kladov <[email protected]>2021-03-01 17:26:37 +0000
commitd9dcfd81c5d4325379ac88c4250b9c77ecbd75e8 (patch)
tree0795d7277733920361a260db1a65306f1a47e1ac /xtask/src/main.rs
parentc17f2bf2a27798858ef2e3012ca28295aed46efa (diff)
Simplify xtask
lib/bin/test separation isn't really needed.
Diffstat (limited to 'xtask/src/main.rs')
-rw-r--r--xtask/src/main.rs134
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`.
10mod codegen;
11mod ast_src;
12#[cfg(test)]
13mod tidy;
10 14
11use std::env; 15mod install;
16mod release;
17mod dist;
18mod pre_commit;
19mod metrics;
20mod pre_cache;
12 21
13use anyhow::bail; 22use anyhow::{bail, Result};
14use codegen::CodegenCmd; 23use codegen::CodegenCmd;
15use pico_args::Arguments; 24use pico_args::Arguments;
16use xshell::{cmd, cp, pushd}; 25use std::{
17use xtask::{ 26 env,
18 codegen::{self, Mode}, 27 path::{Path, PathBuf},
28};
29use walkdir::{DirEntry, WalkDir};
30use xshell::{cmd, cp, pushd, pushenv};
31
32use 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
28fn main() -> Result<()> { 41fn main() -> Result<()> {
@@ -172,3 +185,110 @@ fn finish_args(args: Arguments) -> Result<()> {
172 } 185 }
173 Ok(()) 186 Ok(())
174} 187}
188
189fn 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
199fn rust_files() -> impl Iterator<Item = PathBuf> {
200 rust_files_in(&project_root().join("crates"))
201}
202
203#[cfg(test)]
204fn 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
209fn rust_files_in(path: &Path) -> impl Iterator<Item = PathBuf> {
210 files_in(path, "rs")
211}
212
213fn 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
225fn 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
236fn 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
255fn 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
272fn date_iso() -> Result<String> {
273 let res = cmd!("date --iso --utc").read()?;
274 Ok(res)
275}
276
277fn is_release_tag(tag: &str) -> bool {
278 tag.len() == "2020-02-24".len() && tag.starts_with(|c: char| c.is_ascii_digit())
279}
280
281fn 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}