use std::path::PathBuf;
use clap::{
Args, Parser, Subcommand, ValueEnum,
builder::styling::{AnsiColor, Effects, Styles},
};
use serde::Deserialize;
fn cli_styles() -> Styles {
Styles::styled()
.header(AnsiColor::Cyan.on_default() | Effects::BOLD)
.usage(AnsiColor::Cyan.on_default() | Effects::BOLD)
.literal(AnsiColor::Cyan.on_default())
.placeholder(AnsiColor::BrightBlack.on_default())
.valid(AnsiColor::Green.on_default())
.invalid(AnsiColor::Yellow.on_default())
.error(AnsiColor::Red.on_default() | Effects::BOLD)
}
#[derive(Debug, Parser)]
#[command(
name = "kayro",
version,
about = "Create, build, and run Kayro desktop applications.",
long_about = "Create, build, and run native Kayro desktop applications with a Vite frontend.",
after_help = "Examples:\n kayro new my-app\n kayro dev\n kayro build --arch arm64\n kayro build --json --dry-run",
propagate_version = true,
max_term_width = 100,
styles = cli_styles()
)]
#[allow(clippy::struct_excessive_bools)]
pub struct Cli {
#[arg(long, global = true)]
pub json: bool,
#[arg(long, global = true)]
pub dry_run: bool,
#[arg(short, long, global = true, conflicts_with = "verbose")]
pub quiet: bool,
#[arg(short, long, global = true, conflicts_with = "quiet")]
pub verbose: bool,
#[arg(long, global = true, value_enum, default_value_t = ColorMode::Auto)]
pub color: ColorMode,
#[command(subcommand)]
pub command: KayroCommand,
}
#[derive(Debug, Subcommand)]
pub enum KayroCommand {
New(NewArgs),
Init(InitArgs),
Dev(DevArgs),
Build(BuildArgs),
}
#[derive(Debug, Args)]
pub struct NewArgs {
#[arg(value_name = "NAME")]
pub name: String,
#[arg(long, value_name = "DIR")]
pub directory: Option<PathBuf>,
#[command(flatten)]
pub scaffold: ScaffoldOptions,
}
#[derive(Debug, Args)]
pub struct InitArgs {
#[arg(value_name = "DIR", default_value = ".")]
pub directory: PathBuf,
#[arg(long, value_name = "NAME")]
pub name: Option<String>,
#[command(flatten)]
pub scaffold: ScaffoldOptions,
}
#[derive(Debug, Args)]
pub struct ScaffoldOptions {
#[arg(long, value_name = "NAME")]
pub app_name: Option<String>,
#[arg(long, value_name = "ID")]
pub identifier: Option<String>,
#[arg(long, visible_alias = "skip-install")]
pub no_install: bool,
}
#[derive(Debug, Args)]
pub struct DevArgs {
#[command(flatten)]
pub project: ProjectOptions,
#[arg(long, value_name = "URL", conflicts_with_all = ["host", "port"])]
pub dev_url: Option<String>,
#[arg(long, value_name = "HOST")]
pub host: Option<String>,
#[arg(long, value_name = "PORT")]
pub port: Option<u16>,
#[arg(long, value_name = "SECONDS", default_value_t = 30)]
pub ready_timeout: u64,
#[arg(long)]
pub skip_types: bool,
#[command(flatten)]
pub cargo: CargoOptions,
}
#[derive(Debug, Args)]
#[allow(clippy::struct_excessive_bools)] pub struct BuildArgs {
#[command(flatten)]
pub project: ProjectOptions,
#[arg(long, value_name = "TRIPLE", conflicts_with = "arch")]
pub target: Option<String>,
#[arg(long, value_enum, conflicts_with = "target")]
pub arch: Option<Architecture>,
#[arg(long, value_name = "NAME", conflicts_with = "debug")]
pub profile: Option<String>,
#[arg(long)]
pub debug: bool,
#[arg(long, value_name = "DIR")]
pub target_dir: Option<PathBuf>,
#[arg(long, conflicts_with = "native_only")]
pub frontend_only: bool,
#[arg(long, conflicts_with = "frontend_only")]
pub native_only: bool,
#[arg(long)]
pub skip_types: bool,
#[arg(long, conflicts_with_all = ["bundles", "bundle_dir"])]
pub no_bundle: bool,
#[arg(
long,
value_enum,
value_delimiter = ',',
value_name = "FORMAT",
conflicts_with = "frontend_only"
)]
pub bundles: Vec<BundleFormat>,
#[arg(long, value_name = "DIR", conflicts_with = "frontend_only")]
pub bundle_dir: Option<PathBuf>,
#[command(flatten)]
pub cargo: CargoOptions,
}
#[derive(Debug, Args)]
pub struct ProjectOptions {
#[arg(
long = "project",
visible_alias = "project-dir",
value_name = "DIR",
conflicts_with = "config"
)]
pub project_dir: Option<PathBuf>,
#[arg(long, value_name = "FILE")]
pub config: Option<PathBuf>,
}
impl BuildArgs {
pub fn profile(&self) -> &str {
if self.debug {
"dev"
} else {
self.profile.as_deref().unwrap_or("release")
}
}
}
#[derive(Debug, Args)]
pub struct CargoOptions {
#[arg(long, value_delimiter = ',', value_name = "FEATURE")]
pub features: Vec<String>,
#[arg(long)]
pub no_default_features: bool,
#[arg(long)]
pub unlocked: bool,
#[arg(long)]
pub offline: bool,
#[arg(short = 'j', long, value_name = "N")]
pub jobs: Option<usize>,
#[arg(long, value_name = "NAME")]
pub bin: Option<String>,
#[arg(long, value_name = "ARG", allow_hyphen_values = true)]
pub cargo_arg: Vec<String>,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, ValueEnum)]
pub enum Architecture {
#[value(alias = "arm64")]
Aarch64,
#[value(name = "x86_64", alias = "x86-64", alias = "x64", alias = "amd64")]
X86_64,
#[value(alias = "x86")]
I686,
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, ValueEnum)]
pub enum ColorMode {
#[default]
Auto,
Always,
Never,
}
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, ValueEnum)]
#[serde(rename_all = "lowercase")]
pub enum BundleFormat {
App,
Dmg,
#[value(alias = "setup")]
Nsis,
#[value(alias = "wix")]
#[serde(alias = "wix")]
Msi,
#[value(name = "appimage", alias = "app-image")]
AppImage,
Deb,
}
impl Architecture {
pub fn rust_name(self) -> &'static str {
match self {
Self::Aarch64 => "aarch64",
Self::X86_64 => "x86_64",
Self::I686 => "i686",
}
}
}
#[cfg(test)]
mod tests {
use clap::Parser as _;
use super::*;
#[test]
fn global_agent_flags_work_after_the_subcommand() {
let cli = Cli::try_parse_from(["kayro", "build", "--arch", "arm64", "--json", "--dry-run"])
.unwrap();
assert!(cli.json);
assert!(cli.dry_run);
let KayroCommand::Build(build) = cli.command else {
panic!("expected build command");
};
assert_eq!(build.arch, Some(Architecture::Aarch64));
assert_eq!(build.profile(), "release");
}
#[test]
fn target_and_architecture_are_mutually_exclusive() {
let error = Cli::try_parse_from([
"kayro",
"build",
"--arch",
"x64",
"--target",
"aarch64-apple-darwin",
])
.unwrap_err();
assert_eq!(error.kind(), clap::error::ErrorKind::ArgumentConflict);
}
#[test]
fn bundle_formats_are_comma_separated_and_have_human_aliases() {
let cli = Cli::try_parse_from([
"kayro",
"build",
"--bundles",
"app,dmg,setup,wix,appimage,deb",
])
.unwrap();
let KayroCommand::Build(build) = cli.command else {
panic!("expected build command");
};
assert_eq!(
build.bundles,
[
BundleFormat::App,
BundleFormat::Dmg,
BundleFormat::Nsis,
BundleFormat::Msi,
BundleFormat::AppImage,
BundleFormat::Deb,
]
);
}
#[test]
fn executable_only_mode_conflicts_with_bundle_options() {
let error =
Cli::try_parse_from(["kayro", "build", "--no-bundle", "--bundles", "dmg"]).unwrap_err();
assert_eq!(error.kind(), clap::error::ErrorKind::ArgumentConflict);
}
#[test]
fn new_is_non_interactive_and_fully_configurable() {
let cli = Cli::try_parse_from([
"kayro",
"new",
"hello-world",
"--app-name",
"Hello World",
"--identifier",
"dev.example.hello",
"--no-install",
])
.unwrap();
let KayroCommand::New(new) = cli.command else {
panic!("expected new command");
};
assert_eq!(new.name, "hello-world");
assert_eq!(new.scaffold.app_name.as_deref(), Some("Hello World"));
assert!(new.scaffold.no_install);
}
}