Files
rust/xtask/src/lib.rs

119 lines
3.0 KiB
Rust
Raw Normal View History

2020-02-27 11:07:26 +01:00
//! Support library for `cargo xtask` command.
//!
//! See https://github.com/matklad/cargo-xtask/
pub mod codegen;
mod ast_src;
2020-01-07 14:42:56 +01:00
pub mod install;
2020-06-08 13:58:54 +02:00
pub mod release;
2020-03-04 18:36:16 +01:00
pub mod dist;
2020-01-07 14:42:56 +01:00
pub mod pre_commit;
2020-07-24 16:28:07 +02:00
pub mod metrics;
pub mod pre_cache;
2019-08-18 21:33:31 +03:00
use std::{
2020-02-14 19:03:45 +01:00
env,
2018-10-16 00:54:27 +06:00
path::{Path, PathBuf},
};
2020-03-21 08:04:28 -07:00
use walkdir::{DirEntry, WalkDir};
2020-10-16 19:46:03 +02:00
use xshell::{cmd, pushd, pushenv};
2020-10-16 19:46:03 +02:00
use crate::codegen::Mode;
2019-08-18 21:34:55 +03:00
pub use anyhow::{bail, Context as _, Result};
2020-01-07 15:33:09 +01:00
2018-10-16 00:54:27 +06:00
pub fn project_root() -> PathBuf {
2019-11-01 22:20:44 +02:00
Path::new(
&env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| env!("CARGO_MANIFEST_DIR").to_owned()),
)
.ancestors()
.nth(1)
.unwrap()
.to_path_buf()
2018-10-16 00:54:27 +06:00
}
2020-03-21 08:04:28 -07:00
pub fn rust_files(path: &Path) -> impl Iterator<Item = PathBuf> {
let iter = WalkDir::new(path);
return iter
.into_iter()
.filter_entry(|e| !is_hidden(e))
.map(|e| e.unwrap())
.filter(|e| !e.file_type().is_dir())
.map(|e| e.into_path())
.filter(|path| path.extension().map(|it| it == "rs").unwrap_or(false));
fn is_hidden(entry: &DirEntry) -> bool {
entry.file_name().to_str().map(|s| s.starts_with('.')).unwrap_or(false)
}
}
pub fn run_rustfmt(mode: Mode) -> Result<()> {
2020-10-16 19:46:03 +02:00
let _dir = pushd(project_root())?;
let _e = pushenv("RUSTUP_TOOLCHAIN", "stable");
ensure_rustfmt()?;
2020-10-16 19:46:03 +02:00
let check = match mode {
Mode::Overwrite => &[][..],
Mode::Verify => &["--", "--check"],
};
cmd!("cargo fmt {check...}").run()?;
Ok(())
}
2018-10-31 23:57:38 +03:00
fn ensure_rustfmt() -> Result<()> {
2020-10-16 19:46:03 +02:00
let out = cmd!("rustfmt --version").read()?;
if !out.contains("stable") {
bail!(
"Failed to run rustfmt from toolchain 'stable'. \
Please run `rustup component add rustfmt --toolchain stable` to install it.",
)
}
Ok(())
2018-10-31 23:57:38 +03:00
}
2018-12-09 10:29:13 +00:00
2019-06-03 09:43:06 -04:00
pub fn run_clippy() -> Result<()> {
2020-10-16 19:46:03 +02:00
if cmd!("cargo clippy --version").read().is_err() {
bail!(
"Failed run cargo clippy. \
Please run `rustup component add clippy` to install it.",
)
}
2019-06-03 09:43:06 -04:00
2020-10-16 19:46:03 +02:00
let allowed_lints = "
-A clippy::collapsible_if
-A clippy::needless_pass_by_value
-A clippy::nonminimal_bool
-A clippy::redundant_pattern_matching
"
.split_ascii_whitespace();
cmd!("cargo clippy --all-features --all-targets -- {allowed_lints...}").run()?;
2020-02-14 15:59:19 +01:00
Ok(())
2019-06-03 09:43:06 -04:00
}
2018-12-31 13:14:06 +00:00
pub fn run_fuzzer() -> Result<()> {
2020-10-16 19:46:03 +02:00
let _d = pushd("./crates/syntax")?;
let _e = pushenv("RUSTUP_TOOLCHAIN", "nightly");
2020-10-16 19:46:03 +02:00
if cmd!("cargo fuzz --help").read().is_err() {
cmd!("cargo install cargo-fuzz").run()?;
2018-12-31 13:14:06 +00:00
};
// Expecting nightly rustc
2020-10-16 19:46:03 +02:00
let out = cmd!("rustc --version").read()?;
if !out.contains("nightly") {
bail!("fuzz tests require nightly rustc")
}
2020-10-16 19:46:03 +02:00
cmd!("cargo fuzz run parser").run()?;
2020-02-14 15:59:19 +01:00
Ok(())
2018-12-31 13:14:06 +00:00
}
2020-10-16 19:46:03 +02:00
fn date_iso() -> Result<String> {
let res = cmd!("date --iso --utc").read()?;
Ok(res)
}
2020-03-02 14:54:55 +01:00
fn is_release_tag(tag: &str) -> bool {
tag.len() == "2020-02-24".len() && tag.starts_with(|c: char| c.is_ascii_digit())
}