]> git.lizzy.rs Git - rust.git/blob - xtask/src/main.rs
refactor helper function to work with function taking expression enum instead of...
[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 install;
13 mod release;
14 mod dist;
15 mod metrics;
16
17 use anyhow::{bail, Result};
18 use std::{
19     env,
20     path::{Path, PathBuf},
21 };
22 use xshell::{cmd, cp, pushd, pushenv};
23
24 fn main() -> Result<()> {
25     let _d = pushd(project_root())?;
26
27     let flags = flags::Xtask::from_env()?;
28     match flags.subcommand {
29         flags::XtaskCmd::Help(_) => {
30             println!("{}", flags::Xtask::HELP);
31             Ok(())
32         }
33         flags::XtaskCmd::Install(cmd) => cmd.run(),
34         flags::XtaskCmd::FuzzTests(_) => run_fuzzer(),
35         flags::XtaskCmd::Release(cmd) => cmd.run(),
36         flags::XtaskCmd::Promote(cmd) => cmd.run(),
37         flags::XtaskCmd::Dist(cmd) => cmd.run(),
38         flags::XtaskCmd::Metrics(cmd) => cmd.run(),
39         flags::XtaskCmd::Bb(cmd) => {
40             {
41                 let _d = pushd("./crates/rust-analyzer")?;
42                 cmd!("cargo build --release --features jemalloc").run()?;
43             }
44             cp("./target/release/rust-analyzer", format!("./target/rust-analyzer-{}", cmd.suffix))?;
45             Ok(())
46         }
47     }
48 }
49
50 fn project_root() -> PathBuf {
51     Path::new(
52         &env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| env!("CARGO_MANIFEST_DIR").to_owned()),
53     )
54     .ancestors()
55     .nth(1)
56     .unwrap()
57     .to_path_buf()
58 }
59
60 fn run_fuzzer() -> Result<()> {
61     let _d = pushd("./crates/syntax")?;
62     let _e = pushenv("RUSTUP_TOOLCHAIN", "nightly");
63     if cmd!("cargo fuzz --help").read().is_err() {
64         cmd!("cargo install cargo-fuzz").run()?;
65     };
66
67     // Expecting nightly rustc
68     let out = cmd!("rustc --version").read()?;
69     if !out.contains("nightly") {
70         bail!("fuzz tests require nightly rustc")
71     }
72
73     cmd!("cargo fuzz run parser").run()?;
74     Ok(())
75 }
76
77 fn date_iso() -> Result<String> {
78     let res = cmd!("date -u +%Y-%m-%d").read()?;
79     Ok(res)
80 }
81
82 fn is_release_tag(tag: &str) -> bool {
83     tag.len() == "2020-02-24".len() && tag.starts_with(|c: char| c.is_ascii_digit())
84 }