pets_tools 0.2.0

MusKitty multi-repo source management CLI — one command to clone, build, test, push, and publish 9 independent crate repositories
//! MusKitty multi-repo management tool.
//!
//! Binary name: `pets`
//! Usage: `pets <ACTION> <PROJECT> [OPTIONS]`

use clap::{Args, Parser, Subcommand};
use std::path::PathBuf;

// ── CLI definition ──

#[derive(Parser)]
#[command(
    name = "pets",
    about = "MusKitty multi-repo management tool",
    long_about = "One-command management for the MusKitty browser engine's 9 independent crate repositories.\n\nUsage: pets <ACTION> <PROJECT> [OPTIONS]",
    version = env!("CARGO_PKG_VERSION"),
)]
pub struct Cli {
    #[command(subcommand)]
    pub action: Action,
}

#[derive(Subcommand)]
pub enum Action {
    /// Clone all repos from GitHub to local
    Clone(ProjectArgs),
    /// Pull latest commits for all repos (fast-forward only)
    Pull(ProjectArgs),
    /// One-command dev environment setup (clone + verify deps + pull)
    Setup(ProjectArgs),
    /// Build all crates in topological order
    Build(BuildArgs),
    /// Run tests across all crates
    Test(TestArgs),
    /// Push all repos with confirmation
    Push(PushArgs),
    /// Show git status overview for all repos
    Status(StatusArgs),
    /// Publish all crates to crates.io in dependency order
    Publish(PublishArgs),
    /// Run WPT semantic comparison tests
    Wpt(WptArgs),
    /// Format all crates with cargo fmt
    Fmt(FmtArgs),
    /// Scaffold a new muskitty crate from template
    New(NewArgs),
    /// Run a user-defined command from repos.toml (`[project.commands]` / `[[repos]].commands`)
    Run(RunArgs),
    /// List user-defined commands defined in repos.toml
    List(ListArgs),
}

#[derive(Args)]
pub struct ProjectArgs {
    /// Project name (must match `[project].name` in repos.toml)
    pub project: String,

    /// Path to repos.toml configuration file
    #[arg(short, long, default_value = "repos.toml")]
    pub config: PathBuf,

    /// Verbose output
    #[arg(short, long)]
    pub verbose: bool,
}

#[derive(Args)]
pub struct BuildArgs {
    /// Project name
    pub project: String,

    /// Path to repos.toml
    #[arg(short, long, default_value = "repos.toml")]
    pub config: PathBuf,

    /// Build in release mode (cargo build --release)
    #[arg(short, long)]
    pub release: bool,

    /// Only operate on this specific crate
    #[arg(long)]
    pub only: Option<String>,

    /// Verbose output
    #[arg(short, long)]
    pub verbose: bool,
}

#[derive(Args)]
pub struct TestArgs {
    /// Project name
    pub project: String,

    /// Path to repos.toml
    #[arg(short, long, default_value = "repos.toml")]
    pub config: PathBuf,

    /// Only run lib tests
    #[arg(long)]
    pub lib_only: bool,

    /// Skip integration tests
    #[arg(long)]
    pub no_integration: bool,

    /// Only operate on this specific crate
    #[arg(long)]
    pub only: Option<String>,

    /// Verbose output
    #[arg(short, long)]
    pub verbose: bool,
}

#[derive(Args)]
pub struct PushArgs {
    /// Project name
    pub project: String,

    /// Path to repos.toml
    #[arg(short, long, default_value = "repos.toml")]
    pub config: PathBuf,

    /// Skip confirmation, push all repos
    #[arg(short, long)]
    pub all: bool,

    /// Dry run: show what would be pushed without pushing
    #[arg(long)]
    pub dry_run: bool,

    /// Verbose output
    #[arg(short, long)]
    pub verbose: bool,
}

#[derive(Args)]
pub struct StatusArgs {
    /// Project name
    pub project: String,

    /// Path to repos.toml
    #[arg(short, long, default_value = "repos.toml")]
    pub config: PathBuf,

    /// Output as JSON
    #[arg(long)]
    pub json: bool,

    /// Verbose output
    #[arg(short, long)]
    pub verbose: bool,
}

#[derive(Args)]
pub struct PublishArgs {
    /// Project name
    pub project: String,

    /// Path to repos.toml
    #[arg(short, long, default_value = "repos.toml")]
    pub config: PathBuf,

    /// Dry run: show what would be published without publishing
    #[arg(long)]
    pub dry_run: bool,

    /// Verbose output
    #[arg(short, long)]
    pub verbose: bool,
}

#[derive(Args)]
pub struct FmtArgs {
    /// Project name
    pub project: String,

    /// Path to repos.toml
    #[arg(short, long, default_value = "repos.toml")]
    pub config: PathBuf,

    /// Actually fix formatting (default is check-only)
    #[arg(long)]
    pub fix: bool,

    /// Only operate on this specific crate
    #[arg(long)]
    pub only: Option<String>,

    /// Verbose output
    #[arg(short, long)]
    pub verbose: bool,
}

#[derive(Args)]
pub struct NewArgs {
    /// Project name
    pub project: String,

    /// New crate name (without "muskitty-" prefix, e.g. "css-values")
    pub name: String,

    /// Path to repos.toml
    #[arg(short, long, default_value = "repos.toml")]
    pub config: PathBuf,

    /// Comma-separated dependency crate names
    #[arg(long, value_delimiter = ',')]
    pub deps: Vec<String>,

    /// Comma-separated dev-dependency crate names
    #[arg(long, value_delimiter = ',')]
    pub dev_deps: Vec<String>,

    /// Minimum supported Rust version
    #[arg(long, default_value = "1.85")]
    pub msrv: String,

    /// Dry run: show what would be generated
    #[arg(long)]
    pub dry_run: bool,

    /// Verbose output
    #[arg(short, long)]
    pub verbose: bool,
}

#[derive(Args)]
pub struct RunArgs {
    /// Project name (must match `[project].name` in repos.toml)
    pub project: String,

    /// Script name defined in `[project.commands]` or `[[repos]].commands`
    pub script: String,

    /// Path to repos.toml configuration file
    #[arg(short, long, default_value = "repos.toml")]
    pub config: PathBuf,

    /// Run inside this repo's directory instead of the project root
    #[arg(long)]
    pub repo: Option<String>,

    /// Verbose output
    #[arg(short, long)]
    pub verbose: bool,

    /// Extra args appended to the script command string
    #[arg(trailing_var_arg = true, allow_hyphen_values = true)]
    pub args: Vec<String>,
}

#[derive(Args)]
pub struct ListArgs {
    /// Project name (must match `[project].name` in repos.toml)
    pub project: String,

    /// Path to repos.toml configuration file
    #[arg(short, long, default_value = "repos.toml")]
    pub config: PathBuf,

    /// Only list commands for this repo
    #[arg(long)]
    pub repo: Option<String>,

    /// Verbose output
    #[arg(short, long)]
    pub verbose: bool,
}

#[derive(Args)]
pub struct WptArgs {
    /// Project name (must match `[project].name` in repos.toml)
    pub project: String,

    /// Path to repos.toml configuration file
    #[arg(short, long, default_value = "repos.toml")]
    pub config: PathBuf,

    /// Only run WPT for these crates (comma-separated)
    #[arg(long)]
    pub only: Option<String>,

    /// Record current results as the new baseline
    #[arg(long)]
    pub update_baseline: bool,

    /// Verbose output
    #[arg(short, long)]
    pub verbose: bool,
}

// ── main ──

mod cargo_ops;
mod config;
mod git;
mod ops;
mod token;
mod topology;
mod ui;

fn main() {
    let cli = Cli::parse();

    // Detect whether this action needs auth tokens
    let needs_auth = matches!(
        cli.action,
        Action::Clone(_) | Action::Push(_) | Action::Publish(_)
    );
    token::check_on_startup(needs_auth);

    // Dispatch: each action resolves config and delegates to ops module.
    match cli.action {
        Action::Clone(args) => ops::clone::run(load_config(&args.config), &args.project),
        Action::Pull(args) => ops::pull::run(load_config(&args.config), &args.project),
        Action::Setup(args) => ops::setup::run(load_config(&args.config), &args.project),
        Action::Build(args) => ops::build::run(load_config(&args.config), &args),
        Action::Test(args) => ops::test::run(load_config(&args.config), &args),
        Action::Push(args) => ops::push::run(load_config(&args.config), &args),
        Action::Status(args) => ops::status::run(load_config(&args.config), &args),
        Action::Publish(args) => ops::publish::run(load_config(&args.config), &args),
        Action::Wpt(args) => ops::wpt::run(load_config(&args.config), &args),
        Action::Fmt(args) => ops::fmt::run(load_config(&args.config), &args),
        Action::New(args) => ops::scaffold::run(load_config(&args.config), &args),
        Action::Run(args) => ops::run::run(load_config(&args.config), &args),
        Action::List(args) => ops::list::list(load_config(&args.config), &args),
    }
}

/// Load config, report and exit on failure.
fn load_config(path: &std::path::Path) -> config::Config {
    match config::Config::load(path) {
        Ok(c) => c,
        Err(e) => {
            ui::error(&format!(
                "Failed to load config '{}': {}",
                path.display(),
                e
            ));
            std::process::exit(1);
        }
    }
}