Debian Codegraph @

crates/xtask/src/main.rs

raw
1 //! Helper for local development.
2
3 use std::collections::BTreeMap;
4 use std::error::Error;
5 use std::io::Write;
6 use std::process::exit;
7 use std::process::{Command, Stdio};
8
9 mod changelog;
10
11 type Result<T> = std::result::Result<T, Box<dyn Error>>;
12
13 fn main() -> Result<()> {
14 macro_rules! commands {
15 ($($name:literal => $func:expr),* $(,)?) => {
16 [$(($name, $func as fn() -> Result<()>)),*]
17 };
18 }
19
20 let cmds: BTreeMap<&'static str, fn() -> Result<()>> = commands! {
21 "test-all" => test_all,
22 "test-workspace" => test_workspace,
23 "clippy" => clippy,
24 "doc" => doc,
25 "fmt" => fmt,
26 "semver-checks" => semver_checks,
27 "eslint" => eslint,
28 "gui" => gui,
29 "changelog" => changelog::changelog,
30 }
31 .into_iter()
32 .collect();
33 let keys = cmds.keys().copied().collect::<Vec<_>>().join(", ");
34 let mut args = std::env::args().skip(1).peekable();
35 if args.peek().is_none() {
36 eprintln!("error: specify a command (valid options: {keys})");
37 exit(1);
38 }
39 while let Some(arg) = args.next() {
40 if let Some(cmd_fn) = cmds.get(arg.as_str()) {
41 cmd_fn()?;
42 } else if arg == "bump" {
43 let bump_arg = args
44 .next()
45 .expect("the next argument should be one of major, minor, patch, rc, beta, alpha");
46 bump(&bump_arg)?;
47 } else if matches!(arg.as_str(), "-h" | "--help") {
48 println!("valid options: {keys}");
49 exit(0)
50 } else {
51 eprintln!("error: unknown command `{arg}` (valid options: {keys}");
52 exit(1);
53 }
54 }
55 println!("success!");
56 Ok(())
57 }
58
59 fn test_all() -> Result<()> {
60 test_workspace()?;
61 clippy()?;
62 doc()?;
63 fmt()?;
64 semver_checks()?;
65 eslint()?;
66 gui()?;
67 Ok(())
68 }
69
70 fn cargo(args: &str, cb: &dyn Fn(&mut Command)) -> Result<()> {
71 println!("Running `cargo {args}`");
72 let mut cmd = Command::new("cargo");
73 cmd.args(args.split_whitespace());
74 cb(&mut cmd);
75 let status = cmd.status().expect("cargo should be installed");
76 if !status.success() {
77 return Err(format!("command `cargo {args}` failed").into());
78 }
79 Ok(())
80 }
81
82 fn test_workspace() -> Result<()> {
83 cargo("test --workspace", &|_| {})?;
84 cargo("test --workspace --no-default-features", &|_| {})?;
85 Ok(())
86 }
87
88 fn clippy() -> Result<()> {
89 cargo(
90 "clippy --workspace --all-targets --no-deps -- -D warnings",
91 &|_| {},
92 )?;
93 Ok(())
94 }
95
96 fn doc() -> Result<()> {
97 cargo(
98 "doc --workspace --document-private-items --no-deps",
99 &|cmd| {
100 cmd.env("RUSTDOCFLAGS", "-D warnings");
101 },
102 )?;
103 Ok(())
104 }
105
106 fn fmt() -> Result<()> {
107 cargo("fmt --check", &|_| {})?;
108 Ok(())
109 }
110
111 fn semver_checks() -> Result<()> {
112 cargo("+stable semver-checks --workspace", &|_| {})?;
113 Ok(())
114 }
115
116 fn gui() -> Result<()> {
117 cargo("test --test gui", &|_| {})?;
118 Ok(())
119 }
120
121 fn eslint() -> Result<()> {
122 println!("Running `npm run lint`");
123 let status = Command::new("npm")
124 .args(["run", "lint"])
125 .status()
126 .expect("npm should be installed");
127 if !status.success() {
128 return Err("eslint failed".into());
129 }
130 Ok(())
131 }
132
133 fn bump(bump: &str) -> Result<()> {
134 // Grab all the publishable crate names.
135 let metadata = Command::new("cargo")
136 .args(["metadata", "--format-version=1", "--no-deps"])
137 .output()?;
138 let mut jq = Command::new("jq")
139 .args(["-r", ".packages[] | select(.publish == null) | .name"])
140 .stdin(Stdio::piped())
141 .stdout(Stdio::piped())
142 .spawn()?;
143 jq.stdin.as_mut().unwrap().write_all(&metadata.stdout)?;
144 let jq_out = jq.wait_with_output()?;
145 if !jq_out.status.success() {
146 eprintln!("jq failed");
147 exit(1);
148 }
149 let names = std::str::from_utf8(&jq_out.stdout).unwrap();
150 let mut names: Vec<_> = names.split_whitespace().collect();
151 for i in (0..names.len()).rev() {
152 names.insert(i, "-p");
153 }
154
155 let status = Command::new("cargo")
156 .args(["set-version", "--bump"])
157 .arg(bump)
158 .args(names)
159 .status()?;
160 if !status.success() {
161 eprintln!("cargo set-version failed");
162 exit(1);
163 }
164 Ok(())
165 }