pets_tools 0.2.0

MusKitty multi-repo source management CLI — one command to clone, build, test, push, and publish 9 independent crate repositories
//! `pets build <project>` — build crates in topological order.

use crate::cargo_ops;
use crate::config::Config;
use crate::topology::Topology;
use crate::ui;
use crate::BuildArgs;
use std::collections::HashSet;

pub fn run(config: Config, args: &BuildArgs) {
    let topo = match Topology::compute(&config) {
        Ok(t) => t,
        Err(e) => {
            ui::error(&format!("Topology error: {}", e));
            std::process::exit(1);
        }
    };

    let mode = if args.release {
        "build --release"
    } else {
        "check"
    };
    ui::heading(&format!(
        "Building {} — {} ({} levels, {} crates)",
        config.project_name,
        mode,
        topo.levels.len(),
        config.repos.len(),
    ));

    // Filter if --only specified
    let only_set: Option<HashSet<&str>> = args
        .only
        .as_ref()
        .map(|s| s.split(',').map(|n| n.trim()).collect());

    let mut total_failed = 0u32;
    let mut total_warnings = 0u32;

    for (level_idx, level) in topo.levels.iter().enumerate() {
        let names = level
            .iter()
            .map(|&i| config.repos[i].name.as_str())
            .collect::<Vec<_>>();
        ui::heading(&format!("Level {}: {}", level_idx, names.join(", ")));

        for &crate_idx in level {
            let repo = &config.repos[crate_idx];

            if let Some(ref only) = only_set {
                if !only.contains(repo.name.as_str()) {
                    ui::info(&format!("{} — skipped (--only filter)", repo.name));
                    continue;
                }
            }

            ui::info(&format!("  {} ...", repo.name));

            let result = if args.release {
                cargo_ops::build_release(&repo.abs_dir)
            } else {
                cargo_ops::check(&repo.abs_dir)
            };

            match result {
                Ok(out) => {
                    if out.success {
                        if out.warnings > 0 {
                            ui::warn(&format!("  {} ✓ ({} warnings)", repo.name, out.warnings));
                        } else {
                            ui::success(&format!("{} ✓", repo.name));
                        }
                    } else {
                        ui::error(&format!("{} ✗ — build failed", repo.name));
                        total_failed += 1;
                    }
                    total_warnings += out.warnings;
                }
                Err(e) => {
                    ui::error(&format!("{} ✗ — {}", repo.name, e));
                    total_failed += 1;
                }
            }
        }
    }

    ui::separator();
    if total_failed == 0 {
        ui::success(&format!(
            "Build complete — {} crates, {} warnings",
            config.repos.len(),
            total_warnings,
        ));
    } else {
        ui::error(&format!(
            "Build failed — {} crate(s) with errors, {} warnings",
            total_failed, total_warnings,
        ));
        std::process::exit(1);
    }
}