repositories / smith

smith

There are many coding harnesses - but this one is fast

owned by bugabinga

tooling: launch the dev CLI with cargo x dev

f8d61b5e074fb8c3025e1d7af23da2f686d9ae58

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Builds smith-cli and runs it against an isolated XDG home under
target/dev-home, so trying the binary cannot touch the real user profile.

Changed files (1)

+103 / −4 text lines

Diff layout

xtask/src/main.rs

Modified

+103 / −4 text lines

44//! Cargo-only Smith project automation.
6use std::{env, fs, io::Write, path::Path, process::Command, process::ExitCode};
6use std::{
7 env,
8 ffi::OsString,
9 fs,
10 io::Write,
11 path::{Path, PathBuf},
12 process::Command,
13 process::ExitCode,
14};
8const HELP: &str = "usage: cargo run -p xtask -- <check|lint|fmt|test|doc|arch|pup|audit>\n";
16const HELP: &str = concat!(
17 "usage: cargo x <check|lint|fmt|test|doc|arch|pup|audit>\n",
18 " cargo x dev [args...] build and launch the dev smith binary\n",
19);
920const CRATES: &[&str] = &[
1021 "smith",
1122 "smith-core",
2233 let Some(command) = args.next() else {
2334 return fail(HELP);
2435 };
25 if args.next().is_some() {
36 let rest: Vec<OsString> = args.collect();
37 if command.to_str() == Some("dev") {
38 return dev(&rest);
39 }
40 if !rest.is_empty() {
2641 return fail("xtask accepts exactly one command\n");
2742 }
2843 match command.to_str() {
101116 run("cargo", args)
119/// Builds the CLI, then launches it against an isolated dev home.
120fn dev(args: &[OsString]) -> ExitCode {
121 let code = cargo(&["build", "-p", "smith-cli", "--bin", "smith"]);
122 if code != ExitCode::SUCCESS {
123 return code;
124 }
125 let target = target_dir();
126 let binary = target.join("debug").join(binary_name());
127 if !binary.is_file() {
128 return fail(&format!("missing dev binary: {}\n", binary.display()));
129 }
130 let home = target.join("dev-home");
131 let mut command = Command::new(&binary);
132 for (key, dir) in dev_home_dirs() {
133 let path = home.join(dir);
134 if let Err(error) = fs::create_dir_all(&path) {
135 return fail(&format!("failed to create {}: {error}\n", path.display()));
136 }
137 command.env(key, &path);
138 }
139 command.args(args);
140 note(&format!(
141 "running {} (home {})\n",
142 binary.display(),
143 home.display()
144 ));
145 match command.status() {
146 Ok(status) => match status.code() {
147 Some(0) => ExitCode::SUCCESS,
148 Some(value) => ExitCode::from(u8::try_from(value).unwrap_or(1)),
149 None => fail("smith terminated by signal\n"),
150 },
151 Err(error) => fail(&format!("failed to run {}: {error}\n", binary.display())),
152 }
153}
155/// Environment overrides that keep dev runs out of the real user profile.
156const fn dev_home_dirs() -> &'static [(&'static str, &'static str)] {
157 &[
158 ("XDG_CONFIG_HOME", "config"),
159 ("XDG_DATA_HOME", "data"),
160 ("XDG_STATE_HOME", "state"),
161 ("XDG_CACHE_HOME", "cache"),
162 ]
163}
165fn target_dir() -> PathBuf {
166 env::var_os("CARGO_TARGET_DIR").map_or_else(|| PathBuf::from("target"), PathBuf::from)
167}
169const fn binary_name() -> &'static str {
170 if cfg!(windows) { "smith.exe" } else { "smith" }
171}
173fn note(message: &str) {
174 let _ignored = std::io::stderr().write_all(message.as_bytes());
175}
104177fn run(program: &str, args: &[&str]) -> ExitCode {
105178 match Command::new(program).args(args).status() {
106179 Ok(status) if status.success() => ExitCode::SUCCESS,
201274#[cfg(test)]
202275mod tests {
203 use super::has_dependency;
276 use super::{binary_name, dev_home_dirs, has_dependency, target_dir};
277 use std::path::PathBuf;
279 #[test]
280 fn dev_home_covers_all_xdg_roots() {
281 let keys: Vec<&str> = dev_home_dirs().iter().map(|(key, _)| *key).collect();
282 assert_eq!(
283 keys,
284 vec![
285 "XDG_CONFIG_HOME",
286 "XDG_DATA_HOME",
287 "XDG_STATE_HOME",
288 "XDG_CACHE_HOME"
289 ]
290 );
291 }
293 #[test]
294 fn dev_binary_path_is_platform_correct() {
295 let path = target_dir().join("debug").join(binary_name());
296 assert!(path.ends_with(if cfg!(windows) { "smith.exe" } else { "smith" }));
297 assert!(path.starts_with(env_target()));
298 }
300 fn env_target() -> PathBuf {
301 std::env::var_os("CARGO_TARGET_DIR").map_or_else(|| PathBuf::from("target"), PathBuf::from)
302 }
205304 #[test]
206305 fn detects_inline_and_table_dependencies() {