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