Skip to main content

glory_cli/
lib.rs

1//! Project-owned Glory tooling. Link this library from a small private workspace
2//! package; CLI dependencies never belong to the application's runtime graph.
3mod apple;
4mod args;
5mod build;
6mod completions;
7mod compression;
8mod native_dev;
9mod process;
10mod project;
11mod scaffold;
12mod serve;
13#[cfg(test)]
14mod test_support;
15mod wasm_opt;
16
17use std::path::Path;
18use std::process::ExitCode;
19
20type Result<T> = std::result::Result<T, String>;
21
22/// Run the command line for an application's manifest. A private workspace
23/// executable can pass `concat!(env!("CARGO_MANIFEST_DIR"), "/../../Cargo.toml")`.
24/// Returns an exit code without terminating the process or hiding errors.
25pub fn run_from_manifest(manifest: impl AsRef<Path>) -> ExitCode {
26    let args = match args::Args::parse(std::env::args_os().skip(1)) {
27        Ok(args) => args,
28        Err(error) => {
29            eprintln!("glory: {error}");
30            return ExitCode::FAILURE;
31        }
32    };
33    match execute(manifest.as_ref(), args) {
34        Ok(()) => ExitCode::SUCCESS,
35        Err(error) => {
36            eprintln!("glory: {error}");
37            ExitCode::FAILURE
38        }
39    }
40}
41
42fn execute(manifest: &Path, args: args::Args) -> Result<()> {
43    let manifest = args.manifest.as_deref().unwrap_or(manifest);
44    if args.command == args::Command::Help {
45        println!("{}", args::HELP);
46        return Ok(());
47    }
48    if args.command == args::Command::Version {
49        println!("glory-cli {}", env!("CARGO_PKG_VERSION"));
50        return Ok(());
51    }
52    if args.command == args::Command::Init {
53        return scaffold::init(manifest, args.force, args.cli_path.as_deref());
54    }
55    if args.command == args::Command::Completions {
56        println!(
57            "{}",
58            completions::script(args.shell.as_deref().ok_or("missing completion shell")?)?
59        );
60        return Ok(());
61    }
62    let project = project::Project::load(manifest, &args)?;
63    match args.command {
64        args::Command::Doctor => project.doctor(&args),
65        args::Command::Print => project.print(&args),
66        args::Command::Build => build::build(&project, &args).map(|_| ()),
67        args::Command::Bundle => build::bundle(&project, &args),
68        args::Command::Run => build::run(&project, &args),
69        args::Command::Serve => serve::serve(project, args),
70        args::Command::Check => project.check(&args),
71        args::Command::Fmt => project.format(&args),
72        _ => unreachable!(),
73    }
74}