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 {
#[arg(long, global = true)]
path: Option<PathBuf>,
#[command(subcommand)]
command: Command,
}
#[derive(Subcommand)]
enum Command {
Init,
Add {
names: Vec<String>,
#[arg(long, conflicts_with = "names")]
all: bool,
#[arg(long)]
overwrite: bool,
},
List,
Diff {
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)
}
}
}