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 publish <project>` — publish crates to crates.io in dependency order.

use crate::cargo_ops;
use crate::config::Config;
use crate::topology::Topology;
use crate::ui;
use crate::PublishArgs;

pub fn run(config: Config, args: &PublishArgs) {
    // Check token
    if std::env::var("CARGO_REGISTRY_TOKEN").is_err() {
        ui::error(
            "CARGO_REGISTRY_TOKEN not set. Run:\n  \
             . .\\env.ps1\n  \
             The token should be in ~/.cargo/credentials.toml",
        );
        std::process::exit(1);
    }

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

    ui::heading(&format!(
        "Publishing {} crates to crates.io ({} levels)",
        config.repos.len(),
        topo.levels.len(),
    ));

    let mut published = 0usize;
    let mut skipped = 0usize;

    // Publish in topological order (sequentially within each level)
    for level in &topo.levels {
        for &crate_idx in level {
            let repo = &config.repos[crate_idx];

            // Read current version
            let version = match cargo_ops::read_version(&repo.abs_dir) {
                Ok(v) => v,
                Err(e) => {
                    ui::error(&format!("{} — {}", repo.name, e));
                    continue;
                }
            };

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

            // Idempotency check
            let full_name = repo.name.to_string();
            match cargo_ops::version_exists_on_crates_io(&full_name, &version) {
                Ok(true) => {
                    ui::info(&format!(
                        "  {} v{} already on crates.io — skipped",
                        repo.name, version
                    ));
                    skipped += 1;
                    continue;
                }
                Ok(false) => {} // proceed
                Err(_) => {
                    // If curl fails, proceed anyway (maybe no network, let cargo decide)
                    ui::warn("  Could not check crates.io; proceeding...");
                }
            }

            if args.dry_run {
                ui::info(&format!(
                    "  [dry-run] would publish {} v{}",
                    repo.name, version
                ));
                published += 1;
                continue;
            }

            // Publish
            match cargo_ops::publish(&repo.abs_dir, false) {
                Ok(msg) => {
                    ui::success(&format!("{} v{} — published ✓", repo.name, version));
                    if !msg.is_empty() {
                        println!("    {}", msg);
                    }
                    published += 1;
                }
                Err(e) => {
                    ui::error(&format!("{} v{} — {}", repo.name, version, e));
                    ui::error("Stopping publish chain due to failure.");
                    std::process::exit(1);
                }
            }
        }
    }

    ui::separator();
    ui::summary(published, skipped, 0);
}