]> git.lizzy.rs Git - rust.git/blob - xtask/src/main.rs
3e07daae9719897f6ea84c29f4e6a1ee1c7606b6
[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 `cargo xtask codegen`
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 use std::env;
12
13 use codegen::CodegenCmd;
14 use pico_args::Arguments;
15 use xshell::pushd;
16 use xtask::{
17     codegen::{self, Mode},
18     dist::DistCmd,
19     install::{InstallCmd, Malloc, ServerOpt},
20     metrics::MetricsCmd,
21     pre_cache::PreCacheCmd,
22     pre_commit, project_root,
23     release::{PromoteCmd, ReleaseCmd},
24     run_clippy, run_fuzzer, run_rustfmt, Result,
25 };
26
27 fn main() -> Result<()> {
28     if env::args().next().map(|it| it.contains("pre-commit")) == Some(true) {
29         return pre_commit::run_hook();
30     }
31
32     let _d = pushd(project_root())?;
33
34     let mut args = Arguments::from_env();
35     let subcommand = args.subcommand()?.unwrap_or_default();
36
37     match subcommand.as_str() {
38         "install" => {
39             if args.contains(["-h", "--help"]) {
40                 eprintln!(
41                     "\
42 cargo xtask install
43 Install rust-analyzer server or editor plugin.
44
45 USAGE:
46     cargo xtask install [FLAGS]
47
48 FLAGS:
49         --client[=CLIENT] Install only VS Code plugin.
50                           CLIENT is one of 'code', 'code-exploration', 'code-insiders', 'codium', or 'code-oss'
51         --server          Install only the language server
52         --mimalloc        Use mimalloc for server
53     -h, --help            Prints help information
54         "
55                 );
56                 return Ok(());
57             }
58             let server = args.contains("--server");
59             let client_code = args.contains("--client");
60             if server && client_code {
61                 eprintln!(
62                     "error: The argument `--server` cannot be used with `--client`\n\n\
63                      For more information try --help"
64                 );
65                 return Ok(());
66             }
67
68             let malloc =
69                 if args.contains("--mimalloc") { Malloc::Mimalloc } else { Malloc::System };
70
71             let client_opt = args.opt_value_from_str("--client")?;
72
73             args.finish()?;
74
75             InstallCmd {
76                 client: if server { None } else { Some(client_opt.unwrap_or_default()) },
77                 server: if client_code { None } else { Some(ServerOpt { malloc }) },
78             }
79             .run()
80         }
81         "codegen" => {
82             let features = args.contains("--features");
83             args.finish()?;
84             CodegenCmd { features }.run()
85         }
86         "format" => {
87             args.finish()?;
88             run_rustfmt(Mode::Overwrite)
89         }
90         "install-pre-commit-hook" => {
91             args.finish()?;
92             pre_commit::install_hook()
93         }
94         "lint" => {
95             args.finish()?;
96             run_clippy()
97         }
98         "fuzz-tests" => {
99             args.finish()?;
100             run_fuzzer()
101         }
102         "pre-cache" => {
103             args.finish()?;
104             PreCacheCmd.run()
105         }
106         "release" => {
107             let dry_run = args.contains("--dry-run");
108             args.finish()?;
109             ReleaseCmd { dry_run }.run()
110         }
111         "promote" => {
112             let dry_run = args.contains("--dry-run");
113             args.finish()?;
114             PromoteCmd { dry_run }.run()
115         }
116         "dist" => {
117             let nightly = args.contains("--nightly");
118             let client_version: Option<String> = args.opt_value_from_str("--client")?;
119             args.finish()?;
120             DistCmd { nightly, client_version }.run()
121         }
122         "metrics" => {
123             let dry_run = args.contains("--dry-run");
124             args.finish()?;
125             MetricsCmd { dry_run }.run()
126         }
127         _ => {
128             eprintln!(
129                 "\
130 cargo xtask
131 Run custom build command.
132
133 USAGE:
134     cargo xtask <SUBCOMMAND>
135
136 SUBCOMMANDS:
137     format
138     install-pre-commit-hook
139     fuzz-tests
140     codegen
141     install
142     lint
143     dist
144     promote"
145             );
146             Ok(())
147         }
148     }
149 }