diff options
Diffstat (limited to 'xtask/src/lib.rs')
-rw-r--r-- | xtask/src/lib.rs | 89 |
1 files changed, 36 insertions, 53 deletions
diff --git a/xtask/src/lib.rs b/xtask/src/lib.rs index ec824a518..2b7a461e5 100644 --- a/xtask/src/lib.rs +++ b/xtask/src/lib.rs | |||
@@ -10,23 +10,19 @@ pub mod pre_commit; | |||
10 | pub mod codegen; | 10 | pub mod codegen; |
11 | mod ast_src; | 11 | mod ast_src; |
12 | 12 | ||
13 | use anyhow::Context; | ||
14 | use std::{ | 13 | use std::{ |
15 | env, | 14 | env, |
16 | io::Write, | ||
17 | path::{Path, PathBuf}, | 15 | path::{Path, PathBuf}, |
18 | process::{Command, Stdio}, | ||
19 | }; | 16 | }; |
17 | |||
20 | use walkdir::{DirEntry, WalkDir}; | 18 | use walkdir::{DirEntry, WalkDir}; |
21 | 19 | ||
22 | use crate::{ | 20 | use crate::{ |
23 | codegen::Mode, | 21 | codegen::Mode, |
24 | not_bash::{date_iso, fs2, pushd, rm_rf, run}, | 22 | not_bash::{date_iso, fs2, pushd, pushenv, rm_rf, run}, |
25 | }; | 23 | }; |
26 | 24 | ||
27 | pub use anyhow::Result; | 25 | pub use anyhow::{bail, Context as _, Result}; |
28 | |||
29 | const TOOLCHAIN: &str = "stable"; | ||
30 | 26 | ||
31 | pub fn project_root() -> PathBuf { | 27 | pub fn project_root() -> PathBuf { |
32 | Path::new( | 28 | Path::new( |
@@ -55,54 +51,44 @@ pub fn rust_files(path: &Path) -> impl Iterator<Item = PathBuf> { | |||
55 | 51 | ||
56 | pub fn run_rustfmt(mode: Mode) -> Result<()> { | 52 | pub fn run_rustfmt(mode: Mode) -> Result<()> { |
57 | let _dir = pushd(project_root()); | 53 | let _dir = pushd(project_root()); |
54 | let _e = pushenv("RUSTUP_TOOLCHAIN", "stable"); | ||
58 | ensure_rustfmt()?; | 55 | ensure_rustfmt()?; |
59 | 56 | match mode { | |
60 | let check = if mode == Mode::Verify { "--check" } else { "" }; | 57 | Mode::Overwrite => run!("cargo fmt"), |
61 | run!("rustup run {} -- cargo fmt -- {}", TOOLCHAIN, check)?; | 58 | Mode::Verify => run!("cargo fmt -- --check"), |
59 | }?; | ||
62 | Ok(()) | 60 | Ok(()) |
63 | } | 61 | } |
64 | 62 | ||
65 | fn reformat(text: impl std::fmt::Display) -> Result<String> { | 63 | fn reformat(text: impl std::fmt::Display) -> Result<String> { |
64 | let _e = pushenv("RUSTUP_TOOLCHAIN", "stable"); | ||
66 | ensure_rustfmt()?; | 65 | ensure_rustfmt()?; |
67 | let mut rustfmt = Command::new("rustup") | 66 | let stdout = run!( |
68 | .args(&["run", TOOLCHAIN, "--", "rustfmt", "--config-path"]) | 67 | "rustfmt --config-path {} --config fn_single_line=true", project_root().join("rustfmt.toml").display(); |
69 | .arg(project_root().join("rustfmt.toml")) | 68 | <text.to_string().as_bytes() |
70 | .args(&["--config", "fn_single_line=true"]) | 69 | )?; |
71 | .stdin(Stdio::piped()) | ||
72 | .stdout(Stdio::piped()) | ||
73 | .spawn()?; | ||
74 | write!(rustfmt.stdin.take().unwrap(), "{}", text)?; | ||
75 | let output = rustfmt.wait_with_output()?; | ||
76 | let stdout = String::from_utf8(output.stdout)?; | ||
77 | let preamble = "Generated file, do not edit by hand, see `xtask/src/codegen`"; | 70 | let preamble = "Generated file, do not edit by hand, see `xtask/src/codegen`"; |
78 | Ok(format!("//! {}\n\n{}", preamble, stdout)) | 71 | Ok(format!("//! {}\n\n{}\n", preamble, stdout)) |
79 | } | 72 | } |
80 | 73 | ||
81 | fn ensure_rustfmt() -> Result<()> { | 74 | fn ensure_rustfmt() -> Result<()> { |
82 | match Command::new("rustup") | 75 | let out = run!("rustfmt --version")?; |
83 | .args(&["run", TOOLCHAIN, "--", "cargo", "fmt", "--version"]) | 76 | if !out.contains("stable") { |
84 | .stderr(Stdio::null()) | 77 | bail!( |
85 | .stdout(Stdio::null()) | 78 | "Failed to run rustfmt from toolchain 'stable'. \ |
86 | .status() | 79 | Please run `rustup component add rustfmt --toolchain stable` to install it.", |
87 | { | 80 | ) |
88 | Ok(status) if status.success() => return Ok(()), | 81 | } |
89 | _ => (), | ||
90 | }; | ||
91 | run!("rustup toolchain install {}", TOOLCHAIN)?; | ||
92 | run!("rustup component add rustfmt --toolchain {}", TOOLCHAIN)?; | ||
93 | Ok(()) | 82 | Ok(()) |
94 | } | 83 | } |
95 | 84 | ||
96 | pub fn run_clippy() -> Result<()> { | 85 | pub fn run_clippy() -> Result<()> { |
97 | match Command::new("rustup") | 86 | if run!("cargo clippy --version").is_err() { |
98 | .args(&["run", TOOLCHAIN, "--", "cargo", "clippy", "--version"]) | 87 | bail!( |
99 | .stderr(Stdio::null()) | 88 | "Failed run cargo clippy. \ |
100 | .stdout(Stdio::null()) | 89 | Please run `rustup component add clippy` to install it.", |
101 | .status() | 90 | ) |
102 | { | 91 | } |
103 | Ok(status) if status.success() => (), | ||
104 | _ => install_clippy().context("install clippy")?, | ||
105 | }; | ||
106 | 92 | ||
107 | let allowed_lints = [ | 93 | let allowed_lints = [ |
108 | "clippy::collapsible_if", | 94 | "clippy::collapsible_if", |
@@ -110,27 +96,24 @@ pub fn run_clippy() -> Result<()> { | |||
110 | "clippy::nonminimal_bool", | 96 | "clippy::nonminimal_bool", |
111 | "clippy::redundant_pattern_matching", | 97 | "clippy::redundant_pattern_matching", |
112 | ]; | 98 | ]; |
113 | run!( | 99 | run!("cargo clippy --all-features --all-targets -- -A {}", allowed_lints.join(" -A "))?; |
114 | "rustup run {} -- cargo clippy --all-features --all-targets -- -A {}", | ||
115 | TOOLCHAIN, | ||
116 | allowed_lints.join(" -A ") | ||
117 | )?; | ||
118 | Ok(()) | ||
119 | } | ||
120 | |||
121 | fn install_clippy() -> Result<()> { | ||
122 | run!("rustup toolchain install {}", TOOLCHAIN)?; | ||
123 | run!("rustup component add clippy --toolchain {}", TOOLCHAIN)?; | ||
124 | Ok(()) | 100 | Ok(()) |
125 | } | 101 | } |
126 | 102 | ||
127 | pub fn run_fuzzer() -> Result<()> { | 103 | pub fn run_fuzzer() -> Result<()> { |
128 | let _d = pushd("./crates/ra_syntax"); | 104 | let _d = pushd("./crates/ra_syntax"); |
105 | let _e = pushenv("RUSTUP_TOOLCHAIN", "nightly"); | ||
129 | if run!("cargo fuzz --help").is_err() { | 106 | if run!("cargo fuzz --help").is_err() { |
130 | run!("cargo install cargo-fuzz")?; | 107 | run!("cargo install cargo-fuzz")?; |
131 | }; | 108 | }; |
132 | 109 | ||
133 | run!("rustup run nightly -- cargo fuzz run parser")?; | 110 | // Expecting nightly rustc |
111 | let out = run!("rustc --version")?; | ||
112 | if !out.contains("nightly") { | ||
113 | bail!("fuzz tests require nightly rustc") | ||
114 | } | ||
115 | |||
116 | run!("cargo fuzz run parser")?; | ||
134 | Ok(()) | 117 | Ok(()) |
135 | } | 118 | } |
136 | 119 | ||