aboutsummaryrefslogtreecommitdiff
path: root/xtask
diff options
context:
space:
mode:
Diffstat (limited to 'xtask')
-rw-r--r--xtask/Cargo.toml5
-rw-r--r--xtask/src/codegen.rs14
-rw-r--r--xtask/src/codegen/gen_assists_docs.rs4
-rw-r--r--xtask/src/codegen/gen_diagnostic_docs.rs2
-rw-r--r--xtask/src/codegen/gen_feature_docs.rs2
-rw-r--r--xtask/src/codegen/gen_lint_completions.rs2
-rw-r--r--xtask/src/codegen/gen_parser_tests.rs2
-rw-r--r--xtask/src/codegen/gen_syntax.rs2
-rw-r--r--xtask/src/dist.rs8
-rw-r--r--xtask/src/flags.rs139
-rw-r--r--xtask/src/install.rs18
-rw-r--r--xtask/src/lib.rs131
-rw-r--r--xtask/src/main.rs273
-rw-r--r--xtask/src/metrics.rs10
-rw-r--r--xtask/src/pre_cache.rs6
-rw-r--r--xtask/src/pre_commit.rs38
-rw-r--r--xtask/src/release.rs18
-rw-r--r--xtask/src/tidy.rs (renamed from xtask/tests/tidy.rs)46
18 files changed, 360 insertions, 360 deletions
diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml
index b379797f9..b17dde598 100644
--- a/xtask/Cargo.toml
+++ b/xtask/Cargo.toml
@@ -6,17 +6,14 @@ authors = ["rust-analyzer developers"]
6publish = false 6publish = false
7license = "MIT OR Apache-2.0" 7license = "MIT OR Apache-2.0"
8 8
9[lib]
10doctest = false
11
12[dependencies] 9[dependencies]
13anyhow = "1.0.26" 10anyhow = "1.0.26"
14flate2 = "1.0" 11flate2 = "1.0"
15pico-args = "0.4.0"
16proc-macro2 = "1.0.8" 12proc-macro2 = "1.0.8"
17quote = "1.0.2" 13quote = "1.0.2"
18ungrammar = "=1.11" 14ungrammar = "=1.11"
19walkdir = "2.3.1" 15walkdir = "2.3.1"
20write-json = "0.1.0" 16write-json = "0.1.0"
21xshell = "0.1" 17xshell = "0.1"
18xflags = "0.1.2"
22# Avoid adding more dependencies to this crate 19# Avoid adding more dependencies to this crate
diff --git a/xtask/src/codegen.rs b/xtask/src/codegen.rs
index adea053b6..2f56c5ad0 100644
--- a/xtask/src/codegen.rs
+++ b/xtask/src/codegen.rs
@@ -18,9 +18,9 @@ use std::{
18}; 18};
19use xshell::{cmd, pushenv, read_file, write_file}; 19use xshell::{cmd, pushenv, read_file, write_file};
20 20
21use crate::{ensure_rustfmt, project_root, Result}; 21use crate::{ensure_rustfmt, flags, project_root, Result};
22 22
23pub use self::{ 23pub(crate) use self::{
24 gen_assists_docs::{generate_assists_docs, generate_assists_tests}, 24 gen_assists_docs::{generate_assists_docs, generate_assists_tests},
25 gen_diagnostic_docs::generate_diagnostic_docs, 25 gen_diagnostic_docs::generate_diagnostic_docs,
26 gen_feature_docs::generate_feature_docs, 26 gen_feature_docs::generate_feature_docs,
@@ -30,17 +30,13 @@ pub use self::{
30}; 30};
31 31
32#[derive(Debug, PartialEq, Eq, Clone, Copy)] 32#[derive(Debug, PartialEq, Eq, Clone, Copy)]
33pub enum Mode { 33pub(crate) enum Mode {
34 Overwrite, 34 Overwrite,
35 Verify, 35 Verify,
36} 36}
37 37
38pub struct CodegenCmd { 38impl flags::Codegen {
39 pub features: bool, 39 pub(crate) fn run(self) -> Result<()> {
40}
41
42impl CodegenCmd {
43 pub fn run(self) -> Result<()> {
44 if self.features { 40 if self.features {
45 generate_lint_completions(Mode::Overwrite)?; 41 generate_lint_completions(Mode::Overwrite)?;
46 } 42 }
diff --git a/xtask/src/codegen/gen_assists_docs.rs b/xtask/src/codegen/gen_assists_docs.rs
index 1ae1343a5..c469b388d 100644
--- a/xtask/src/codegen/gen_assists_docs.rs
+++ b/xtask/src/codegen/gen_assists_docs.rs
@@ -7,12 +7,12 @@ use crate::{
7 project_root, rust_files_in, Result, 7 project_root, rust_files_in, Result,
8}; 8};
9 9
10pub fn generate_assists_tests(mode: Mode) -> Result<()> { 10pub(crate) fn generate_assists_tests(mode: Mode) -> Result<()> {
11 let assists = Assist::collect()?; 11 let assists = Assist::collect()?;
12 generate_tests(&assists, mode) 12 generate_tests(&assists, mode)
13} 13}
14 14
15pub fn generate_assists_docs(mode: Mode) -> Result<()> { 15pub(crate) fn generate_assists_docs(mode: Mode) -> Result<()> {
16 let assists = Assist::collect()?; 16 let assists = Assist::collect()?;
17 let contents = assists.into_iter().map(|it| it.to_string()).collect::<Vec<_>>().join("\n\n"); 17 let contents = assists.into_iter().map(|it| it.to_string()).collect::<Vec<_>>().join("\n\n");
18 let contents = format!("//{}\n{}\n", PREAMBLE, contents.trim()); 18 let contents = format!("//{}\n{}\n", PREAMBLE, contents.trim());
diff --git a/xtask/src/codegen/gen_diagnostic_docs.rs b/xtask/src/codegen/gen_diagnostic_docs.rs
index 7c14d4a07..a2561817b 100644
--- a/xtask/src/codegen/gen_diagnostic_docs.rs
+++ b/xtask/src/codegen/gen_diagnostic_docs.rs
@@ -7,7 +7,7 @@ use crate::{
7 project_root, rust_files, Result, 7 project_root, rust_files, Result,
8}; 8};
9 9
10pub fn generate_diagnostic_docs(mode: Mode) -> Result<()> { 10pub(crate) fn generate_diagnostic_docs(mode: Mode) -> Result<()> {
11 let diagnostics = Diagnostic::collect()?; 11 let diagnostics = Diagnostic::collect()?;
12 let contents = 12 let contents =
13 diagnostics.into_iter().map(|it| it.to_string()).collect::<Vec<_>>().join("\n\n"); 13 diagnostics.into_iter().map(|it| it.to_string()).collect::<Vec<_>>().join("\n\n");
diff --git a/xtask/src/codegen/gen_feature_docs.rs b/xtask/src/codegen/gen_feature_docs.rs
index 61081063b..cad7ff477 100644
--- a/xtask/src/codegen/gen_feature_docs.rs
+++ b/xtask/src/codegen/gen_feature_docs.rs
@@ -7,7 +7,7 @@ use crate::{
7 project_root, rust_files, Result, 7 project_root, rust_files, Result,
8}; 8};
9 9
10pub fn generate_feature_docs(mode: Mode) -> Result<()> { 10pub(crate) fn generate_feature_docs(mode: Mode) -> Result<()> {
11 let features = Feature::collect()?; 11 let features = Feature::collect()?;
12 let contents = features.into_iter().map(|it| it.to_string()).collect::<Vec<_>>().join("\n\n"); 12 let contents = features.into_iter().map(|it| it.to_string()).collect::<Vec<_>>().join("\n\n");
13 let contents = format!("//{}\n{}\n", PREAMBLE, contents.trim()); 13 let contents = format!("//{}\n{}\n", PREAMBLE, contents.trim());
diff --git a/xtask/src/codegen/gen_lint_completions.rs b/xtask/src/codegen/gen_lint_completions.rs
index 8c51d35c7..b1c057037 100644
--- a/xtask/src/codegen/gen_lint_completions.rs
+++ b/xtask/src/codegen/gen_lint_completions.rs
@@ -10,7 +10,7 @@ use crate::{
10 run_rustfmt, 10 run_rustfmt,
11}; 11};
12 12
13pub fn generate_lint_completions(mode: Mode) -> Result<()> { 13pub(crate) fn generate_lint_completions(mode: Mode) -> Result<()> {
14 if !Path::new("./target/rust").exists() { 14 if !Path::new("./target/rust").exists() {
15 cmd!("git clone --depth=1 https://github.com/rust-lang/rust ./target/rust").run()?; 15 cmd!("git clone --depth=1 https://github.com/rust-lang/rust ./target/rust").run()?;
16 } 16 }
diff --git a/xtask/src/codegen/gen_parser_tests.rs b/xtask/src/codegen/gen_parser_tests.rs
index 6e4abd10c..cb8939063 100644
--- a/xtask/src/codegen/gen_parser_tests.rs
+++ b/xtask/src/codegen/gen_parser_tests.rs
@@ -12,7 +12,7 @@ use crate::{
12 project_root, Result, 12 project_root, Result,
13}; 13};
14 14
15pub fn generate_parser_tests(mode: Mode) -> Result<()> { 15pub(crate) fn generate_parser_tests(mode: Mode) -> Result<()> {
16 let tests = tests_from_dir(&project_root().join(Path::new("crates/parser/src/grammar")))?; 16 let tests = tests_from_dir(&project_root().join(Path::new("crates/parser/src/grammar")))?;
17 fn install_tests(tests: &HashMap<String, Test>, into: &str, mode: Mode) -> Result<()> { 17 fn install_tests(tests: &HashMap<String, Test>, into: &str, mode: Mode) -> Result<()> {
18 let tests_dir = project_root().join(into); 18 let tests_dir = project_root().join(into);
diff --git a/xtask/src/codegen/gen_syntax.rs b/xtask/src/codegen/gen_syntax.rs
index eb524d85a..191bc0e9d 100644
--- a/xtask/src/codegen/gen_syntax.rs
+++ b/xtask/src/codegen/gen_syntax.rs
@@ -18,7 +18,7 @@ use crate::{
18 project_root, Result, 18 project_root, Result,
19}; 19};
20 20
21pub fn generate_syntax(mode: Mode) -> Result<()> { 21pub(crate) fn generate_syntax(mode: Mode) -> Result<()> {
22 let grammar = rust_grammar(); 22 let grammar = rust_grammar();
23 let ast = lower(&grammar); 23 let ast = lower(&grammar);
24 24
diff --git a/xtask/src/dist.rs b/xtask/src/dist.rs
index 56bf9f99d..f2503f807 100644
--- a/xtask/src/dist.rs
+++ b/xtask/src/dist.rs
@@ -11,13 +11,13 @@ use xshell::{cmd, cp, mkdir_p, pushd, read_file, rm_rf, write_file};
11 11
12use crate::{date_iso, project_root}; 12use crate::{date_iso, project_root};
13 13
14pub struct DistCmd { 14pub(crate) struct DistCmd {
15 pub nightly: bool, 15 pub(crate) nightly: bool,
16 pub client_version: Option<String>, 16 pub(crate) client_version: Option<String>,
17} 17}
18 18
19impl DistCmd { 19impl DistCmd {
20 pub fn run(self) -> Result<()> { 20 pub(crate) fn run(self) -> Result<()> {
21 let dist = project_root().join("dist"); 21 let dist = project_root().join("dist");
22 rm_rf(&dist)?; 22 rm_rf(&dist)?;
23 mkdir_p(&dist)?; 23 mkdir_p(&dist)?;
diff --git a/xtask/src/flags.rs b/xtask/src/flags.rs
new file mode 100644
index 000000000..5710fbdb5
--- /dev/null
+++ b/xtask/src/flags.rs
@@ -0,0 +1,139 @@
1#![allow(unreachable_pub)]
2
3xflags::args_parser! {
4 /// Run custom build command.
5 cmd xtask {
6 default cmd help {
7 /// Print help information.
8 optional -h, --help
9 }
10
11 /// Install rust-analyzer server or editor plugin.
12 cmd install {
13 /// Install only VS Code plugin.
14 optional --client
15 /// One of 'code', 'code-exploration', 'code-insiders', 'codium', or 'code-oss'.
16 optional --code-bin name: String
17
18 /// Install only the language server.
19 optional --server
20 /// Use mimalloc allocator for server
21 optional --mimalloc
22 /// Use jemalloc allocator for server
23 optional --jemalloc
24 }
25
26 cmd codegen {
27 optional --features
28 }
29
30 cmd lint {}
31 cmd fuzz-tests {}
32 cmd pre-cache {}
33
34 cmd release {
35 optional --dry-run
36 }
37 cmd promote {
38 optional --dry-run
39 }
40 cmd dist {
41 optional --nightly
42 optional --client version: String
43 }
44 cmd metrics {
45 optional --dry-run
46 }
47 /// Builds a benchmark version of rust-analyzer and puts it into `./target`.
48 cmd bb
49 required suffix: String
50 {}
51 }
52}
53
54// generated start
55// The following code is generated by `xflags` macro.
56// Run `env XFLAGS_DUMP= cargo build` to regenerate.
57#[derive(Debug)]
58pub struct Xtask {
59 pub subcommand: XtaskCmd,
60}
61
62#[derive(Debug)]
63pub enum XtaskCmd {
64 Help(Help),
65 Install(Install),
66 Codegen(Codegen),
67 Lint(Lint),
68 FuzzTests(FuzzTests),
69 PreCache(PreCache),
70 Release(Release),
71 Promote(Promote),
72 Dist(Dist),
73 Metrics(Metrics),
74 Bb(Bb),
75}
76
77#[derive(Debug)]
78pub struct Help {
79 pub help: bool,
80}
81
82#[derive(Debug)]
83pub struct Install {
84 pub client: bool,
85 pub code_bin: Option<String>,
86 pub server: bool,
87 pub mimalloc: bool,
88 pub jemalloc: bool,
89}
90
91#[derive(Debug)]
92pub struct Codegen {
93 pub features: bool,
94}
95
96#[derive(Debug)]
97pub struct Lint {}
98
99#[derive(Debug)]
100pub struct FuzzTests {}
101
102#[derive(Debug)]
103pub struct PreCache {}
104
105#[derive(Debug)]
106pub struct Release {
107 pub dry_run: bool,
108}
109
110#[derive(Debug)]
111pub struct Promote {
112 pub dry_run: bool,
113}
114
115#[derive(Debug)]
116pub struct Dist {
117 pub nightly: bool,
118 pub client: Option<String>,
119}
120
121#[derive(Debug)]
122pub struct Metrics {
123 pub dry_run: bool,
124}
125
126#[derive(Debug)]
127pub struct Bb {
128 pub suffix: String,
129}
130
131impl Xtask {
132 pub const HELP: &'static str = Self::_HELP;
133
134 pub fn from_env() -> xflags::Result<Self> {
135 let mut p = xflags::rt::Parser::new_from_env();
136 Self::_parse(&mut p)
137 }
138}
139// generated end
diff --git a/xtask/src/install.rs b/xtask/src/install.rs
index 4c5c2673c..ea2194248 100644
--- a/xtask/src/install.rs
+++ b/xtask/src/install.rs
@@ -8,13 +8,13 @@ use xshell::{cmd, pushd};
8// Latest stable, feel free to send a PR if this lags behind. 8// Latest stable, feel free to send a PR if this lags behind.
9const REQUIRED_RUST_VERSION: u32 = 50; 9const REQUIRED_RUST_VERSION: u32 = 50;
10 10
11pub struct InstallCmd { 11pub(crate) struct InstallCmd {
12 pub client: Option<ClientOpt>, 12 pub(crate) client: Option<ClientOpt>,
13 pub server: Option<ServerOpt>, 13 pub(crate) server: Option<ServerOpt>,
14} 14}
15 15
16#[derive(Clone, Copy)] 16#[derive(Clone, Copy)]
17pub enum ClientOpt { 17pub(crate) enum ClientOpt {
18 VsCode, 18 VsCode,
19 VsCodeExploration, 19 VsCodeExploration,
20 VsCodeInsiders, 20 VsCodeInsiders,
@@ -24,7 +24,7 @@ pub enum ClientOpt {
24} 24}
25 25
26impl ClientOpt { 26impl ClientOpt {
27 pub const fn as_cmds(&self) -> &'static [&'static str] { 27 pub(crate) const fn as_cmds(&self) -> &'static [&'static str] {
28 match self { 28 match self {
29 ClientOpt::VsCode => &["code"], 29 ClientOpt::VsCode => &["code"],
30 ClientOpt::VsCodeExploration => &["code-exploration"], 30 ClientOpt::VsCodeExploration => &["code-exploration"],
@@ -60,18 +60,18 @@ impl std::str::FromStr for ClientOpt {
60 } 60 }
61} 61}
62 62
63pub struct ServerOpt { 63pub(crate) struct ServerOpt {
64 pub malloc: Malloc, 64 pub(crate) malloc: Malloc,
65} 65}
66 66
67pub enum Malloc { 67pub(crate) enum Malloc {
68 System, 68 System,
69 Mimalloc, 69 Mimalloc,
70 Jemalloc, 70 Jemalloc,
71} 71}
72 72
73impl InstallCmd { 73impl InstallCmd {
74 pub fn run(self) -> Result<()> { 74 pub(crate) fn run(self) -> Result<()> {
75 if cfg!(target_os = "macos") { 75 if cfg!(target_os = "macos") {
76 fix_path_for_mac().context("Fix path for mac")? 76 fix_path_for_mac().context("Fix path for mac")?
77 } 77 }
diff --git a/xtask/src/lib.rs b/xtask/src/lib.rs
deleted file mode 100644
index b19985fb2..000000000
--- a/xtask/src/lib.rs
+++ /dev/null
@@ -1,131 +0,0 @@
1//! Support library for `cargo xtask` command.
2//!
3//! See https://github.com/matklad/cargo-xtask/
4
5pub mod codegen;
6mod ast_src;
7
8pub mod install;
9pub mod release;
10pub mod dist;
11pub mod pre_commit;
12pub mod metrics;
13pub mod pre_cache;
14
15use std::{
16 env,
17 path::{Path, PathBuf},
18};
19
20use walkdir::{DirEntry, WalkDir};
21use xshell::{cmd, pushd, pushenv};
22
23use crate::codegen::Mode;
24
25pub use anyhow::{bail, Context as _, Result};
26
27pub fn project_root() -> PathBuf {
28 Path::new(
29 &env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| env!("CARGO_MANIFEST_DIR").to_owned()),
30 )
31 .ancestors()
32 .nth(1)
33 .unwrap()
34 .to_path_buf()
35}
36
37pub fn rust_files() -> impl Iterator<Item = PathBuf> {
38 rust_files_in(&project_root().join("crates"))
39}
40
41pub fn cargo_files() -> impl Iterator<Item = PathBuf> {
42 files_in(&project_root(), "toml")
43 .filter(|path| path.file_name().map(|it| it == "Cargo.toml").unwrap_or(false))
44}
45
46pub fn rust_files_in(path: &Path) -> impl Iterator<Item = PathBuf> {
47 files_in(path, "rs")
48}
49
50pub fn run_rustfmt(mode: Mode) -> Result<()> {
51 let _dir = pushd(project_root())?;
52 let _e = pushenv("RUSTUP_TOOLCHAIN", "stable");
53 ensure_rustfmt()?;
54 let check = match mode {
55 Mode::Overwrite => &[][..],
56 Mode::Verify => &["--", "--check"],
57 };
58 cmd!("cargo fmt {check...}").run()?;
59 Ok(())
60}
61
62fn ensure_rustfmt() -> Result<()> {
63 let out = cmd!("rustfmt --version").read()?;
64 if !out.contains("stable") {
65 bail!(
66 "Failed to run rustfmt from toolchain 'stable'. \
67 Please run `rustup component add rustfmt --toolchain stable` to install it.",
68 )
69 }
70 Ok(())
71}
72
73pub fn run_clippy() -> Result<()> {
74 if cmd!("cargo clippy --version").read().is_err() {
75 bail!(
76 "Failed run cargo clippy. \
77 Please run `rustup component add clippy` to install it.",
78 )
79 }
80
81 let allowed_lints = "
82 -A clippy::collapsible_if
83 -A clippy::needless_pass_by_value
84 -A clippy::nonminimal_bool
85 -A clippy::redundant_pattern_matching
86 "
87 .split_ascii_whitespace();
88 cmd!("cargo clippy --all-features --all-targets -- {allowed_lints...}").run()?;
89 Ok(())
90}
91
92pub fn run_fuzzer() -> Result<()> {
93 let _d = pushd("./crates/syntax")?;
94 let _e = pushenv("RUSTUP_TOOLCHAIN", "nightly");
95 if cmd!("cargo fuzz --help").read().is_err() {
96 cmd!("cargo install cargo-fuzz").run()?;
97 };
98
99 // Expecting nightly rustc
100 let out = cmd!("rustc --version").read()?;
101 if !out.contains("nightly") {
102 bail!("fuzz tests require nightly rustc")
103 }
104
105 cmd!("cargo fuzz run parser").run()?;
106 Ok(())
107}
108
109fn date_iso() -> Result<String> {
110 let res = cmd!("date --iso --utc").read()?;
111 Ok(res)
112}
113
114fn is_release_tag(tag: &str) -> bool {
115 tag.len() == "2020-02-24".len() && tag.starts_with(|c: char| c.is_ascii_digit())
116}
117
118fn files_in(path: &Path, ext: &'static str) -> impl Iterator<Item = PathBuf> {
119 let iter = WalkDir::new(path);
120 return iter
121 .into_iter()
122 .filter_entry(|e| !is_hidden(e))
123 .map(|e| e.unwrap())
124 .filter(|e| !e.file_type().is_dir())
125 .map(|e| e.into_path())
126 .filter(move |path| path.extension().map(|it| it == ext).unwrap_or(false));
127
128 fn is_hidden(entry: &DirEntry) -> bool {
129 entry.file_name().to_str().map(|s| s.starts_with('.')).unwrap_or(false)
130 }
131}
diff --git a/xtask/src/main.rs b/xtask/src/main.rs
index cbb9b315e..e419db7a7 100644
--- a/xtask/src/main.rs
+++ b/xtask/src/main.rs
@@ -7,59 +7,44 @@
7//! 7//!
8//! This binary is integrated into the `cargo` command line by using an alias in 8//! This binary is integrated into the `cargo` command line by using an alias in
9//! `.cargo/config`. 9//! `.cargo/config`.
10mod flags;
10 11
11use std::env; 12mod codegen;
13mod ast_src;
14#[cfg(test)]
15mod tidy;
12 16
13use anyhow::bail; 17mod install;
14use codegen::CodegenCmd; 18mod release;
15use pico_args::Arguments; 19mod dist;
16use xshell::{cmd, cp, pushd}; 20mod metrics;
17use xtask::{ 21mod pre_cache;
18 codegen::{self, Mode}, 22
23use anyhow::{bail, Result};
24use std::{
25 env,
26 path::{Path, PathBuf},
27};
28use walkdir::{DirEntry, WalkDir};
29use xshell::{cmd, cp, pushd, pushenv};
30
31use crate::{
32 codegen::Mode,
19 dist::DistCmd, 33 dist::DistCmd,
20 install::{InstallCmd, Malloc, ServerOpt}, 34 install::{InstallCmd, Malloc, ServerOpt},
21 metrics::MetricsCmd,
22 pre_cache::PreCacheCmd,
23 pre_commit, project_root,
24 release::{PromoteCmd, ReleaseCmd},
25 run_clippy, run_fuzzer, run_rustfmt, Result,
26}; 35};
27 36
28fn main() -> Result<()> { 37fn main() -> Result<()> {
29 if env::args().next().map(|it| it.contains("pre-commit")) == Some(true) {
30 return pre_commit::run_hook();
31 }
32
33 let _d = pushd(project_root())?; 38 let _d = pushd(project_root())?;
34 39
35 let mut args = Arguments::from_env(); 40 let flags = flags::Xtask::from_env()?;
36 let subcommand = args.subcommand()?.unwrap_or_default(); 41 match flags.subcommand {
37 42 flags::XtaskCmd::Help(_) => {
38 match subcommand.as_str() { 43 println!("{}", flags::Xtask::HELP);
39 "install" => { 44 return Ok(());
40 if args.contains(["-h", "--help"]) { 45 }
41 eprintln!( 46 flags::XtaskCmd::Install(flags) => {
42 "\ 47 if flags.server && flags.client {
43cargo xtask install
44Install rust-analyzer server or editor plugin.
45
46USAGE:
47 cargo xtask install [FLAGS]
48
49FLAGS:
50 --client[=CLIENT] Install only VS Code plugin.
51 CLIENT is one of 'code', 'code-exploration', 'code-insiders', 'codium', or 'code-oss'
52 --server Install only the language server
53 --mimalloc Use mimalloc allocator for server
54 --jemalloc Use jemalloc allocator for server
55 -h, --help Prints help information
56 "
57 );
58 return Ok(());
59 }
60 let server = args.contains("--server");
61 let client_code = args.contains("--client");
62 if server && client_code {
63 eprintln!( 48 eprintln!(
64 "error: The argument `--server` cannot be used with `--client`\n\n\ 49 "error: The argument `--server` cannot be used with `--client`\n\n\
65 For more information try --help" 50 For more information try --help"
@@ -67,108 +52,146 @@ FLAGS:
67 return Ok(()); 52 return Ok(());
68 } 53 }
69 54
70 let malloc = if args.contains("--mimalloc") { 55 let malloc = if flags.mimalloc {
71 Malloc::Mimalloc 56 Malloc::Mimalloc
72 } else if args.contains("--jemalloc") { 57 } else if flags.jemalloc {
73 Malloc::Jemalloc 58 Malloc::Jemalloc
74 } else { 59 } else {
75 Malloc::System 60 Malloc::System
76 }; 61 };
77 62
78 let client_opt = args.opt_value_from_str("--client")?; 63 let client_bin = flags.code_bin.map(|it| it.parse()).transpose()?;
79
80 finish_args(args)?;
81 64
82 InstallCmd { 65 InstallCmd {
83 client: if server { None } else { Some(client_opt.unwrap_or_default()) }, 66 client: if flags.server { None } else { client_bin },
84 server: if client_code { None } else { Some(ServerOpt { malloc }) }, 67 server: if flags.client { None } else { Some(ServerOpt { malloc }) },
85 } 68 }
86 .run() 69 .run()
87 } 70 }
88 "codegen" => { 71 flags::XtaskCmd::Codegen(cmd) => cmd.run(),
89 let features = args.contains("--features"); 72 flags::XtaskCmd::Lint(_) => run_clippy(),
90 finish_args(args)?; 73 flags::XtaskCmd::FuzzTests(_) => run_fuzzer(),
91 CodegenCmd { features }.run() 74 flags::XtaskCmd::PreCache(cmd) => cmd.run(),
92 } 75 flags::XtaskCmd::Release(cmd) => cmd.run(),
93 "format" => { 76 flags::XtaskCmd::Promote(cmd) => cmd.run(),
94 finish_args(args)?; 77 flags::XtaskCmd::Dist(flags) => {
95 run_rustfmt(Mode::Overwrite) 78 DistCmd { nightly: flags.nightly, client_version: flags.client }.run()
96 }
97 "install-pre-commit-hook" => {
98 finish_args(args)?;
99 pre_commit::install_hook()
100 } 79 }
101 "lint" => { 80 flags::XtaskCmd::Metrics(cmd) => cmd.run(),
102 finish_args(args)?; 81 flags::XtaskCmd::Bb(cmd) => {
103 run_clippy()
104 }
105 "fuzz-tests" => {
106 finish_args(args)?;
107 run_fuzzer()
108 }
109 "pre-cache" => {
110 finish_args(args)?;
111 PreCacheCmd.run()
112 }
113 "release" => {
114 let dry_run = args.contains("--dry-run");
115 finish_args(args)?;
116 ReleaseCmd { dry_run }.run()
117 }
118 "promote" => {
119 let dry_run = args.contains("--dry-run");
120 finish_args(args)?;
121 PromoteCmd { dry_run }.run()
122 }
123 "dist" => {
124 let nightly = args.contains("--nightly");
125 let client_version: Option<String> = args.opt_value_from_str("--client")?;
126 finish_args(args)?;
127 DistCmd { nightly, client_version }.run()
128 }
129 "metrics" => {
130 let dry_run = args.contains("--dry-run");
131 finish_args(args)?;
132 MetricsCmd { dry_run }.run()
133 }
134 "bb" => {
135 let suffix: String = args.free_from_str()?;
136 finish_args(args)?;
137 { 82 {
138 let _d = pushd("./crates/rust-analyzer")?; 83 let _d = pushd("./crates/rust-analyzer")?;
139 cmd!("cargo build --release --features jemalloc").run()?; 84 cmd!("cargo build --release --features jemalloc").run()?;
140 } 85 }
141 cp("./target/release/rust-analyzer", format!("./target/rust-analyzer-{}", suffix))?; 86 cp("./target/release/rust-analyzer", format!("./target/rust-analyzer-{}", cmd.suffix))?;
142 Ok(())
143 }
144 _ => {
145 eprintln!(
146 "\
147cargo xtask
148Run custom build command.
149
150USAGE:
151 cargo xtask <SUBCOMMAND>
152
153SUBCOMMANDS:
154 format
155 install-pre-commit-hook
156 fuzz-tests
157 codegen
158 install
159 lint
160 dist
161 promote
162 bb"
163 );
164 Ok(()) 87 Ok(())
165 } 88 }
166 } 89 }
167} 90}
168 91
169fn finish_args(args: Arguments) -> Result<()> { 92fn project_root() -> PathBuf {
170 if !args.finish().is_empty() { 93 Path::new(
171 bail!("Unused arguments."); 94 &env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| env!("CARGO_MANIFEST_DIR").to_owned()),
95 )
96 .ancestors()
97 .nth(1)
98 .unwrap()
99 .to_path_buf()
100}
101
102fn rust_files() -> impl Iterator<Item = PathBuf> {
103 rust_files_in(&project_root().join("crates"))
104}
105
106#[cfg(test)]
107fn cargo_files() -> impl Iterator<Item = PathBuf> {
108 files_in(&project_root(), "toml")
109 .filter(|path| path.file_name().map(|it| it == "Cargo.toml").unwrap_or(false))
110}
111
112fn rust_files_in(path: &Path) -> impl Iterator<Item = PathBuf> {
113 files_in(path, "rs")
114}
115
116fn run_rustfmt(mode: Mode) -> Result<()> {
117 let _dir = pushd(project_root())?;
118 let _e = pushenv("RUSTUP_TOOLCHAIN", "stable");
119 ensure_rustfmt()?;
120 let check = match mode {
121 Mode::Overwrite => &[][..],
122 Mode::Verify => &["--", "--check"],
123 };
124 cmd!("cargo fmt {check...}").run()?;
125 Ok(())
126}
127
128fn ensure_rustfmt() -> Result<()> {
129 let out = cmd!("rustfmt --version").read()?;
130 if !out.contains("stable") {
131 bail!(
132 "Failed to run rustfmt from toolchain 'stable'. \
133 Please run `rustup component add rustfmt --toolchain stable` to install it.",
134 )
135 }
136 Ok(())
137}
138
139fn run_clippy() -> Result<()> {
140 if cmd!("cargo clippy --version").read().is_err() {
141 bail!(
142 "Failed run cargo clippy. \
143 Please run `rustup component add clippy` to install it.",
144 )
145 }
146
147 let allowed_lints = "
148 -A clippy::collapsible_if
149 -A clippy::needless_pass_by_value
150 -A clippy::nonminimal_bool
151 -A clippy::redundant_pattern_matching
152 "
153 .split_ascii_whitespace();
154 cmd!("cargo clippy --all-features --all-targets -- {allowed_lints...}").run()?;
155 Ok(())
156}
157
158fn run_fuzzer() -> Result<()> {
159 let _d = pushd("./crates/syntax")?;
160 let _e = pushenv("RUSTUP_TOOLCHAIN", "nightly");
161 if cmd!("cargo fuzz --help").read().is_err() {
162 cmd!("cargo install cargo-fuzz").run()?;
163 };
164
165 // Expecting nightly rustc
166 let out = cmd!("rustc --version").read()?;
167 if !out.contains("nightly") {
168 bail!("fuzz tests require nightly rustc")
172 } 169 }
170
171 cmd!("cargo fuzz run parser").run()?;
173 Ok(()) 172 Ok(())
174} 173}
174
175fn date_iso() -> Result<String> {
176 let res = cmd!("date --iso --utc").read()?;
177 Ok(res)
178}
179
180fn is_release_tag(tag: &str) -> bool {
181 tag.len() == "2020-02-24".len() && tag.starts_with(|c: char| c.is_ascii_digit())
182}
183
184fn files_in(path: &Path, ext: &'static str) -> impl Iterator<Item = PathBuf> {
185 let iter = WalkDir::new(path);
186 return iter
187 .into_iter()
188 .filter_entry(|e| !is_hidden(e))
189 .map(|e| e.unwrap())
190 .filter(|e| !e.file_type().is_dir())
191 .map(|e| e.into_path())
192 .filter(move |path| path.extension().map(|it| it == ext).unwrap_or(false));
193
194 fn is_hidden(entry: &DirEntry) -> bool {
195 entry.file_name().to_str().map(|s| s.starts_with('.')).unwrap_or(false)
196 }
197}
diff --git a/xtask/src/metrics.rs b/xtask/src/metrics.rs
index 624ad3b7e..eb58b3274 100644
--- a/xtask/src/metrics.rs
+++ b/xtask/src/metrics.rs
@@ -9,14 +9,12 @@ use std::{
9use anyhow::{bail, format_err, Result}; 9use anyhow::{bail, format_err, Result};
10use xshell::{cmd, mkdir_p, pushd, pushenv, read_file, rm_rf}; 10use xshell::{cmd, mkdir_p, pushd, pushenv, read_file, rm_rf};
11 11
12type Unit = String; 12use crate::flags;
13 13
14pub struct MetricsCmd { 14type Unit = String;
15 pub dry_run: bool,
16}
17 15
18impl MetricsCmd { 16impl flags::Metrics {
19 pub fn run(self) -> Result<()> { 17 pub(crate) fn run(self) -> Result<()> {
20 let mut metrics = Metrics::new()?; 18 let mut metrics = Metrics::new()?;
21 if !self.dry_run { 19 if !self.dry_run {
22 rm_rf("./target/release")?; 20 rm_rf("./target/release")?;
diff --git a/xtask/src/pre_cache.rs b/xtask/src/pre_cache.rs
index 569f88f68..b456224fd 100644
--- a/xtask/src/pre_cache.rs
+++ b/xtask/src/pre_cache.rs
@@ -6,12 +6,12 @@ use std::{
6use anyhow::Result; 6use anyhow::Result;
7use xshell::rm_rf; 7use xshell::rm_rf;
8 8
9pub struct PreCacheCmd; 9use crate::flags;
10 10
11impl PreCacheCmd { 11impl flags::PreCache {
12 /// Cleans the `./target` dir after the build such that only 12 /// Cleans the `./target` dir after the build such that only
13 /// dependencies are cached on CI. 13 /// dependencies are cached on CI.
14 pub fn run(self) -> Result<()> { 14 pub(crate) fn run(self) -> Result<()> {
15 let slow_tests_cookie = Path::new("./target/.slow_tests_cookie"); 15 let slow_tests_cookie = Path::new("./target/.slow_tests_cookie");
16 if !slow_tests_cookie.exists() { 16 if !slow_tests_cookie.exists() {
17 panic!("slow tests were skipped on CI!") 17 panic!("slow tests were skipped on CI!")
diff --git a/xtask/src/pre_commit.rs b/xtask/src/pre_commit.rs
deleted file mode 100644
index 8f2dbea19..000000000
--- a/xtask/src/pre_commit.rs
+++ /dev/null
@@ -1,38 +0,0 @@
1//! pre-commit hook for code formatting.
2
3use std::{fs, path::PathBuf};
4
5use anyhow::{bail, Result};
6use xshell::cmd;
7
8use crate::{project_root, run_rustfmt, Mode};
9
10// FIXME: if there are changed `.ts` files, also reformat TypeScript (by
11// shelling out to `npm fmt`).
12pub fn run_hook() -> Result<()> {
13 run_rustfmt(Mode::Overwrite)?;
14
15 let diff = cmd!("git diff --diff-filter=MAR --name-only --cached").read()?;
16
17 let root = project_root();
18 for line in diff.lines() {
19 let file = root.join(line);
20 cmd!("git update-index --add {file}").run()?;
21 }
22
23 Ok(())
24}
25
26pub fn install_hook() -> Result<()> {
27 let hook_path: PathBuf =
28 format!("./.git/hooks/pre-commit{}", std::env::consts::EXE_SUFFIX).into();
29
30 if hook_path.exists() {
31 bail!("Git hook already created");
32 }
33
34 let me = std::env::current_exe()?;
35 fs::copy(me, hook_path)?;
36
37 Ok(())
38}
diff --git a/xtask/src/release.rs b/xtask/src/release.rs
index 63556476d..d8d86fd63 100644
--- a/xtask/src/release.rs
+++ b/xtask/src/release.rs
@@ -2,14 +2,10 @@ use std::fmt::Write;
2 2
3use xshell::{cmd, cp, pushd, read_dir, write_file}; 3use xshell::{cmd, cp, pushd, read_dir, write_file};
4 4
5use crate::{codegen, date_iso, is_release_tag, project_root, Mode, Result}; 5use crate::{codegen, date_iso, flags, is_release_tag, project_root, Mode, Result};
6 6
7pub struct ReleaseCmd { 7impl flags::Release {
8 pub dry_run: bool, 8 pub(crate) fn run(self) -> Result<()> {
9}
10
11impl ReleaseCmd {
12 pub fn run(self) -> Result<()> {
13 if !self.dry_run { 9 if !self.dry_run {
14 cmd!("git switch release").run()?; 10 cmd!("git switch release").run()?;
15 cmd!("git fetch upstream --tags --force").run()?; 11 cmd!("git fetch upstream --tags --force").run()?;
@@ -86,12 +82,8 @@ https://github.com/sponsors/rust-analyzer[GitHub Sponsors].
86 } 82 }
87} 83}
88 84
89pub struct PromoteCmd { 85impl flags::Promote {
90 pub dry_run: bool, 86 pub(crate) fn run(self) -> Result<()> {
91}
92
93impl PromoteCmd {
94 pub fn run(self) -> Result<()> {
95 let _dir = pushd("../rust-rust-analyzer")?; 87 let _dir = pushd("../rust-rust-analyzer")?;
96 cmd!("git switch master").run()?; 88 cmd!("git switch master").run()?;
97 cmd!("git fetch upstream").run()?; 89 cmd!("git fetch upstream").run()?;
diff --git a/xtask/tests/tidy.rs b/xtask/src/tidy.rs
index a72498a38..349ca14d0 100644
--- a/xtask/tests/tidy.rs
+++ b/xtask/src/tidy.rs
@@ -4,7 +4,8 @@ use std::{
4}; 4};
5 5
6use xshell::{cmd, read_file}; 6use xshell::{cmd, read_file};
7use xtask::{ 7
8use crate::{
8 cargo_files, 9 cargo_files,
9 codegen::{self, Mode}, 10 codegen::{self, Mode},
10 project_root, run_rustfmt, rust_files, 11 project_root, run_rustfmt, rust_files,
@@ -100,21 +101,44 @@ fn cargo_files_are_tidy() {
100 let mut section = None; 101 let mut section = None;
101 for (line_no, text) in read_file(&cargo).unwrap().lines().enumerate() { 102 for (line_no, text) in read_file(&cargo).unwrap().lines().enumerate() {
102 let text = text.trim(); 103 let text = text.trim();
103 if text.starts_with("[") { 104 if text.starts_with('[') {
105 if !text.ends_with(']') {
106 panic!(
107 "\nplease don't add comments or trailing whitespace in section lines.\n\
108 {}:{}\n",
109 cargo.display(),
110 line_no + 1
111 )
112 }
104 section = Some(text); 113 section = Some(text);
105 continue; 114 continue;
106 } 115 }
107 if !section.map(|it| it.starts_with("[dependencies")).unwrap_or(false) { 116 let text: String = text.split_whitespace().collect();
117 if !text.contains("path=") {
108 continue; 118 continue;
109 } 119 }
110 let text: String = text.split_whitespace().collect(); 120 match section {
111 if text.contains("path=") && !text.contains("version") { 121 Some(s) if s.contains("dev-dependencies") => {
112 panic!( 122 if text.contains("version") {
113 "\ncargo internal dependencies should have version.\n\ 123 panic!(
114 {}:{}\n", 124 "\ncargo internal dev-dependencies should not have a version.\n\
115 cargo.display(), 125 {}:{}\n",
116 line_no + 1 126 cargo.display(),
117 ) 127 line_no + 1
128 );
129 }
130 }
131 Some(s) if s.contains("dependencies") => {
132 if !text.contains("version") {
133 panic!(
134 "\ncargo internal dependencies should have a version.\n\
135 {}:{}\n",
136 cargo.display(),
137 line_no + 1
138 );
139 }
140 }
141 _ => {}
118 } 142 }
119 } 143 }
120 } 144 }