]> git.lizzy.rs Git - rust.git/blob - xtask/src/main.rs
e0e620c762d7b10f231589752046021e6cd1ece2
[rust.git] / xtask / src / main.rs
1 //! See https://github.com/matklad/cargo-xtask/.
2 //!
3 //! This binary defines various auxiliary build commands, which are not
4 //! expressible with just `cargo`. Notably, it provides tests via `cargo test -p xtask`
5 //! for code generation and `cargo xtask install` for installation of
6 //! rust-analyzer server and client.
7 //!
8 //! This binary is integrated into the `cargo` command line by using an alias in
9 //! `.cargo/config`.
10 mod flags;
11
12 mod codegen;
13 mod ast_src;
14 #[cfg(test)]
15 mod tidy;
16
17 mod install;
18 mod release;
19 mod dist;
20 mod metrics;
21 mod pre_cache;
22
23 use anyhow::{bail, Result};
24 use std::{
25     env,
26     path::{Path, PathBuf},
27 };
28 use walkdir::{DirEntry, WalkDir};
29 use xshell::{cmd, cp, pushd, pushenv};
30
31 fn main() -> Result<()> {
32     let _d = pushd(project_root())?;
33
34     let flags = flags::Xtask::from_env()?;
35     match flags.subcommand {
36         flags::XtaskCmd::Help(_) => {
37             println!("{}", flags::Xtask::HELP);
38             Ok(())
39         }
40         flags::XtaskCmd::Install(cmd) => cmd.run(),
41         flags::XtaskCmd::FuzzTests(_) => run_fuzzer(),
42         flags::XtaskCmd::PreCache(cmd) => cmd.run(),
43         flags::XtaskCmd::Release(cmd) => cmd.run(),
44         flags::XtaskCmd::Promote(cmd) => cmd.run(),
45         flags::XtaskCmd::Dist(cmd) => cmd.run(),
46         flags::XtaskCmd::Metrics(cmd) => cmd.run(),
47         flags::XtaskCmd::Bb(cmd) => {
48             {
49                 let _d = pushd("./crates/rust-analyzer")?;
50                 cmd!("cargo build --release --features jemalloc").run()?;
51             }
52             cp("./target/release/rust-analyzer", format!("./target/rust-analyzer-{}", cmd.suffix))?;
53             Ok(())
54         }
55     }
56 }
57
58 fn project_root() -> PathBuf {
59     Path::new(
60         &env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| env!("CARGO_MANIFEST_DIR").to_owned()),
61     )
62     .ancestors()
63     .nth(1)
64     .unwrap()
65     .to_path_buf()
66 }
67
68 fn rust_files() -> impl Iterator<Item = PathBuf> {
69     rust_files_in(&project_root().join("crates"))
70 }
71
72 #[cfg(test)]
73 fn cargo_files() -> impl Iterator<Item = PathBuf> {
74     files_in(&project_root(), "toml")
75         .filter(|path| path.file_name().map(|it| it == "Cargo.toml").unwrap_or(false))
76 }
77
78 fn rust_files_in(path: &Path) -> impl Iterator<Item = PathBuf> {
79     files_in(path, "rs")
80 }
81
82 fn ensure_rustfmt() -> Result<()> {
83     let out = cmd!("rustfmt --version").read()?;
84     if !out.contains("stable") {
85         bail!(
86             "Failed to run rustfmt from toolchain 'stable'. \
87              Please run `rustup component add rustfmt --toolchain stable` to install it.",
88         )
89     }
90     Ok(())
91 }
92
93 fn run_fuzzer() -> Result<()> {
94     let _d = pushd("./crates/syntax")?;
95     let _e = pushenv("RUSTUP_TOOLCHAIN", "nightly");
96     if cmd!("cargo fuzz --help").read().is_err() {
97         cmd!("cargo install cargo-fuzz").run()?;
98     };
99
100     // Expecting nightly rustc
101     let out = cmd!("rustc --version").read()?;
102     if !out.contains("nightly") {
103         bail!("fuzz tests require nightly rustc")
104     }
105
106     cmd!("cargo fuzz run parser").run()?;
107     Ok(())
108 }
109
110 fn date_iso() -> Result<String> {
111     let res = cmd!("date --iso --utc").read()?;
112     Ok(res)
113 }
114
115 fn is_release_tag(tag: &str) -> bool {
116     tag.len() == "2020-02-24".len() && tag.starts_with(|c: char| c.is_ascii_digit())
117 }
118
119 fn files_in(path: &Path, ext: &'static str) -> impl Iterator<Item = PathBuf> {
120     let iter = WalkDir::new(path);
121     return iter
122         .into_iter()
123         .filter_entry(|e| !is_hidden(e))
124         .map(|e| e.unwrap())
125         .filter(|e| !e.file_type().is_dir())
126         .map(|e| e.into_path())
127         .filter(move |path| path.extension().map(|it| it == ext).unwrap_or(false));
128
129     fn is_hidden(entry: &DirEntry) -> bool {
130         entry.file_name().to_str().map(|s| s.starts_with('.')).unwrap_or(false)
131     }
132 }