hyuskui 0.0.1

Add hyuskui components to a GPUI project: cargo hyuskui add button
//! `cargo hyuskui`: copy hyuskui components into a GPUI project.
//!
//! The crate is `hyuskui` (`cargo install hyuskui`) and the binary is `cargo-hyuskui`, so cargo
//! runs it as a subcommand and passes `hyuskui` as the first argument.

mod project;
mod registry;

use std::path::PathBuf;

use anyhow::{Result, bail};
use clap::{Parser, Subcommand};

use project::Project;

#[derive(Parser)]
#[command(name = "cargo", bin_name = "cargo")]
enum Cargo {
    Hyuskui(Hyuskui),
}

#[derive(clap::Args)]
#[command(version, about = "Copy hyuskui components into your GPUI project")]
struct Hyuskui {
    /// Project directory (defaults to the nearest Cargo.toml above the current directory)
    #[arg(long, global = true)]
    path: Option<PathBuf>,

    #[command(subcommand)]
    command: Command,
}

#[derive(Subcommand)]
enum Command {
    /// Add the theme and the src/ui module to your project
    Init,
    /// Copy components into src/ui/
    Add {
        /// Component names, e.g. `button card`
        names: Vec<String>,
        /// Add every component
        #[arg(long, conflicts_with = "names")]
        all: bool,
        /// Replace files that already exist
        #[arg(long)]
        overwrite: bool,
    },
    /// List available components
    List,
    /// Show how your copy of a component differs from the registry
    Diff {
        /// Component name
        name: String,
    },
}

fn main() {
    let Cargo::Hyuskui(cli) = Cargo::parse();

    if let Err(error) = run(cli) {
        eprintln!("error: {error:#}");
        std::process::exit(1);
    }
}

fn run(cli: Hyuskui) -> Result<()> {
    match cli.command {
        Command::List => {
            let width = registry::components().map(|item| item.name.len()).max().unwrap_or(0);
            for item in registry::components() {
                println!("{:width$}  {}", item.name.replace('_', "-"), item.description);
            }
            Ok(())
        }
        Command::Init => {
            let project = Project::locate(cli.path)?;
            project.init()?;
            project.check_gpui();
            Ok(())
        }
        Command::Add { names, all, overwrite } => {
            let names: Vec<String> = if all {
                registry::components().map(|item| item.name.to_string()).collect()
            } else {
                names
            };
            if names.is_empty() {
                bail!("name at least one component, e.g. `cargo hyuskui add button`, or pass --all");
            }
            for name in &names {
                match registry::find(name) {
                    Some(item) if item.kind == "registry:ui" => {}
                    _ => bail!("unknown component `{name}`. Run `cargo hyuskui list` to see them all."),
                }
            }

            let project = Project::locate(cli.path)?;
            project.init()?;
            for name in &names {
                project.add(registry::find(name).expect("checked above"), overwrite)?;
            }
            project.check_gpui();
            Ok(())
        }
        Command::Diff { name } => {
            let Some(item) = registry::find(&name) else {
                bail!("unknown component `{name}`. Run `cargo hyuskui list` to see them all.");
            };
            Project::locate(cli.path)?.diff(item)
        }
    }
}