Skip to main content

libcrapapp/
cli.rs

1//! CLI implementation for the `cargo crapapp` subcommand.
2//!
3//! The public entrypoint is [`run_cli`]. It normalizes Cargo subcommand
4//! arguments, parses the command line, and dispatches to the build-manifest and
5//! bundling services.
6
7use std::env;
8use std::ffi::OsString;
9
10use anyhow::Result;
11use clap::{Parser, Subcommand, ValueEnum};
12
13use crate::build_manifest::{BuildManifest, BuildManifestFormatter};
14use crate::builder::{Builder, BundleSelection};
15use crate::bundlers::{LinuxBundlerKind, MacosBundlerKind, WindowsBundlerKind};
16use crate::manifest_file::{CrapManifest, MANIFEST_PATH};
17
18#[derive(Debug, Parser)]
19#[command(name = "crapapp")]
20#[command(about = "Build installers and self-contained app bundles from CRAP.toml")]
21struct Cli {
22    #[command(subcommand)]
23    command: Command,
24}
25
26#[derive(Debug, Subcommand)]
27enum Command {
28    /// Read and validate a CRAP.toml manifest.
29    Inspect {
30        /// Output format.
31        #[arg(short, long, value_enum, default_value_t = OutputFormat::Text)]
32        output: OutputFormat,
33    },
34    /// Build configured cargo packages for configured platform targets.
35    Build,
36    /// Build installers and self-contained app bundles.
37    Bundle {
38        /// Skip building configured cargo packages before bundling.
39        #[arg(short = 'n', long)]
40        no_build: bool,
41        /// Build only these Linux bundle formats.
42        #[arg(long, value_enum, num_args = 1..)]
43        linux: Vec<LinuxBundlerKind>,
44        /// Build only these Windows bundle formats.
45        #[arg(long, value_enum, num_args = 1..)]
46        windows: Vec<WindowsBundlerKind>,
47        /// Build only these macOS bundle formats.
48        #[arg(long, value_enum, num_args = 1..)]
49        macos: Vec<MacosBundlerKind>,
50    },
51}
52
53#[derive(Clone, Copy, Debug, ValueEnum)]
54enum OutputFormat {
55    Text,
56    Json,
57}
58
59pub fn run_cli() -> Result<()> {
60    let cli = Cli::parse_from(cargo_args());
61
62    match cli.command {
63        Command::Inspect { output } => {
64            let manifest = CrapManifest::load(MANIFEST_PATH)?;
65            let build_manifest = BuildManifest::from_crap_manifest(&manifest)?;
66            let output = BuildManifestFormatter::from(output);
67
68            print!("{}", build_manifest.display(output)?);
69        }
70        Command::Build => {
71            let manifest = CrapManifest::load(MANIFEST_PATH)?;
72            let build_manifest = BuildManifest::from_crap_manifest(&manifest)?;
73
74            Builder::new(&build_manifest).build()?;
75        }
76        Command::Bundle {
77            no_build,
78            linux,
79            windows,
80            macos,
81        } => {
82            let manifest = CrapManifest::load(MANIFEST_PATH)?;
83            let build_manifest = BuildManifest::from_crap_manifest(&manifest)?;
84            let selection = BundleSelection::new(windows, macos, linux);
85
86            Builder::new(&build_manifest).bundle(!no_build, &selection)?;
87        }
88    }
89
90    Ok(())
91}
92
93impl From<OutputFormat> for BuildManifestFormatter {
94    fn from(value: OutputFormat) -> Self {
95        match value {
96            OutputFormat::Text => BuildManifestFormatter::Text,
97            OutputFormat::Json => BuildManifestFormatter::Json,
98        }
99    }
100}
101
102fn cargo_args() -> Vec<OsString> {
103    let mut args = env::args_os();
104    let mut normalized = Vec::new();
105
106    if let Some(bin) = args.next() {
107        normalized.push(bin);
108    }
109
110    match args.next() {
111        Some(arg) if arg == "crapapp" => {}
112        Some(arg) => normalized.push(arg),
113        None => {}
114    }
115
116    normalized.extend(args);
117    normalized
118}