repositories / smith

smith

There are many coding harnesses - but this one is fast

owned by bugabinga

scoped cargo x commands

6c28ef1dc2c0999d795f532a12221089dac1fa2f

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (2)

+99 / −53 text lines

Diff layout

.system/RULES.md

Modified

+1 / −0 text lines

4747- `smith-cli` unsafe only for OS terminal manipulation, with safe wrapper, `// SAFETY:`, and tests.
4848- Public docs required.
4949- fmt, clippy, tests, docs, audit, arch, and architecture-test gates in CI.
50- Every `cargo x` command accepts argument passthrough; `-p` scoping overrides workspace defaults. Lockfile-wide commands (`audit`, `bootstrap`) reject scope explicitly.
5152## Architecture

xtask/src/main.rs

Modified

+98 / −53 text lines

5353 if command.to_str() == Some("dev") {
5454 return dev(&rest);
5555 }
56 if command.to_str() == Some("mutants") {
57 return cargo_mutants(&rest);
58 }
59 if !rest.is_empty() {
60 return fail("xtask accepts exactly one command\n");
61 }
6256 match command.to_str() {
63 Some("check") => check(),
64 Some("lint") => lint(),
65 Some("fmt") => cargo(&["fmt"]),
66 Some("test") => cargo_nextest(),
67 Some("doc") => cargo(&["doc", "--workspace", "--no-deps"]),
68 Some("arch") => arch(),
69 Some("audit") => audit(),
70 Some("bootstrap") => bootstrap(),
57 Some("check") => check(&rest),
58 Some("lint") => lint(&rest),
59 Some("fmt") => cargo_fmt_step(&rest),
60 Some("test") => cargo_nextest(&rest),
61 Some("doc") => cargo_doc(&rest),
62 Some("arch") => arch(&rest),
63 Some("mutants") => cargo_mutants(&rest),
64 Some("audit") => audit(&rest),
65 Some("bootstrap") => bootstrap(&rest),
7166 _ => fail(HELP),
7267 }
75fn check() -> ExitCode {
76 sequence(&[
77 ensure_mold,
78 cargo_fmt,
79 cargo_clippy,
80 arch,
81 cargo_nextest,
82 cargo_doc,
83 cargo_machete,
84 ])
70fn check(extra: &[OsString]) -> ExitCode {
71 sequence(
72 &[
73 ensure_mold,
74 cargo_fmt_step,
75 cargo_clippy,
76 arch,
77 cargo_nextest,
78 cargo_doc,
79 cargo_machete,
80 ],
81 extra,
82 )
87type Step = fn() -> ExitCode;
85type Step = fn(&[OsString]) -> ExitCode;
89fn sequence(steps: &[Step]) -> ExitCode {
87fn sequence(steps: &[Step], extra: &[OsString]) -> ExitCode {
9088 for step in steps {
91 let code = step();
89 let code = step(extra);
9290 if code != ExitCode::SUCCESS {
9391 return code;
9492 }
9694 ExitCode::SUCCESS
99fn lint() -> ExitCode {
100 sequence(&[cargo_fmt, cargo_clippy])
97/// Append passthrough args; package scoping overrides workspace defaults,
98/// so `-p crate` drops the `--workspace` flag rather than conflicting.
99fn scoped(base: &[&str], extra: &[OsString]) -> Vec<OsString> {
100 let mut args: Vec<OsString> = base.iter().map(OsString::from).collect();
101 let narrows = extra.iter().any(|arg| {
102 arg == "-p" || arg == "--package" || arg.to_string_lossy().starts_with("--package=")
103 });
104 if narrows {
105 args.retain(|arg| arg != "--workspace");
106 }
107 args.extend_from_slice(extra);
108 args
103fn audit() -> ExitCode {
104 sequence(&[install_deny, install_audit, cargo_deny, cargo_audit])
111fn lint(extra: &[OsString]) -> ExitCode {
112 sequence(&[cargo_fmt_step, cargo_clippy], extra)
113}
115/// Audit gates read the lockfile; scoping does not apply.
116fn audit(extra: &[OsString]) -> ExitCode {
117 if !extra.is_empty() {
118 return fail("audit is lockfile-wide and admits no scoping\n");
119 }
120 sequence(
121 &[install_deny, install_audit, cargo_deny, cargo_audit],
122 extra,
123 )
107126/// Mutation testing runs on demand, never in the blocking check chain.
158177 }
161fn cargo_fmt() -> ExitCode {
162 cargo(&["fmt", "--check"])
180fn cargo_fmt_step(extra: &[OsString]) -> ExitCode {
181 let args = scoped(&["fmt", "--check"], extra);
182 run_os("cargo", &args)
165fn cargo_clippy() -> ExitCode {
185fn cargo_clippy(extra: &[OsString]) -> ExitCode {
186 let args = scoped(
187 &["clippy", "--workspace", "--all-targets", "--all-features"],
188 extra,
189 );
166190 match Command::new("cargo")
167191 .env("CLIPPY_CONF_DIR", ".config")
168 .args(["clippy", "--workspace", "--all-targets", "--all-features"])
192 .args(&args)
169193 .status()
170194 {
171195 Ok(status) if status.success() => ExitCode::SUCCESS,
174198 }
177fn cargo_nextest() -> ExitCode {
178 nextest_run(&["--workspace", "--all-features"])
201fn cargo_nextest(extra: &[OsString]) -> ExitCode {
202 let args = scoped(&["--workspace", "--all-features"], extra);
203 nextest_run_os(&args)
181206/// nextest has no warning-deny flag, so config-drift warnings are turned into
182207/// a gate failure by scanning its stderr.
183fn nextest_run(args: &[&str]) -> ExitCode {
208fn nextest_run_os(args: &[OsString]) -> ExitCode {
184209 let mut child = match Command::new("cargo")
185210 .arg("nextest")
186211 .arg("run")
216241 }
219fn cargo_machete() -> ExitCode {
244fn cargo_machete(_extra: &[OsString]) -> ExitCode {
220245 let installed = tool_present_bin("cargo-machete", MACHETE_VERSION, || {
221246 cargo(&[
222247 "install",
235260/// mold is a build prerequisite on linux-gnu `x86_64`; xtask verifies it, the
236261/// system package manager installs it.
237fn ensure_mold() -> ExitCode {
262fn ensure_mold(_extra: &[OsString]) -> ExitCode {
238263 if cfg!(all(
239264 target_arch = "x86_64",
240265 target_os = "linux",
260285///
261286/// `cargo-nextest` arrives as a prebuilt binary through `cargo-binstall`.
262287/// The step is idempotent.
263fn bootstrap() -> ExitCode {
288/// Installs tools; scoping does not apply.
289fn bootstrap(extra: &[OsString]) -> ExitCode {
290 if !extra.is_empty() {
291 return fail("bootstrap installs tools and admits no scoping\n");
292 }
264293 let steps: [Step; 6] = [
265294 install_binstall,
266295 install_nextest,
269298 install_audit,
270299 install_mutants,
271300 ];
272 sequence(&steps)
301 sequence(&steps, extra)
275fn install_machete() -> ExitCode {
304fn install_machete(_extra: &[OsString]) -> ExitCode {
276305 tool_present_bin("cargo-machete", MACHETE_VERSION, || {
277306 cargo(&[
278307 "install",
284313 })
287fn install_deny() -> ExitCode {
316fn install_deny(_extra: &[OsString]) -> ExitCode {
288317 tool_present_bin("cargo-deny", DENY_VERSION, || {
289318 cargo(&[
290319 "binstall",
294323 })
297fn install_audit() -> ExitCode {
326fn install_audit(_extra: &[OsString]) -> ExitCode {
298327 tool_present_bin("cargo-audit", AUDIT_VERSION, || {
299328 cargo(&[
300329 "binstall",
304333 })
307fn install_mutants() -> ExitCode {
336fn install_mutants(_extra: &[OsString]) -> ExitCode {
308337 tool_present_bin("cargo-mutants", MUTANTS_VERSION, || {
309338 cargo(&[
310339 "binstall",
314343 })
317fn install_binstall() -> ExitCode {
346fn install_binstall(_extra: &[OsString]) -> ExitCode {
318347 tool_present_bin("cargo-binstall", BINSTALL_VERSION, || {
319348 cargo(&[
320349 "install",
328357 })
331fn install_nextest() -> ExitCode {
360fn install_nextest(_extra: &[OsString]) -> ExitCode {
332361 tool_present_bin("cargo-nextest", NEXTEST_VERSION, || {
333362 cargo(&[
334363 "binstall",
354383 if pinned { ExitCode::SUCCESS } else { install() }
357fn cargo_doc() -> ExitCode {
358 cargo(&["doc", "--workspace", "--no-deps"])
386fn cargo_doc(extra: &[OsString]) -> ExitCode {
387 let args = scoped(&["doc", "--workspace", "--no-deps"], extra);
388 run_os("cargo", &args)
361fn cargo_deny() -> ExitCode {
391fn cargo_deny(_extra: &[OsString]) -> ExitCode {
362392 cargo(&["deny", "--config", ".config/deny.toml", "check"])
365fn cargo_audit() -> ExitCode {
395fn cargo_audit(_extra: &[OsString]) -> ExitCode {
366396 cargo(&["audit", "--deny", "warnings"])
428458 let _ignored = std::io::stderr().write_all(message.as_bytes());
461fn run_os(program: &str, args: &[OsString]) -> ExitCode {
462 match Command::new(program).args(args).status() {
463 Ok(status) if status.success() => ExitCode::SUCCESS,
464 Ok(status) => fail(&format!("{program} exited with {status}\n")),
465 Err(error) => fail(&format!("failed to run {program}: {error}\n")),
466 }
467}
431469fn run(program: &str, args: &[&str]) -> ExitCode {
432470 match Command::new(program).args(args).status() {
433471 Ok(status) if status.success() => ExitCode::SUCCESS,
436474 }
439fn arch() -> ExitCode {
477fn arch(extra: &[OsString]) -> ExitCode {
478 let wanted: Vec<String> = extra
479 .iter()
480 .map(|arg| arg.to_string_lossy().into_owned())
481 .collect();
440482 for crate_name in CRATES {
441483 if !Path::new(crate_name).join("Cargo.toml").is_file() {
442484 return fail(&format!("missing crate manifest: {crate_name}\n"));
443485 }
444486 }
445487 for rule in dependency_rules() {
488 if !wanted.is_empty() && !wanted.iter().any(|name| name == rule.crate_name) {
489 continue;
490 }
446491 let manifest = match fs::read_to_string(Path::new(rule.crate_name).join("Cargo.toml")) {
447492 Ok(value) => value,
448493 Err(error) => {