#[cfg(all(feature = "mimalloc", not(debug_assertions)))]
#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
fn main() {
let embedder = &aube_util::identity::AUBE;
let code = if is_usage_invocation() {
print_usage_spec(embedder)
} else {
aube::cli_main(embedder)
};
std::process::exit(code);
}
fn print_usage_spec(embedder: &'static aube_util::Embedder) -> i32 {
let mut spec = format!(
"// @generated by usage-rs metadata\nmin_usage_version \"4.0\"\n{}\n",
aube::usage_kdl_for(embedder.name)
)
.into_bytes();
let extra = extra_usage_spec(embedder.name);
spec.extend_from_slice(extra.trim_end().as_bytes());
spec.push(b'\n');
use std::io::Write;
let mut out = std::io::stdout();
match out.write_all(&spec).and_then(|()| out.flush()) {
Ok(()) => 0,
Err(e) if e.kind() == std::io::ErrorKind::BrokenPipe => 0,
Err(e) => {
let code = aube_codes::errors::ERR_AUBE_USAGE_SPEC_WRITE_FAILED;
eprintln!("{}: {code}: failed to write usage spec: {e}", embedder.name);
aube_codes::exit::exit_code_for(code).unwrap_or(aube_codes::exit::EXIT_GENERIC)
}
}
}
fn extra_usage_spec(bin: &str) -> String {
include_str!("../assets/extra.usage.kdl")
.replace("{bin}", bin)
.replace(
"{completers}",
&include_str!("../assets/completion.usage.kdl").replace("{bin}", bin),
)
}
fn is_usage_invocation() -> bool {
let mut args = std::env::args_os();
let invoked_as_aube = args
.next()
.as_deref()
.map(std::path::Path::new)
.and_then(|p| p.file_stem())
.and_then(|s| s.to_str())
.map(|stem| {
!matches!(
stem.to_ascii_lowercase().as_str(),
"aubr" | "aubx" | "node" | "npm" | "npx" | "pnpm" | "pnpx" | "yarn" | "yarnpkg"
)
})
.unwrap_or(true);
invoked_as_aube && args.next().as_deref() == Some(std::ffi::OsStr::new("usage"))
}
#[cfg(test)]
mod tests {
use super::extra_usage_spec;
#[test]
fn external_completers_use_the_compiled_runtime_bridge() {
let extra = extra_usage_spec("embedded");
assert!(
extra.contains(
"embedded __complete_word__ --candidates package --line={{ words | shell_join | shell_quote }}"
),
"{extra}"
);
assert!(
extra.contains(
"embedded __complete_word__ --candidates script --line={{ words | shell_join | shell_quote }}"
),
"{extra}"
);
assert!(!extra.contains("completion _ --complete"), "{extra}");
}
}