Your browser sucks, JavaScript disabled! Gunnar's console needs JavaScript and WebAssembly. Git over SSH on port 2222 does not.

nordisk/holger

xtask/boot/src/main.rs

//! `cargo xtask`'s launcher; see `Cargo.toml`. No dependencies, on purpose.

use std::path::PathBuf;
use std::process::{Command, ExitCode};

mod siblings;

/// The checkout the alias ran in: the nearest directory, from the working directory
/// up, that holds `xtask/boot/Cargo.toml`. Never `CARGO_MANIFEST_DIR`: cargo gives a
/// path package the same build hash in every checkout, so a target dir shared by two
/// checkouts would hand one of them the other one's compiled-in path.
fn repo_root() -> PathBuf {
    let mut dir = std::env::current_dir().expect("a working directory");
    loop {
        if dir.join("xtask/boot/Cargo.toml").is_file() {
            return dir;
        }
        if !dir.pop() {
            eprintln!("xtask: run cargo xtask from inside a holger checkout");
            std::process::exit(2);
        }
    }
}

fn main() -> ExitCode {
    let root = repo_root();
    let args: Vec<String> = std::env::args().skip(1).collect();
    let refreshed = siblings::refresh(&root);
    if args.first().map(String::as_str) == Some("siblings") {
        return match refreshed {
            Ok(siblings::Refresh::Written) => {
                println!(
                    "siblings: {} written from {} (the sibling patches are ON)",
                    siblings::COPY,
                    siblings::TEMPLATE
                );
                ExitCode::SUCCESS
            }
            Ok(siblings::Refresh::Current) => {
                println!(
                    "siblings: {} is current (the sibling patches are ON)",
                    siblings::COPY
                );
                ExitCode::SUCCESS
            }
            Ok(siblings::Refresh::Lone(missing)) => {
                eprintln!(
                    "siblings: ../{missing} is not checked out beside {}; the sibling patches stay OFF \
                     (needed: {}). A lone clone builds from crates.io and needs none.",
                    root.display(),
                    siblings::SIBLINGS.join(", ")
                );
                ExitCode::FAILURE
            }
            Err(e) => {
                eprintln!("siblings: could not write {}: {e}", siblings::COPY);
                ExitCode::FAILURE
            }
        };
    }
    match refreshed {
        Ok(siblings::Refresh::Written) => eprintln!(
            "xtask: {} written from {} (the sibling patches are ON)",
            siblings::COPY,
            siblings::TEMPLATE
        ),
        Ok(_) => {}
        Err(e) => eprintln!("xtask: could not refresh {}: {e}", siblings::COPY),
    }
    let cargo = std::env::var("CARGO").unwrap_or_else(|_| "cargo".into());
    let status = Command::new(cargo)
        // `--locked` (HOL-PRODUCTS-9): a verb never rewrites the committed `estate/Cargo.lock`;
        // sibling checkouts that do not match it refuse the run by name instead.
        .args([
            "run",
            "--quiet",
            "--locked",
            "--package",
            "xtask",
            "--manifest-path",
        ])
        // xtask is a member of the `estate/` workspace (H1), never of the root.
        .arg(root.join("xtask/Cargo.toml"))
        .arg("--")
        .args(&args)
        .status();
    match status {
        Ok(s) => ExitCode::from(s.code().unwrap_or(1).clamp(0, 255) as u8),
        Err(e) => {
            eprintln!("xtask: could not start the real xtask: {e}");
            ExitCode::FAILURE
        }
    }
}