diff options
Diffstat (limited to 'xtask/src/lib.rs')
-rw-r--r-- | xtask/src/lib.rs | 131 |
1 files changed, 0 insertions, 131 deletions
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 | } | ||