]> git.lizzy.rs Git - rust.git/blob - xtask/src/main.rs
Revert "Rewrite `#[derive]` removal to be based on AST"
[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 use crate::dist::DistCmd;
32
33 fn main() -> Result<()> {
34     let _d = pushd(project_root())?;
35
36     let flags = flags::Xtask::from_env()?;
37     match flags.subcommand {
38         flags::XtaskCmd::Help(_) => {
39             println!("{}", flags::Xtask::HELP);
40             Ok(())
41         }
42         flags::XtaskCmd::Install(cmd) => cmd.run(),
43         flags::XtaskCmd::FuzzTests(_) => run_fuzzer(),
44         flags::XtaskCmd::PreCache(cmd) => cmd.run(),
45         flags::XtaskCmd::Release(cmd) => cmd.run(),
46         flags::XtaskCmd::Promote(cmd) => cmd.run(),
47         flags::XtaskCmd::Dist(flags) => {
48             DistCmd { nightly: flags.nightly, client_version: flags.client }.run()
49         }
50         flags::XtaskCmd::Metrics(cmd) => cmd.run(),
51         flags::XtaskCmd::Bb(cmd) => {
52             {
53                 let _d = pushd("./crates/rust-analyzer")?;
54                 cmd!("cargo build --release --features jemalloc").run()?;
55             }
56             cp("./target/release/rust-analyzer", format!("./target/rust-analyzer-{}", cmd.suffix))?;
57             Ok(())
58         }
59     }
60 }
61
62 fn project_root() -> PathBuf {
63     Path::new(
64         &env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| env!("CARGO_MANIFEST_DIR").to_owned()),
65     )
66     .ancestors()
67     .nth(1)
68     .unwrap()
69     .to_path_buf()
70 }
71
72 fn rust_files() -> impl Iterator<Item = PathBuf> {
73     rust_files_in(&project_root().join("crates"))
74 }
75
76 #[cfg(test)]
77 fn cargo_files() -> impl Iterator<Item = PathBuf> {
78     files_in(&project_root(), "toml")
79         .filter(|path| path.file_name().map(|it| it == "Cargo.toml").unwrap_or(false))
80 }
81
82 fn rust_files_in(path: &Path) -> impl Iterator<Item = PathBuf> {
83     files_in(path, "rs")
84 }
85
86 fn ensure_rustfmt() -> Result<()> {
87     let out = cmd!("rustfmt --version").read()?;
88     if !out.contains("stable") {
89         bail!(
90             "Failed to run rustfmt from toolchain 'stable'. \
91              Please run `rustup component add rustfmt --toolchain stable` to install it.",
92         )
93     }
94     Ok(())
95 }
96
97 fn run_fuzzer() -> Result<()> {
98     let _d = pushd("./crates/syntax")?;
99     let _e = pushenv("RUSTUP_TOOLCHAIN", "nightly");
100     if cmd!("cargo fuzz --help").read().is_err() {
101         cmd!("cargo install cargo-fuzz").run()?;
102     };
103
104     // Expecting nightly rustc
105     let out = cmd!("rustc --version").read()?;
106     if !out.contains("nightly") {
107         bail!("fuzz tests require nightly rustc")
108     }
109
110     cmd!("cargo fuzz run parser").run()?;
111     Ok(())
112 }
113
114 fn date_iso() -> Result<String> {
115     let res = cmd!("date --iso --utc").read()?;
116     Ok(res)
117 }
118
119 fn is_release_tag(tag: &str) -> bool {
120     tag.len() == "2020-02-24".len() && tag.starts_with(|c: char| c.is_ascii_digit())
121 }
122
123 fn files_in(path: &Path, ext: &'static str) -> impl Iterator<Item = PathBuf> {
124     let iter = WalkDir::new(path);
125     return iter
126         .into_iter()
127         .filter_entry(|e| !is_hidden(e))
128         .map(|e| e.unwrap())
129         .filter(|e| !e.file_type().is_dir())
130         .map(|e| e.into_path())
131         .filter(move |path| path.extension().map(|it| it == ext).unwrap_or(false));
132
133     fn is_hidden(entry: &DirEntry) -> bool {
134         entry.file_name().to_str().map(|s| s.starts_with('.')).unwrap_or(false)
135     }
136 }