Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

3.2 KB
Raw Blame
//! Generates .github/workspace-dep-closures.json: for every workspace
//! member, the directories (relative to the workspace root) of the member
//! plus every workspace crate in its transitive dependency closure.
//!
//! flake.nix uses the map to give each deploy artifact a pruned source tree,
//! so a leaf derivation's hash only moves when a crate it actually depends on
//! changes. Closures follow normal and build edges but not dev-only edges:
//! deploy artifacts are built with `cargo build --bin`, which never compiles
//! dev-deps, and including them would only widen closures. A superset closure
//! is sound (at worst a spurious rebuild); a missing crate fails the build
//! loudly inside the nix sandbox.

use std::collections::{BTreeMap, BTreeSet};

use anyhow::{Context, Result};
use guppy::graph::{DependencyDirection, PackageGraph};

const OUTPUT_REL: &str = ".github/workspace-dep-closures.json";

/// Regenerates (or, in check mode, diffs) the closure map.
pub fn run(graph: &PackageGraph, check: bool, drift: &mut Vec<String>) -> Result<()> {
    let workspace = graph.workspace();
    let ws_root = workspace.root();
    let repo_root = ws_root;
    let output_path = repo_root.join(OUTPUT_REL);

    let mut closures: BTreeMap<String, BTreeSet<String>> = BTreeMap::new();
    for member in workspace.iter() {
        // Follow only links that stay inside the workspace (registry deps
        // cannot depend back into it) and are not dev-only.
        let set = graph
            .query_forward([member.id()])
            .context("querying workspace member")?
            .resolve_with_fn(|_, link| link.to().in_workspace() && !link.dev_only());
        let dirs = set
            .packages(DependencyDirection::Forward)
            .filter(|p| p.in_workspace())
            .map(|p| {
                let dir = p
                    .manifest_path()
                    .parent()
                    .with_context(|| format!("manifest {} has no parent", p.manifest_path()))?;
                Ok(dir
                    .strip_prefix(ws_root)
                    .with_context(|| format!("{dir} is outside the workspace root"))?
                    .as_str()
                    .to_owned())
            })
            .collect::<Result<BTreeSet<_>>>()?;
        closures.insert(member.name().to_owned(), dirs);
    }

    let doc = serde_json::json!({
        "//": "Generated by `cargo run -p xtask -- deps` (see tooling/xtask); do not edit by hand. Maps every Rust workspace crate to the directories of its transitive workspace dependency closure (normal + build deps, dev-deps excluded). Consumed by flake.nix to build pruned per-artifact deploy sources.",
        "closures": closures,
    });
    let new_contents =
        serde_json::to_string_pretty(&doc).context("serializing closure map")? + "\n";

    let current = std::fs::read_to_string(&output_path).unwrap_or_default();
    if current != new_contents {
        if check {
            drift.push(OUTPUT_REL.to_owned());
        } else {
            std::fs::write(&output_path, &new_contents)
                .with_context(|| format!("writing {output_path}"))?;
            println!("updated {OUTPUT_REL} ({} members)", closures.len());
        }
    }
    Ok(())
}