diff options
Diffstat (limited to 'xtask/src')
-rw-r--r-- | xtask/src/codegen.rs | 14 | ||||
-rw-r--r-- | xtask/src/codegen/gen_assists_docs.rs | 4 | ||||
-rw-r--r-- | xtask/src/codegen/gen_diagnostic_docs.rs | 2 | ||||
-rw-r--r-- | xtask/src/codegen/gen_feature_docs.rs | 2 | ||||
-rw-r--r-- | xtask/src/codegen/gen_lint_completions.rs | 2 | ||||
-rw-r--r-- | xtask/src/codegen/gen_parser_tests.rs | 2 | ||||
-rw-r--r-- | xtask/src/codegen/gen_syntax.rs | 2 | ||||
-rw-r--r-- | xtask/src/dist.rs | 8 | ||||
-rw-r--r-- | xtask/src/flags.rs | 172 | ||||
-rw-r--r-- | xtask/src/install.rs | 43 | ||||
-rw-r--r-- | xtask/src/lib.rs | 131 | ||||
-rw-r--r-- | xtask/src/main.rs | 286 | ||||
-rw-r--r-- | xtask/src/metrics.rs | 12 | ||||
-rw-r--r-- | xtask/src/pre_cache.rs | 6 | ||||
-rw-r--r-- | xtask/src/pre_commit.rs | 38 | ||||
-rw-r--r-- | xtask/src/release.rs | 18 | ||||
-rw-r--r-- | xtask/src/tidy.rs | 447 |
17 files changed, 810 insertions, 379 deletions
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 | }; |
19 | use xshell::{cmd, pushenv, read_file, write_file}; | 19 | use xshell::{cmd, pushenv, read_file, write_file}; |
20 | 20 | ||
21 | use crate::{ensure_rustfmt, project_root, Result}; | 21 | use crate::{ensure_rustfmt, flags, project_root, Result}; |
22 | 22 | ||
23 | pub use self::{ | 23 | pub(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)] |
33 | pub enum Mode { | 33 | pub(crate) enum Mode { |
34 | Overwrite, | 34 | Overwrite, |
35 | Verify, | 35 | Verify, |
36 | } | 36 | } |
37 | 37 | ||
38 | pub struct CodegenCmd { | 38 | impl flags::Codegen { |
39 | pub features: bool, | 39 | pub(crate) fn run(self) -> Result<()> { |
40 | } | ||
41 | |||
42 | impl 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 | ||
10 | pub fn generate_assists_tests(mode: Mode) -> Result<()> { | 10 | pub(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 | ||
15 | pub fn generate_assists_docs(mode: Mode) -> Result<()> { | 15 | pub(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 | ||
10 | pub fn generate_diagnostic_docs(mode: Mode) -> Result<()> { | 10 | pub(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 | ||
10 | pub fn generate_feature_docs(mode: Mode) -> Result<()> { | 10 | pub(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 | ||
13 | pub fn generate_lint_completions(mode: Mode) -> Result<()> { | 13 | pub(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 | ||
15 | pub fn generate_parser_tests(mode: Mode) -> Result<()> { | 15 | pub(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 | ||
21 | pub fn generate_syntax(mode: Mode) -> Result<()> { | 21 | pub(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 | ||
12 | use crate::{date_iso, project_root}; | 12 | use crate::{date_iso, project_root}; |
13 | 13 | ||
14 | pub struct DistCmd { | 14 | pub(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 | ||
19 | impl DistCmd { | 19 | impl 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..2ca05d3df --- /dev/null +++ b/xtask/src/flags.rs | |||
@@ -0,0 +1,172 @@ | |||
1 | #![allow(unreachable_pub)] | ||
2 | |||
3 | use crate::install::{ClientOpt, Malloc, ServerOpt}; | ||
4 | |||
5 | xflags::args_parser! { | ||
6 | /// Run custom build command. | ||
7 | cmd xtask { | ||
8 | default cmd help { | ||
9 | /// Print help information. | ||
10 | optional -h, --help | ||
11 | } | ||
12 | |||
13 | /// Install rust-analyzer server or editor plugin. | ||
14 | cmd install { | ||
15 | /// Install only VS Code plugin. | ||
16 | optional --client | ||
17 | /// One of 'code', 'code-exploration', 'code-insiders', 'codium', or 'code-oss'. | ||
18 | optional --code-bin name: String | ||
19 | |||
20 | /// Install only the language server. | ||
21 | optional --server | ||
22 | /// Use mimalloc allocator for server | ||
23 | optional --mimalloc | ||
24 | /// Use jemalloc allocator for server | ||
25 | optional --jemalloc | ||
26 | } | ||
27 | |||
28 | cmd codegen { | ||
29 | optional --features | ||
30 | } | ||
31 | |||
32 | cmd lint {} | ||
33 | cmd fuzz-tests {} | ||
34 | cmd pre-cache {} | ||
35 | |||
36 | cmd release { | ||
37 | optional --dry-run | ||
38 | } | ||
39 | cmd promote { | ||
40 | optional --dry-run | ||
41 | } | ||
42 | cmd dist { | ||
43 | optional --nightly | ||
44 | optional --client version: String | ||
45 | } | ||
46 | cmd metrics { | ||
47 | optional --dry-run | ||
48 | } | ||
49 | /// Builds a benchmark version of rust-analyzer and puts it into `./target`. | ||
50 | cmd bb | ||
51 | required suffix: String | ||
52 | {} | ||
53 | } | ||
54 | } | ||
55 | |||
56 | // generated start | ||
57 | // The following code is generated by `xflags` macro. | ||
58 | // Run `env XFLAGS_DUMP= cargo build` to regenerate. | ||
59 | #[derive(Debug)] | ||
60 | pub struct Xtask { | ||
61 | pub subcommand: XtaskCmd, | ||
62 | } | ||
63 | |||
64 | #[derive(Debug)] | ||
65 | pub enum XtaskCmd { | ||
66 | Help(Help), | ||
67 | Install(Install), | ||
68 | Codegen(Codegen), | ||
69 | Lint(Lint), | ||
70 | FuzzTests(FuzzTests), | ||
71 | PreCache(PreCache), | ||
72 | Release(Release), | ||
73 | Promote(Promote), | ||
74 | Dist(Dist), | ||
75 | Metrics(Metrics), | ||
76 | Bb(Bb), | ||
77 | } | ||
78 | |||
79 | #[derive(Debug)] | ||
80 | pub struct Help { | ||
81 | pub help: bool, | ||
82 | } | ||
83 | |||
84 | #[derive(Debug)] | ||
85 | pub struct Install { | ||
86 | pub client: bool, | ||
87 | pub code_bin: Option<String>, | ||
88 | pub server: bool, | ||
89 | pub mimalloc: bool, | ||
90 | pub jemalloc: bool, | ||
91 | } | ||
92 | |||
93 | #[derive(Debug)] | ||
94 | pub struct Codegen { | ||
95 | pub features: bool, | ||
96 | } | ||
97 | |||
98 | #[derive(Debug)] | ||
99 | pub struct Lint {} | ||
100 | |||
101 | #[derive(Debug)] | ||
102 | pub struct FuzzTests {} | ||
103 | |||
104 | #[derive(Debug)] | ||
105 | pub struct PreCache {} | ||
106 | |||
107 | #[derive(Debug)] | ||
108 | pub struct Release { | ||
109 | pub dry_run: bool, | ||
110 | } | ||
111 | |||
112 | #[derive(Debug)] | ||
113 | pub struct Promote { | ||
114 | pub dry_run: bool, | ||
115 | } | ||
116 | |||
117 | #[derive(Debug)] | ||
118 | pub struct Dist { | ||
119 | pub nightly: bool, | ||
120 | pub client: Option<String>, | ||
121 | } | ||
122 | |||
123 | #[derive(Debug)] | ||
124 | pub struct Metrics { | ||
125 | pub dry_run: bool, | ||
126 | } | ||
127 | |||
128 | #[derive(Debug)] | ||
129 | pub struct Bb { | ||
130 | pub suffix: String, | ||
131 | } | ||
132 | |||
133 | impl Xtask { | ||
134 | pub const HELP: &'static str = Self::_HELP; | ||
135 | |||
136 | pub fn from_env() -> xflags::Result<Self> { | ||
137 | let mut p = xflags::rt::Parser::new_from_env(); | ||
138 | Self::_parse(&mut p) | ||
139 | } | ||
140 | } | ||
141 | // generated end | ||
142 | |||
143 | impl Install { | ||
144 | pub(crate) fn validate(&self) -> xflags::Result<()> { | ||
145 | if let Some(code_bin) = &self.code_bin { | ||
146 | if let Err(err) = code_bin.parse::<ClientOpt>() { | ||
147 | return Err(xflags::Error::new(format!("failed to parse `--code-bin`: {}", err))); | ||
148 | } | ||
149 | } | ||
150 | Ok(()) | ||
151 | } | ||
152 | pub(crate) fn server(&self) -> Option<ServerOpt> { | ||
153 | if self.client && !self.server { | ||
154 | return None; | ||
155 | } | ||
156 | let malloc = if self.mimalloc { | ||
157 | Malloc::Mimalloc | ||
158 | } else if self.jemalloc { | ||
159 | Malloc::Jemalloc | ||
160 | } else { | ||
161 | Malloc::System | ||
162 | }; | ||
163 | Some(ServerOpt { malloc }) | ||
164 | } | ||
165 | pub(crate) fn client(&self) -> Option<ClientOpt> { | ||
166 | if !self.client && self.server { | ||
167 | return None; | ||
168 | } | ||
169 | let client_opt = self.code_bin.as_ref().and_then(|it| it.parse().ok()).unwrap_or_default(); | ||
170 | Some(client_opt) | ||
171 | } | ||
172 | } | ||
diff --git a/xtask/src/install.rs b/xtask/src/install.rs index 4c5c2673c..3e8fbe0a6 100644 --- a/xtask/src/install.rs +++ b/xtask/src/install.rs | |||
@@ -5,16 +5,28 @@ use std::{env, path::PathBuf, str}; | |||
5 | use anyhow::{bail, format_err, Context, Result}; | 5 | use anyhow::{bail, format_err, Context, Result}; |
6 | use xshell::{cmd, pushd}; | 6 | use xshell::{cmd, pushd}; |
7 | 7 | ||
8 | use crate::flags; | ||
9 | |||
8 | // Latest stable, feel free to send a PR if this lags behind. | 10 | // Latest stable, feel free to send a PR if this lags behind. |
9 | const REQUIRED_RUST_VERSION: u32 = 50; | 11 | const REQUIRED_RUST_VERSION: u32 = 50; |
10 | 12 | ||
11 | pub struct InstallCmd { | 13 | impl flags::Install { |
12 | pub client: Option<ClientOpt>, | 14 | pub(crate) fn run(self) -> Result<()> { |
13 | pub server: Option<ServerOpt>, | 15 | if cfg!(target_os = "macos") { |
16 | fix_path_for_mac().context("Fix path for mac")? | ||
17 | } | ||
18 | if let Some(server) = self.server() { | ||
19 | install_server(server).context("install server")?; | ||
20 | } | ||
21 | if let Some(client) = self.client() { | ||
22 | install_client(client).context("install client")?; | ||
23 | } | ||
24 | Ok(()) | ||
25 | } | ||
14 | } | 26 | } |
15 | 27 | ||
16 | #[derive(Clone, Copy)] | 28 | #[derive(Clone, Copy)] |
17 | pub enum ClientOpt { | 29 | pub(crate) enum ClientOpt { |
18 | VsCode, | 30 | VsCode, |
19 | VsCodeExploration, | 31 | VsCodeExploration, |
20 | VsCodeInsiders, | 32 | VsCodeInsiders, |
@@ -24,7 +36,7 @@ pub enum ClientOpt { | |||
24 | } | 36 | } |
25 | 37 | ||
26 | impl ClientOpt { | 38 | impl ClientOpt { |
27 | pub const fn as_cmds(&self) -> &'static [&'static str] { | 39 | pub(crate) const fn as_cmds(&self) -> &'static [&'static str] { |
28 | match self { | 40 | match self { |
29 | ClientOpt::VsCode => &["code"], | 41 | ClientOpt::VsCode => &["code"], |
30 | ClientOpt::VsCodeExploration => &["code-exploration"], | 42 | ClientOpt::VsCodeExploration => &["code-exploration"], |
@@ -60,31 +72,16 @@ impl std::str::FromStr for ClientOpt { | |||
60 | } | 72 | } |
61 | } | 73 | } |
62 | 74 | ||
63 | pub struct ServerOpt { | 75 | pub(crate) struct ServerOpt { |
64 | pub malloc: Malloc, | 76 | pub(crate) malloc: Malloc, |
65 | } | 77 | } |
66 | 78 | ||
67 | pub enum Malloc { | 79 | pub(crate) enum Malloc { |
68 | System, | 80 | System, |
69 | Mimalloc, | 81 | Mimalloc, |
70 | Jemalloc, | 82 | Jemalloc, |
71 | } | 83 | } |
72 | 84 | ||
73 | impl InstallCmd { | ||
74 | pub fn run(self) -> Result<()> { | ||
75 | if cfg!(target_os = "macos") { | ||
76 | fix_path_for_mac().context("Fix path for mac")? | ||
77 | } | ||
78 | if let Some(server) = self.server { | ||
79 | install_server(server).context("install server")?; | ||
80 | } | ||
81 | if let Some(client) = self.client { | ||
82 | install_client(client).context("install client")?; | ||
83 | } | ||
84 | Ok(()) | ||
85 | } | ||
86 | } | ||
87 | |||
88 | fn fix_path_for_mac() -> Result<()> { | 85 | fn fix_path_for_mac() -> Result<()> { |
89 | let mut vscode_path: Vec<PathBuf> = { | 86 | let mut vscode_path: Vec<PathBuf> = { |
90 | const COMMON_APP_PATH: &str = | 87 | const COMMON_APP_PATH: &str = |
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 | |||
5 | pub mod codegen; | ||
6 | mod ast_src; | ||
7 | |||
8 | pub mod install; | ||
9 | pub mod release; | ||
10 | pub mod dist; | ||
11 | pub mod pre_commit; | ||
12 | pub mod metrics; | ||
13 | pub mod pre_cache; | ||
14 | |||
15 | use std::{ | ||
16 | env, | ||
17 | path::{Path, PathBuf}, | ||
18 | }; | ||
19 | |||
20 | use walkdir::{DirEntry, WalkDir}; | ||
21 | use xshell::{cmd, pushd, pushenv}; | ||
22 | |||
23 | use crate::codegen::Mode; | ||
24 | |||
25 | pub use anyhow::{bail, Context as _, Result}; | ||
26 | |||
27 | pub 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 | |||
37 | pub fn rust_files() -> impl Iterator<Item = PathBuf> { | ||
38 | rust_files_in(&project_root().join("crates")) | ||
39 | } | ||
40 | |||
41 | pub 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 | |||
46 | pub fn rust_files_in(path: &Path) -> impl Iterator<Item = PathBuf> { | ||
47 | files_in(path, "rs") | ||
48 | } | ||
49 | |||
50 | pub 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 | |||
62 | fn 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 | |||
73 | pub 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 | |||
92 | pub 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 | |||
109 | fn date_iso() -> Result<String> { | ||
110 | let res = cmd!("date --iso --utc").read()?; | ||
111 | Ok(res) | ||
112 | } | ||
113 | |||
114 | fn is_release_tag(tag: &str) -> bool { | ||
115 | tag.len() == "2020-02-24".len() && tag.starts_with(|c: char| c.is_ascii_digit()) | ||
116 | } | ||
117 | |||
118 | fn 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..ca27b6cec 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs | |||
@@ -7,168 +7,166 @@ | |||
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`. |
10 | mod flags; | ||
10 | 11 | ||
11 | use std::env; | 12 | mod codegen; |
12 | 13 | mod ast_src; | |
13 | use anyhow::bail; | 14 | #[cfg(test)] |
14 | use codegen::CodegenCmd; | 15 | mod tidy; |
15 | use pico_args::Arguments; | ||
16 | use xshell::{cmd, cp, pushd}; | ||
17 | use xtask::{ | ||
18 | codegen::{self, Mode}, | ||
19 | dist::DistCmd, | ||
20 | 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 | }; | ||
27 | 16 | ||
28 | fn main() -> Result<()> { | 17 | mod install; |
29 | if env::args().next().map(|it| it.contains("pre-commit")) == Some(true) { | 18 | mod release; |
30 | return pre_commit::run_hook(); | 19 | mod dist; |
31 | } | 20 | mod metrics; |
21 | mod pre_cache; | ||
32 | 22 | ||
33 | let _d = pushd(project_root())?; | 23 | use anyhow::{bail, Result}; |
34 | 24 | use std::{ | |
35 | let mut args = Arguments::from_env(); | 25 | env, |
36 | let subcommand = args.subcommand()?.unwrap_or_default(); | 26 | path::{Path, PathBuf}, |
37 | 27 | }; | |
38 | match subcommand.as_str() { | 28 | use walkdir::{DirEntry, WalkDir}; |
39 | "install" => { | 29 | use xshell::{cmd, cp, pushd, pushenv}; |
40 | if args.contains(["-h", "--help"]) { | ||
41 | eprintln!( | ||
42 | "\ | ||
43 | cargo xtask install | ||
44 | Install rust-analyzer server or editor plugin. | ||
45 | |||
46 | USAGE: | ||
47 | cargo xtask install [FLAGS] | ||
48 | |||
49 | FLAGS: | ||
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!( | ||
64 | "error: The argument `--server` cannot be used with `--client`\n\n\ | ||
65 | For more information try --help" | ||
66 | ); | ||
67 | return Ok(()); | ||
68 | } | ||
69 | |||
70 | let malloc = if args.contains("--mimalloc") { | ||
71 | Malloc::Mimalloc | ||
72 | } else if args.contains("--jemalloc") { | ||
73 | Malloc::Jemalloc | ||
74 | } else { | ||
75 | Malloc::System | ||
76 | }; | ||
77 | 30 | ||
78 | let client_opt = args.opt_value_from_str("--client")?; | 31 | use crate::{codegen::Mode, dist::DistCmd}; |
79 | 32 | ||
80 | finish_args(args)?; | 33 | fn main() -> Result<()> { |
34 | let _d = pushd(project_root())?; | ||
81 | 35 | ||
82 | InstallCmd { | 36 | let flags = flags::Xtask::from_env()?; |
83 | client: if server { None } else { Some(client_opt.unwrap_or_default()) }, | 37 | match flags.subcommand { |
84 | server: if client_code { None } else { Some(ServerOpt { malloc }) }, | 38 | flags::XtaskCmd::Help(_) => { |
85 | } | 39 | println!("{}", flags::Xtask::HELP); |
86 | .run() | 40 | return Ok(()); |
87 | } | ||
88 | "codegen" => { | ||
89 | let features = args.contains("--features"); | ||
90 | finish_args(args)?; | ||
91 | CodegenCmd { features }.run() | ||
92 | } | 41 | } |
93 | "format" => { | 42 | flags::XtaskCmd::Install(cmd) => { |
94 | finish_args(args)?; | 43 | cmd.validate()?; |
95 | run_rustfmt(Mode::Overwrite) | 44 | cmd.run() |
96 | } | 45 | } |
97 | "install-pre-commit-hook" => { | 46 | flags::XtaskCmd::Codegen(cmd) => cmd.run(), |
98 | finish_args(args)?; | 47 | flags::XtaskCmd::Lint(_) => run_clippy(), |
99 | pre_commit::install_hook() | 48 | flags::XtaskCmd::FuzzTests(_) => run_fuzzer(), |
49 | flags::XtaskCmd::PreCache(cmd) => cmd.run(), | ||
50 | flags::XtaskCmd::Release(cmd) => cmd.run(), | ||
51 | flags::XtaskCmd::Promote(cmd) => cmd.run(), | ||
52 | flags::XtaskCmd::Dist(flags) => { | ||
53 | DistCmd { nightly: flags.nightly, client_version: flags.client }.run() | ||
100 | } | 54 | } |
101 | "lint" => { | 55 | flags::XtaskCmd::Metrics(cmd) => cmd.run(), |
102 | finish_args(args)?; | 56 | 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 | { | 57 | { |
138 | let _d = pushd("./crates/rust-analyzer")?; | 58 | let _d = pushd("./crates/rust-analyzer")?; |
139 | cmd!("cargo build --release --features jemalloc").run()?; | 59 | cmd!("cargo build --release --features jemalloc").run()?; |
140 | } | 60 | } |
141 | cp("./target/release/rust-analyzer", format!("./target/rust-analyzer-{}", suffix))?; | 61 | cp("./target/release/rust-analyzer", format!("./target/rust-analyzer-{}", cmd.suffix))?; |
142 | Ok(()) | ||
143 | } | ||
144 | _ => { | ||
145 | eprintln!( | ||
146 | "\ | ||
147 | cargo xtask | ||
148 | Run custom build command. | ||
149 | |||
150 | USAGE: | ||
151 | cargo xtask <SUBCOMMAND> | ||
152 | |||
153 | SUBCOMMANDS: | ||
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(()) | 62 | Ok(()) |
165 | } | 63 | } |
166 | } | 64 | } |
167 | } | 65 | } |
168 | 66 | ||
169 | fn finish_args(args: Arguments) -> Result<()> { | 67 | fn project_root() -> PathBuf { |
170 | if !args.finish().is_empty() { | 68 | Path::new( |
171 | bail!("Unused arguments."); | 69 | &env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| env!("CARGO_MANIFEST_DIR").to_owned()), |
70 | ) | ||
71 | .ancestors() | ||
72 | .nth(1) | ||
73 | .unwrap() | ||
74 | .to_path_buf() | ||
75 | } | ||
76 | |||
77 | fn rust_files() -> impl Iterator<Item = PathBuf> { | ||
78 | rust_files_in(&project_root().join("crates")) | ||
79 | } | ||
80 | |||
81 | #[cfg(test)] | ||
82 | fn cargo_files() -> impl Iterator<Item = PathBuf> { | ||
83 | files_in(&project_root(), "toml") | ||
84 | .filter(|path| path.file_name().map(|it| it == "Cargo.toml").unwrap_or(false)) | ||
85 | } | ||
86 | |||
87 | fn rust_files_in(path: &Path) -> impl Iterator<Item = PathBuf> { | ||
88 | files_in(path, "rs") | ||
89 | } | ||
90 | |||
91 | fn run_rustfmt(mode: Mode) -> Result<()> { | ||
92 | let _dir = pushd(project_root())?; | ||
93 | let _e = pushenv("RUSTUP_TOOLCHAIN", "stable"); | ||
94 | ensure_rustfmt()?; | ||
95 | let check = match mode { | ||
96 | Mode::Overwrite => &[][..], | ||
97 | Mode::Verify => &["--", "--check"], | ||
98 | }; | ||
99 | cmd!("cargo fmt {check...}").run()?; | ||
100 | Ok(()) | ||
101 | } | ||
102 | |||
103 | fn ensure_rustfmt() -> Result<()> { | ||
104 | let out = cmd!("rustfmt --version").read()?; | ||
105 | if !out.contains("stable") { | ||
106 | bail!( | ||
107 | "Failed to run rustfmt from toolchain 'stable'. \ | ||
108 | Please run `rustup component add rustfmt --toolchain stable` to install it.", | ||
109 | ) | ||
110 | } | ||
111 | Ok(()) | ||
112 | } | ||
113 | |||
114 | fn run_clippy() -> Result<()> { | ||
115 | if cmd!("cargo clippy --version").read().is_err() { | ||
116 | bail!( | ||
117 | "Failed run cargo clippy. \ | ||
118 | Please run `rustup component add clippy` to install it.", | ||
119 | ) | ||
120 | } | ||
121 | |||
122 | let allowed_lints = " | ||
123 | -A clippy::collapsible_if | ||
124 | -A clippy::needless_pass_by_value | ||
125 | -A clippy::nonminimal_bool | ||
126 | -A clippy::redundant_pattern_matching | ||
127 | " | ||
128 | .split_ascii_whitespace(); | ||
129 | cmd!("cargo clippy --all-features --all-targets -- {allowed_lints...}").run()?; | ||
130 | Ok(()) | ||
131 | } | ||
132 | |||
133 | fn run_fuzzer() -> Result<()> { | ||
134 | let _d = pushd("./crates/syntax")?; | ||
135 | let _e = pushenv("RUSTUP_TOOLCHAIN", "nightly"); | ||
136 | if cmd!("cargo fuzz --help").read().is_err() { | ||
137 | cmd!("cargo install cargo-fuzz").run()?; | ||
138 | }; | ||
139 | |||
140 | // Expecting nightly rustc | ||
141 | let out = cmd!("rustc --version").read()?; | ||
142 | if !out.contains("nightly") { | ||
143 | bail!("fuzz tests require nightly rustc") | ||
172 | } | 144 | } |
145 | |||
146 | cmd!("cargo fuzz run parser").run()?; | ||
173 | Ok(()) | 147 | Ok(()) |
174 | } | 148 | } |
149 | |||
150 | fn date_iso() -> Result<String> { | ||
151 | let res = cmd!("date --iso --utc").read()?; | ||
152 | Ok(res) | ||
153 | } | ||
154 | |||
155 | fn is_release_tag(tag: &str) -> bool { | ||
156 | tag.len() == "2020-02-24".len() && tag.starts_with(|c: char| c.is_ascii_digit()) | ||
157 | } | ||
158 | |||
159 | fn files_in(path: &Path, ext: &'static str) -> impl Iterator<Item = PathBuf> { | ||
160 | let iter = WalkDir::new(path); | ||
161 | return iter | ||
162 | .into_iter() | ||
163 | .filter_entry(|e| !is_hidden(e)) | ||
164 | .map(|e| e.unwrap()) | ||
165 | .filter(|e| !e.file_type().is_dir()) | ||
166 | .map(|e| e.into_path()) | ||
167 | .filter(move |path| path.extension().map(|it| it == ext).unwrap_or(false)); | ||
168 | |||
169 | fn is_hidden(entry: &DirEntry) -> bool { | ||
170 | entry.file_name().to_str().map(|s| s.starts_with('.')).unwrap_or(false) | ||
171 | } | ||
172 | } | ||
diff --git a/xtask/src/metrics.rs b/xtask/src/metrics.rs index 624ad3b7e..72de92c64 100644 --- a/xtask/src/metrics.rs +++ b/xtask/src/metrics.rs | |||
@@ -9,14 +9,12 @@ use std::{ | |||
9 | use anyhow::{bail, format_err, Result}; | 9 | use anyhow::{bail, format_err, Result}; |
10 | use xshell::{cmd, mkdir_p, pushd, pushenv, read_file, rm_rf}; | 10 | use xshell::{cmd, mkdir_p, pushd, pushenv, read_file, rm_rf}; |
11 | 11 | ||
12 | type Unit = String; | 12 | use crate::flags; |
13 | 13 | ||
14 | pub struct MetricsCmd { | 14 | type Unit = String; |
15 | pub dry_run: bool, | ||
16 | } | ||
17 | 15 | ||
18 | impl MetricsCmd { | 16 | impl 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")?; |
@@ -82,7 +80,7 @@ impl Metrics { | |||
82 | fn measure_analysis_stats_path(&mut self, name: &str, path: &str) -> Result<()> { | 80 | fn measure_analysis_stats_path(&mut self, name: &str, path: &str) -> Result<()> { |
83 | eprintln!("\nMeasuring analysis-stats/{}", name); | 81 | eprintln!("\nMeasuring analysis-stats/{}", name); |
84 | let output = | 82 | let output = |
85 | cmd!("./target/release/rust-analyzer analysis-stats --quiet --memory-usage {path}") | 83 | cmd!("./target/release/rust-analyzer --quiet analysis-stats --memory-usage {path}") |
86 | .read()?; | 84 | .read()?; |
87 | for (metric, value, unit) in parse_metrics(&output) { | 85 | for (metric, value, unit) in parse_metrics(&output) { |
88 | self.report(&format!("analysis-stats/{}/{}", name, metric), value, unit.into()); | 86 | self.report(&format!("analysis-stats/{}/{}", name, metric), value, unit.into()); |
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::{ | |||
6 | use anyhow::Result; | 6 | use anyhow::Result; |
7 | use xshell::rm_rf; | 7 | use xshell::rm_rf; |
8 | 8 | ||
9 | pub struct PreCacheCmd; | 9 | use crate::flags; |
10 | 10 | ||
11 | impl PreCacheCmd { | 11 | impl 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 | |||
3 | use std::{fs, path::PathBuf}; | ||
4 | |||
5 | use anyhow::{bail, Result}; | ||
6 | use xshell::cmd; | ||
7 | |||
8 | use 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`). | ||
12 | pub 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 | |||
26 | pub 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 | ||
3 | use xshell::{cmd, cp, pushd, read_dir, write_file}; | 3 | use xshell::{cmd, cp, pushd, read_dir, write_file}; |
4 | 4 | ||
5 | use crate::{codegen, date_iso, is_release_tag, project_root, Mode, Result}; | 5 | use crate::{codegen, date_iso, flags, is_release_tag, project_root, Mode, Result}; |
6 | 6 | ||
7 | pub struct ReleaseCmd { | 7 | impl flags::Release { |
8 | pub dry_run: bool, | 8 | pub(crate) fn run(self) -> Result<()> { |
9 | } | ||
10 | |||
11 | impl 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 | ||
89 | pub struct PromoteCmd { | 85 | impl flags::Promote { |
90 | pub dry_run: bool, | 86 | pub(crate) fn run(self) -> Result<()> { |
91 | } | ||
92 | |||
93 | impl 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/src/tidy.rs b/xtask/src/tidy.rs new file mode 100644 index 000000000..349ca14d0 --- /dev/null +++ b/xtask/src/tidy.rs | |||
@@ -0,0 +1,447 @@ | |||
1 | use std::{ | ||
2 | collections::HashMap, | ||
3 | path::{Path, PathBuf}, | ||
4 | }; | ||
5 | |||
6 | use xshell::{cmd, read_file}; | ||
7 | |||
8 | use crate::{ | ||
9 | cargo_files, | ||
10 | codegen::{self, Mode}, | ||
11 | project_root, run_rustfmt, rust_files, | ||
12 | }; | ||
13 | |||
14 | #[test] | ||
15 | fn generated_grammar_is_fresh() { | ||
16 | if let Err(error) = codegen::generate_syntax(Mode::Verify) { | ||
17 | panic!("{}. Please update it by running `cargo xtask codegen`", error); | ||
18 | } | ||
19 | } | ||
20 | |||
21 | #[test] | ||
22 | fn generated_tests_are_fresh() { | ||
23 | if let Err(error) = codegen::generate_parser_tests(Mode::Verify) { | ||
24 | panic!("{}. Please update tests by running `cargo xtask codegen`", error); | ||
25 | } | ||
26 | } | ||
27 | |||
28 | #[test] | ||
29 | fn generated_assists_are_fresh() { | ||
30 | if let Err(error) = codegen::generate_assists_tests(Mode::Verify) { | ||
31 | panic!("{}. Please update assists by running `cargo xtask codegen`", error); | ||
32 | } | ||
33 | } | ||
34 | |||
35 | #[test] | ||
36 | fn check_code_formatting() { | ||
37 | if let Err(error) = run_rustfmt(Mode::Verify) { | ||
38 | panic!("{}. Please format the code by running `cargo format`", error); | ||
39 | } | ||
40 | } | ||
41 | |||
42 | #[test] | ||
43 | fn smoke_test_docs_generation() { | ||
44 | // We don't commit docs to the repo, so we can just overwrite in tests. | ||
45 | codegen::generate_assists_docs(Mode::Overwrite).unwrap(); | ||
46 | codegen::generate_feature_docs(Mode::Overwrite).unwrap(); | ||
47 | codegen::generate_diagnostic_docs(Mode::Overwrite).unwrap(); | ||
48 | } | ||
49 | |||
50 | #[test] | ||
51 | fn check_lsp_extensions_docs() { | ||
52 | let expected_hash = { | ||
53 | let lsp_ext_rs = | ||
54 | read_file(project_root().join("crates/rust-analyzer/src/lsp_ext.rs")).unwrap(); | ||
55 | stable_hash(lsp_ext_rs.as_str()) | ||
56 | }; | ||
57 | |||
58 | let actual_hash = { | ||
59 | let lsp_extensions_md = | ||
60 | read_file(project_root().join("docs/dev/lsp-extensions.md")).unwrap(); | ||
61 | let text = lsp_extensions_md | ||
62 | .lines() | ||
63 | .find_map(|line| line.strip_prefix("lsp_ext.rs hash:")) | ||
64 | .unwrap() | ||
65 | .trim(); | ||
66 | u64::from_str_radix(text, 16).unwrap() | ||
67 | }; | ||
68 | |||
69 | if actual_hash != expected_hash { | ||
70 | panic!( | ||
71 | " | ||
72 | lsp_ext.rs was changed without touching lsp-extensions.md. | ||
73 | |||
74 | Expected hash: {:x} | ||
75 | Actual hash: {:x} | ||
76 | |||
77 | Please adjust docs/dev/lsp-extensions.md. | ||
78 | ", | ||
79 | expected_hash, actual_hash | ||
80 | ) | ||
81 | } | ||
82 | } | ||
83 | |||
84 | #[test] | ||
85 | fn rust_files_are_tidy() { | ||
86 | let mut tidy_docs = TidyDocs::default(); | ||
87 | for path in rust_files() { | ||
88 | let text = read_file(&path).unwrap(); | ||
89 | check_todo(&path, &text); | ||
90 | check_dbg(&path, &text); | ||
91 | check_trailing_ws(&path, &text); | ||
92 | deny_clippy(&path, &text); | ||
93 | tidy_docs.visit(&path, &text); | ||
94 | } | ||
95 | tidy_docs.finish(); | ||
96 | } | ||
97 | |||
98 | #[test] | ||
99 | fn cargo_files_are_tidy() { | ||
100 | for cargo in cargo_files() { | ||
101 | let mut section = None; | ||
102 | for (line_no, text) in read_file(&cargo).unwrap().lines().enumerate() { | ||
103 | let text = text.trim(); | ||
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 | } | ||
113 | section = Some(text); | ||
114 | continue; | ||
115 | } | ||
116 | let text: String = text.split_whitespace().collect(); | ||
117 | if !text.contains("path=") { | ||
118 | continue; | ||
119 | } | ||
120 | match section { | ||
121 | Some(s) if s.contains("dev-dependencies") => { | ||
122 | if text.contains("version") { | ||
123 | panic!( | ||
124 | "\ncargo internal dev-dependencies should not have a version.\n\ | ||
125 | {}:{}\n", | ||
126 | cargo.display(), | ||
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 | _ => {} | ||
142 | } | ||
143 | } | ||
144 | } | ||
145 | } | ||
146 | |||
147 | #[test] | ||
148 | fn check_merge_commits() { | ||
149 | let stdout = cmd!("git rev-list --merges --invert-grep --author 'bors\\[bot\\]' HEAD~19..") | ||
150 | .read() | ||
151 | .unwrap(); | ||
152 | if !stdout.is_empty() { | ||
153 | panic!( | ||
154 | " | ||
155 | Merge commits are not allowed in the history. | ||
156 | |||
157 | When updating a pull-request, please rebase your feature branch | ||
158 | on top of master by running `git rebase master`. If rebase fails, | ||
159 | you can re-apply your changes like this: | ||
160 | |||
161 | # Just look around to see the current state. | ||
162 | $ git status | ||
163 | $ git log | ||
164 | |||
165 | # Abort in-progress rebase and merges, if any. | ||
166 | $ git rebase --abort | ||
167 | $ git merge --abort | ||
168 | |||
169 | # Make the branch point to the latest commit from master, | ||
170 | # while maintaining your local changes uncommited. | ||
171 | $ git reset --soft origin/master | ||
172 | |||
173 | # Commit all changes in a single batch. | ||
174 | $ git commit -am'My changes' | ||
175 | |||
176 | # Verify that everything looks alright. | ||
177 | $ git status | ||
178 | $ git log | ||
179 | |||
180 | # Push the changes. We did a rebase, so we need `--force` option. | ||
181 | # `--force-with-lease` is a more safe (Rusty) version of `--force`. | ||
182 | $ git push --force-with-lease | ||
183 | |||
184 | # Verify that both local and remote branch point to the same commit. | ||
185 | $ git log | ||
186 | |||
187 | And don't fear to mess something up during a rebase -- you can | ||
188 | always restore the previous state using `git ref-log`: | ||
189 | |||
190 | https://github.blog/2015-06-08-how-to-undo-almost-anything-with-git/#redo-after-undo-local | ||
191 | " | ||
192 | ); | ||
193 | } | ||
194 | } | ||
195 | |||
196 | fn deny_clippy(path: &PathBuf, text: &String) { | ||
197 | let ignore = &[ | ||
198 | // The documentation in string literals may contain anything for its own purposes | ||
199 | "ide_completion/src/generated_lint_completions.rs", | ||
200 | ]; | ||
201 | if ignore.iter().any(|p| path.ends_with(p)) { | ||
202 | return; | ||
203 | } | ||
204 | |||
205 | if text.contains("\u{61}llow(clippy") { | ||
206 | panic!( | ||
207 | "\n\nallowing lints is forbidden: {}. | ||
208 | rust-analyzer intentionally doesn't check clippy on CI. | ||
209 | You can allow lint globally via `xtask clippy`. | ||
210 | See https://github.com/rust-lang/rust-clippy/issues/5537 for discussion. | ||
211 | |||
212 | ", | ||
213 | path.display() | ||
214 | ) | ||
215 | } | ||
216 | } | ||
217 | |||
218 | #[test] | ||
219 | fn check_licenses() { | ||
220 | let expected = " | ||
221 | 0BSD OR MIT OR Apache-2.0 | ||
222 | Apache-2.0 | ||
223 | Apache-2.0 OR BSL-1.0 | ||
224 | Apache-2.0 OR MIT | ||
225 | Apache-2.0/MIT | ||
226 | BSD-3-Clause | ||
227 | CC0-1.0 | ||
228 | ISC | ||
229 | MIT | ||
230 | MIT / Apache-2.0 | ||
231 | MIT OR Apache-2.0 | ||
232 | MIT OR Apache-2.0 OR Zlib | ||
233 | MIT OR Zlib OR Apache-2.0 | ||
234 | MIT/Apache-2.0 | ||
235 | Unlicense OR MIT | ||
236 | Unlicense/MIT | ||
237 | Zlib OR Apache-2.0 OR MIT | ||
238 | " | ||
239 | .lines() | ||
240 | .filter(|it| !it.is_empty()) | ||
241 | .collect::<Vec<_>>(); | ||
242 | |||
243 | let meta = cmd!("cargo metadata --format-version 1").read().unwrap(); | ||
244 | let mut licenses = meta | ||
245 | .split(|c| c == ',' || c == '{' || c == '}') | ||
246 | .filter(|it| it.contains(r#""license""#)) | ||
247 | .map(|it| it.trim()) | ||
248 | .map(|it| it[r#""license":"#.len()..].trim_matches('"')) | ||
249 | .collect::<Vec<_>>(); | ||
250 | licenses.sort(); | ||
251 | licenses.dedup(); | ||
252 | if licenses != expected { | ||
253 | let mut diff = String::new(); | ||
254 | |||
255 | diff += &format!("New Licenses:\n"); | ||
256 | for &l in licenses.iter() { | ||
257 | if !expected.contains(&l) { | ||
258 | diff += &format!(" {}\n", l) | ||
259 | } | ||
260 | } | ||
261 | |||
262 | diff += &format!("\nMissing Licenses:\n"); | ||
263 | for &l in expected.iter() { | ||
264 | if !licenses.contains(&l) { | ||
265 | diff += &format!(" {}\n", l) | ||
266 | } | ||
267 | } | ||
268 | |||
269 | panic!("different set of licenses!\n{}", diff); | ||
270 | } | ||
271 | assert_eq!(licenses, expected); | ||
272 | } | ||
273 | |||
274 | fn check_todo(path: &Path, text: &str) { | ||
275 | let need_todo = &[ | ||
276 | // This file itself obviously needs to use todo (<- like this!). | ||
277 | "tests/tidy.rs", | ||
278 | // Some of our assists generate `todo!()`. | ||
279 | "handlers/add_turbo_fish.rs", | ||
280 | "handlers/generate_function.rs", | ||
281 | // To support generating `todo!()` in assists, we have `expr_todo()` in | ||
282 | // `ast::make`. | ||
283 | "ast/make.rs", | ||
284 | // The documentation in string literals may contain anything for its own purposes | ||
285 | "ide_completion/src/generated_lint_completions.rs", | ||
286 | ]; | ||
287 | if need_todo.iter().any(|p| path.ends_with(p)) { | ||
288 | return; | ||
289 | } | ||
290 | if text.contains("TODO") || text.contains("TOOD") || text.contains("todo!") { | ||
291 | // Generated by an assist | ||
292 | if text.contains("${0:todo!()}") { | ||
293 | return; | ||
294 | } | ||
295 | |||
296 | panic!( | ||
297 | "\nTODO markers or todo! macros should not be committed to the master branch,\n\ | ||
298 | use FIXME instead\n\ | ||
299 | {}\n", | ||
300 | path.display(), | ||
301 | ) | ||
302 | } | ||
303 | } | ||
304 | |||
305 | fn check_dbg(path: &Path, text: &str) { | ||
306 | let need_dbg = &[ | ||
307 | // This file itself obviously needs to use dbg. | ||
308 | "tests/tidy.rs", | ||
309 | // Assists to remove `dbg!()` | ||
310 | "handlers/remove_dbg.rs", | ||
311 | // We have .dbg postfix | ||
312 | "ide_completion/src/completions/postfix.rs", | ||
313 | // The documentation in string literals may contain anything for its own purposes | ||
314 | "ide_completion/src/lib.rs", | ||
315 | "ide_completion/src/generated_lint_completions.rs", | ||
316 | // test for doc test for remove_dbg | ||
317 | "src/tests/generated.rs", | ||
318 | ]; | ||
319 | if need_dbg.iter().any(|p| path.ends_with(p)) { | ||
320 | return; | ||
321 | } | ||
322 | if text.contains("dbg!") { | ||
323 | panic!( | ||
324 | "\ndbg! macros should not be committed to the master branch,\n\ | ||
325 | {}\n", | ||
326 | path.display(), | ||
327 | ) | ||
328 | } | ||
329 | } | ||
330 | |||
331 | fn check_trailing_ws(path: &Path, text: &str) { | ||
332 | if is_exclude_dir(path, &["test_data"]) { | ||
333 | return; | ||
334 | } | ||
335 | for (line_number, line) in text.lines().enumerate() { | ||
336 | if line.chars().last().map(char::is_whitespace) == Some(true) { | ||
337 | panic!("Trailing whitespace in {} at line {}", path.display(), line_number) | ||
338 | } | ||
339 | } | ||
340 | } | ||
341 | |||
342 | #[derive(Default)] | ||
343 | struct TidyDocs { | ||
344 | missing_docs: Vec<String>, | ||
345 | contains_fixme: Vec<PathBuf>, | ||
346 | } | ||
347 | |||
348 | impl TidyDocs { | ||
349 | fn visit(&mut self, path: &Path, text: &str) { | ||
350 | // Test hopefully don't really need comments, and for assists we already | ||
351 | // have special comments which are source of doc tests and user docs. | ||
352 | if is_exclude_dir(path, &["tests", "test_data"]) { | ||
353 | return; | ||
354 | } | ||
355 | |||
356 | if is_exclude_file(path) { | ||
357 | return; | ||
358 | } | ||
359 | |||
360 | let first_line = match text.lines().next() { | ||
361 | Some(it) => it, | ||
362 | None => return, | ||
363 | }; | ||
364 | |||
365 | if first_line.starts_with("//!") { | ||
366 | if first_line.contains("FIXME") { | ||
367 | self.contains_fixme.push(path.to_path_buf()); | ||
368 | } | ||
369 | } else { | ||
370 | if text.contains("// Feature:") || text.contains("// Assist:") { | ||
371 | return; | ||
372 | } | ||
373 | self.missing_docs.push(path.display().to_string()); | ||
374 | } | ||
375 | |||
376 | fn is_exclude_file(d: &Path) -> bool { | ||
377 | let file_names = ["tests.rs", "famous_defs_fixture.rs"]; | ||
378 | |||
379 | d.file_name() | ||
380 | .unwrap_or_default() | ||
381 | .to_str() | ||
382 | .map(|f_n| file_names.iter().any(|name| *name == f_n)) | ||
383 | .unwrap_or(false) | ||
384 | } | ||
385 | } | ||
386 | |||
387 | fn finish(self) { | ||
388 | if !self.missing_docs.is_empty() { | ||
389 | panic!( | ||
390 | "\nMissing docs strings\n\n\ | ||
391 | modules:\n{}\n\n", | ||
392 | self.missing_docs.join("\n") | ||
393 | ) | ||
394 | } | ||
395 | |||
396 | let poorly_documented = [ | ||
397 | "hir", | ||
398 | "hir_expand", | ||
399 | "ide", | ||
400 | "mbe", | ||
401 | "parser", | ||
402 | "profile", | ||
403 | "project_model", | ||
404 | "syntax", | ||
405 | "tt", | ||
406 | "hir_ty", | ||
407 | ]; | ||
408 | |||
409 | let mut has_fixmes = | ||
410 | poorly_documented.iter().map(|it| (*it, false)).collect::<HashMap<&str, bool>>(); | ||
411 | 'outer: for path in self.contains_fixme { | ||
412 | for krate in poorly_documented.iter() { | ||
413 | if path.components().any(|it| it.as_os_str() == *krate) { | ||
414 | has_fixmes.insert(krate, true); | ||
415 | continue 'outer; | ||
416 | } | ||
417 | } | ||
418 | panic!("FIXME doc in a fully-documented crate: {}", path.display()) | ||
419 | } | ||
420 | |||
421 | for (krate, has_fixme) in has_fixmes.iter() { | ||
422 | if !has_fixme { | ||
423 | panic!("crate {} is fully documented :tada:, remove it from the list of poorly documented crates", krate) | ||
424 | } | ||
425 | } | ||
426 | } | ||
427 | } | ||
428 | |||
429 | fn is_exclude_dir(p: &Path, dirs_to_exclude: &[&str]) -> bool { | ||
430 | p.strip_prefix(project_root()) | ||
431 | .unwrap() | ||
432 | .components() | ||
433 | .rev() | ||
434 | .skip(1) | ||
435 | .filter_map(|it| it.as_os_str().to_str()) | ||
436 | .any(|it| dirs_to_exclude.contains(&it)) | ||
437 | } | ||
438 | |||
439 | #[allow(deprecated)] | ||
440 | fn stable_hash(text: &str) -> u64 { | ||
441 | use std::hash::{Hash, Hasher, SipHasher}; | ||
442 | |||
443 | let text = text.replace('\r', ""); | ||
444 | let mut hasher = SipHasher::default(); | ||
445 | text.hash(&mut hasher); | ||
446 | hasher.finish() | ||
447 | } | ||