rust-analyzer/xtask/src/lib.rs

161 lines
4.5 KiB
Rust
Raw Normal View History

2020-02-27 10:07:26 +00:00
//! Support library for `cargo xtask` command.
//!
//! See https://github.com/matklad/cargo-xtask/
2020-02-14 14:59:19 +00:00
pub mod not_bash;
2020-01-07 13:42:56 +00:00
pub mod install;
2020-06-08 11:58:54 +00:00
pub mod release;
2020-03-04 17:36:16 +00:00
pub mod dist;
2020-01-07 13:42:56 +00:00
pub mod pre_commit;
2020-07-24 14:28:07 +00:00
pub mod metrics;
2020-01-07 14:33:51 +00:00
pub mod codegen;
mod ast_src;
2019-08-18 18:33:31 +00:00
use std::{
2020-02-14 18:03:45 +00:00
env,
2018-10-15 18:54:27 +00:00
path::{Path, PathBuf},
};
2020-03-21 15:04:28 +00:00
use walkdir::{DirEntry, WalkDir};
2020-02-10 14:32:03 +00:00
use crate::{
codegen::Mode,
2020-07-14 15:14:00 +00:00
not_bash::{fs2, pushd, pushenv, rm_rf},
2020-02-10 14:32:03 +00:00
};
2019-08-18 18:34:55 +00:00
pub use anyhow::{bail, Context as _, Result};
2020-01-07 14:33:09 +00:00
2018-10-15 18:54:27 +00:00
pub fn project_root() -> PathBuf {
2019-11-01 20:20:44 +00: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-15 18:54:27 +00:00
}
2020-03-21 15:04:28 +00: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-03-13 11:28:13 +00:00
let _dir = pushd(project_root());
let _e = pushenv("RUSTUP_TOOLCHAIN", "stable");
ensure_rustfmt()?;
match mode {
Mode::Overwrite => run!("cargo fmt"),
Mode::Verify => run!("cargo fmt -- --check"),
}?;
Ok(())
}
2018-10-31 20:57:38 +00:00
fn reformat(text: impl std::fmt::Display) -> Result<String> {
let _e = pushenv("RUSTUP_TOOLCHAIN", "stable");
ensure_rustfmt()?;
let stdout = run!(
"rustfmt --config-path {} --config fn_single_line=true", project_root().join("rustfmt.toml").display();
<text.to_string().as_bytes()
)?;
2020-02-07 21:35:34 +00:00
let preamble = "Generated file, do not edit by hand, see `xtask/src/codegen`";
Ok(format!("//! {}\n\n{}\n", preamble, stdout))
}
fn ensure_rustfmt() -> Result<()> {
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(())
2018-10-31 20:57:38 +00:00
}
2018-12-09 10:29:13 +00:00
2019-06-03 13:43:06 +00:00
pub fn run_clippy() -> Result<()> {
if run!("cargo clippy --version").is_err() {
bail!(
"Failed run cargo clippy. \
Please run `rustup component add clippy` to install it.",
)
}
2019-06-03 13:43:06 +00:00
let allowed_lints = [
"clippy::collapsible_if",
"clippy::needless_pass_by_value",
"clippy::nonminimal_bool",
"clippy::redundant_pattern_matching",
];
run!("cargo clippy --all-features --all-targets -- -A {}", allowed_lints.join(" -A "))?;
2020-02-14 14:59:19 +00:00
Ok(())
2019-06-03 13:43:06 +00:00
}
2018-12-31 13:14:06 +00:00
pub fn run_fuzzer() -> Result<()> {
2020-02-14 14:59:19 +00:00
let _d = pushd("./crates/ra_syntax");
let _e = pushenv("RUSTUP_TOOLCHAIN", "nightly");
2020-02-14 18:13:26 +00:00
if run!("cargo fuzz --help").is_err() {
run!("cargo install cargo-fuzz")?;
2018-12-31 13:14:06 +00:00
};
// Expecting nightly rustc
let out = run!("rustc --version")?;
if !out.contains("nightly") {
bail!("fuzz tests require nightly rustc")
}
run!("cargo fuzz run parser")?;
2020-02-14 14:59:19 +00:00
Ok(())
2018-12-31 13:14:06 +00:00
}
/// Cleans the `./target` dir after the build such that only
/// dependencies are cached on CI.
pub fn run_pre_cache() -> Result<()> {
let slow_tests_cookie = Path::new("./target/.slow_tests_cookie");
if !slow_tests_cookie.exists() {
panic!("slow tests were skipped on CI!")
}
rm_rf(slow_tests_cookie)?;
for entry in Path::new("./target/debug").read_dir()? {
let entry = entry?;
if entry.file_type().map(|it| it.is_file()).ok() == Some(true) {
// Can't delete yourself on windows :-(
if !entry.path().ends_with("xtask.exe") {
rm_rf(&entry.path())?
}
}
}
2020-02-14 18:03:45 +00:00
fs2::remove_file("./target/.rustc_info.json")?;
let to_delete = ["ra_", "heavy_test", "xtask"];
for &dir in ["./target/debug/deps", "target/debug/.fingerprint"].iter() {
for entry in Path::new(dir).read_dir()? {
let entry = entry?;
if to_delete.iter().any(|&it| entry.path().display().to_string().contains(it)) {
// Can't delete yourself on windows :-(
if !entry.path().ends_with("xtask.exe") {
rm_rf(&entry.path())?
}
}
}
}
Ok(())
}
2020-03-02 13:54:55 +00:00
fn is_release_tag(tag: &str) -> bool {
tag.len() == "2020-02-24".len() && tag.starts_with(|c: char| c.is_ascii_digit())
}