xtask/lib.rs
1//! Library surface of the `xtask` helper crate: the workspace root and the
2//! two command builders every task runs through, plus the generators.
3
4use std::path::{Path, PathBuf};
5
6pub mod autogen;
7
8/// The workspace root. `xtask` lives one level below it.
9///
10/// # Panics
11/// On an impossible layout (no parent directory) — a build helper should
12/// abort loudly rather than thread that through every command.
13#[expect(clippy::expect_used)]
14pub fn workspace_root() -> PathBuf {
15 Path::new(env!("CARGO_MANIFEST_DIR"))
16 .parent()
17 .expect("xtask crate has a parent directory")
18 .to_path_buf()
19}
20
21/// A command rooted at the workspace, so it runs from the right directory
22/// regardless of where `cargo xtask` was invoked.
23pub fn command(program: &str, args: &[&str]) -> duct::Expression {
24 duct::cmd(program, args.iter().copied()).dir(workspace_root())
25}
26
27pub fn cargo(args: &[&str]) -> duct::Expression {
28 let cargo = std::env::var("CARGO").unwrap_or_else(|_| "cargo".to_string());
29 command(&cargo, args)
30}