repositories / smith
smith
There are many coding harnesses - but this one is fast
owned by bugabinga
xtask/src/main.rs
RawFile outline
- HELP line 8
- CRATES line 9
- main line 19
- check line 41
- Step line 52
- sequence line 54
- lint line 64
- audit line 68
- cargo_fmt line 72
- cargo_clippy line 76
- cargo_nextest line 80
- cargo_pup line 84
- cargo_doc line 88
- cargo_deny line 92
- cargo_audit line 96
- cargo line 100
- run line 104
- arch line 112
- DependencyRule line 140
- dependency_rules line 145
- has_dependency line 187
- fail line 196
- tests line 201
- detects_inline_and_table_dependencies line 205
- ignores_non_dependency_mentions line 217
#![forbid(unsafe_code)]
#![warn(missing_docs)]
//! Cargo-only Smith project automation.
use std::{env, fs, io::Write, path::Path, process::Command, process::ExitCode};
const HELP: &str = "usage: cargo run -p xtask -- <check|lint|fmt|test|doc|arch|pup|audit>\n";
const CRATES: &[&str] = &[
"smith",
"smith-core",
"smith-ai",
"smith-tui",
"smith-harness",
"smith-cli",
"xtask",
];
fn main() -> ExitCode {
let mut args = env::args_os();
let _program = args.next();
let Some(command) = args.next() else {
return fail(HELP);
};
if args.next().is_some() {
return fail("xtask accepts exactly one command\n");
}
match command.to_str() {
Some("check") => check(),
Some("lint") => lint(),
Some("fmt") => cargo(&["fmt"]),
Some("test") => cargo_nextest(),
Some("doc") => cargo(&["doc", "--workspace", "--no-deps"]),
Some("arch") => arch(),
Some("pup") => cargo_pup(),
Some("audit") => audit(),
_ => fail(HELP),
}
}
fn check() -> ExitCode {
sequence(&[
cargo_fmt,
cargo_clippy,
arch,
cargo_pup,
cargo_nextest,
cargo_doc,
])
}
type Step = fn() -> ExitCode;
fn sequence(steps: &[Step]) -> ExitCode {
for step in steps {
let code = step();
if code != ExitCode::SUCCESS {
return code;
}
}
ExitCode::SUCCESS
}
fn lint() -> ExitCode {
sequence(&[cargo_fmt, cargo_clippy])
}
fn audit() -> ExitCode {
sequence(&[cargo_deny, cargo_audit])
}
fn cargo_fmt() -> ExitCode {
cargo(&["fmt", "--check"])
}
fn cargo_clippy() -> ExitCode {
cargo(&["clippy", "--workspace", "--all-targets", "--all-features"])
}
fn cargo_nextest() -> ExitCode {
cargo(&["nextest", "run", "--workspace", "--all-features"])
}
fn cargo_pup() -> ExitCode {
cargo(&["+nightly-2026-01-22", "pup"])
}
fn cargo_doc() -> ExitCode {
cargo(&["doc", "--workspace", "--no-deps"])
}
fn cargo_deny() -> ExitCode {
cargo(&["deny", "check"])
}
fn cargo_audit() -> ExitCode {
cargo(&["audit", "--deny", "warnings"])
}
fn cargo(args: &[&str]) -> ExitCode {
run("cargo", args)
}
fn run(program: &str, args: &[&str]) -> ExitCode {
match Command::new(program).args(args).status() {
Ok(status) if status.success() => ExitCode::SUCCESS,
Ok(status) => fail(&format!("{program} exited with {status}\n")),
Err(error) => fail(&format!("failed to run {program}: {error}\n")),
}
}
fn arch() -> ExitCode {
for crate_name in CRATES {
if !Path::new(crate_name).join("Cargo.toml").is_file() {
return fail(&format!("missing crate manifest: {crate_name}\n"));
}
}
for rule in dependency_rules() {
let manifest = match fs::read_to_string(Path::new(rule.crate_name).join("Cargo.toml")) {
Ok(value) => value,
Err(error) => {
return fail(&format!(
"failed to read {} Cargo.toml: {error}\n",
rule.crate_name
));
}
};
for denied in rule.denied {
if has_dependency(&manifest, denied) {
return fail(&format!(
"{} must not depend on {denied}\n",
rule.crate_name
));
}
}
}
ExitCode::SUCCESS
}
struct DependencyRule {
crate_name: &'static str,
denied: &'static [&'static str],
}
const fn dependency_rules() -> &'static [DependencyRule] {
&[
DependencyRule {
crate_name: "smith",
denied: &[
"smith-core",
"smith-ai",
"smith-tui",
"smith-harness",
"smith-cli",
],
},
DependencyRule {
crate_name: "smith-core",
denied: &["smith-ai", "smith-tui", "smith-harness", "smith-cli"],
},
DependencyRule {
crate_name: "smith-ai",
denied: &["smith-core", "smith-tui", "smith-harness", "smith-cli"],
},
DependencyRule {
crate_name: "smith-tui",
denied: &["smith-core", "smith-ai", "smith-harness", "smith-cli"],
},
DependencyRule {
crate_name: "smith-cli",
denied: &["smith-core", "smith-ai", "smith-tui"],
},
DependencyRule {
crate_name: "xtask",
denied: &[
"smith",
"smith-core",
"smith-ai",
"smith-tui",
"smith-harness",
"smith-cli",
],
},
]
}
fn has_dependency(manifest: &str, crate_name: &str) -> bool {
let table = format!("[dependencies.{crate_name}]");
let assignment = format!("{crate_name} =");
manifest.lines().any(|line| {
let trimmed = line.trim_start();
trimmed.starts_with(&table) || trimmed.starts_with(&assignment)
})
}
fn fail(message: &str) -> ExitCode {
let _ignored = std::io::stderr().write_all(message.as_bytes());
ExitCode::FAILURE
}
#[cfg(test)]
mod tests {
use super::has_dependency;
#[test]
fn detects_inline_and_table_dependencies() {
assert!(has_dependency(
"smith-core = { path = \"../smith-core\" }",
"smith-core"
));
assert!(has_dependency(
"[dependencies.smith-ai]\npath = \"../smith-ai\"",
"smith-ai"
));
}
#[test]
fn ignores_non_dependency_mentions() {
assert!(!has_dependency("# smith-core = forbidden", "smith-core"));
assert!(!has_dependency("smith_core = \"0.1\"", "smith-core"));
}
}