tambo 0.5.0

Install and prove the whole AI knowledge-tool kit
Documentation
//! Embed the git commit in `caboodle --version`. Two builds of one release
//! version are otherwise indistinguishable, which hid a stale copy shadowing a
//! current one on PATH (aegis-nvw6ye.1).
use std::process::Command;

fn git(args: &[&str]) -> Option<String> {
    let dir = std::env::var("CARGO_MANIFEST_DIR").ok()?;
    let out = Command::new("git")
        .arg("-C")
        .arg(dir)
        .args(args)
        .output()
        .ok()?;
    let text = String::from_utf8_lossy(&out.stdout).trim().to_owned();
    (out.status.success() && !text.is_empty()).then_some(text)
}

fn main() {
    println!("cargo:rerun-if-env-changed=CABOODLE_GIT_SHA");
    // Rebuild when HEAD moves: HEAD itself, the branch ref it names, and
    // packed refs. Paths come from git so linked worktrees work too.
    for path in ["HEAD", "packed-refs"] {
        if let Some(p) = git(&["rev-parse", "--git-path", path]) {
            println!("cargo:rerun-if-changed={p}");
        }
    }
    if let Some(reference) = git(&["symbolic-ref", "-q", "HEAD"]) {
        if let Some(p) = git(&["rev-parse", "--git-path", &reference]) {
            println!("cargo:rerun-if-changed={p}");
        }
    }
    // Only this crate's own checkout counts. A source tree sitting inside some
    // other repository would otherwise report THAT repository's commit.
    let own_checkout = || {
        let top = std::fs::canonicalize(git(&["rev-parse", "--show-toplevel"])?).ok()?;
        let manifest = std::fs::canonicalize(std::env::var("CARGO_MANIFEST_DIR").ok()?).ok()?;
        (top == manifest).then_some(())
    };
    let sha = std::env::var("CABOODLE_GIT_SHA")
        .ok()
        .filter(|s| !s.is_empty())
        .or_else(|| own_checkout().and_then(|()| git(&["rev-parse", "--short=12", "HEAD"])))
        .unwrap_or_else(|| "unknown".to_owned());
    println!("cargo:rustc-env=CABOODLE_GIT_SHA={sha}");
    embed_members();
}

/// Embed every stack-member manifest (aegis-1i5h1j C1). A member is DATA: adding
/// one is a reviewed `members/<name>.toml`, never a Rust match arm. Embedded at
/// build time so the pinned version and digests are those this caboodle build
/// was reviewed with, not whatever a runtime directory holds.
fn embed_members() {
    let root = std::path::PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());
    let mut dirs = vec![root.join("members")];
    if std::env::var_os("CARGO_FEATURE_FIXTURE_MEMBERS").is_some() {
        // A test-only member must never reach a user's machine, e.g. through
        // `cargo install --all-features`.
        assert!(
            std::env::var("PROFILE").as_deref() != Ok("release"),
            "the fixture-members feature is for tests only and refuses a release build"
        );
        dirs.push(root.join("tests/fixtures/members"));
    }
    let mut entries = Vec::new();
    for dir in &dirs {
        println!("cargo:rerun-if-changed={}", dir.display());
        let Ok(read) = std::fs::read_dir(dir) else {
            continue;
        };
        let mut files: Vec<_> = read
            .filter_map(Result::ok)
            .map(|e| e.path())
            .filter(|p| p.extension().is_some_and(|x| x == "toml"))
            .collect();
        files.sort();
        for f in files {
            println!("cargo:rerun-if-changed={}", f.display());
            entries.push(format!(
                "    include_str!({:?}),\n",
                f.display().to_string()
            ));
        }
    }
    let out =
        std::path::PathBuf::from(std::env::var("OUT_DIR").unwrap()).join("members_embedded.rs");
    std::fs::write(out, format!("&[\n{}]", entries.concat())).unwrap();
}