57 lines
1.6 KiB
Rust
57 lines
1.6 KiB
Rust
use clap::{CommandFactory, ValueEnum};
|
|
use clap_complete::{Shell, generate_to};
|
|
use std::{env, ffi::OsString, fs, io::Error, path::PathBuf};
|
|
|
|
include!("src/cli/mod.rs");
|
|
|
|
fn shell_name(shell: Shell) -> String {
|
|
shell
|
|
.to_possible_value()
|
|
.map(|v| v.get_name().to_owned())
|
|
.unwrap_or_default()
|
|
}
|
|
|
|
fn non_empty(name: &str) -> Option<OsString> {
|
|
env::var_os(name).filter(|v| !v.is_empty())
|
|
}
|
|
|
|
fn install_dir(shell: Shell) -> Option<(PathBuf, bool)> {
|
|
let per_shell = format!("SHELL_COMPLETIONS_DIR_{}", shell_name(shell).to_uppercase());
|
|
|
|
if let Some(dir) = non_empty(&per_shell).or_else(|| non_empty("SHELL_COMPLETIONS_DIR")) {
|
|
return Some((PathBuf::from(dir), true));
|
|
}
|
|
non_empty("OUT_DIR").map(|dir| (PathBuf::from(dir), false))
|
|
}
|
|
|
|
fn main() -> Result<(), Error> {
|
|
println!("cargo::rerun-if-env-changed=SHELL_COMPLETIONS_DIR");
|
|
for shell in Shell::value_variants() {
|
|
println!(
|
|
"cargo::rerun-if-env-changed=SHELL_COMPLETIONS_DIR_{}",
|
|
shell_name(*shell).to_uppercase()
|
|
);
|
|
}
|
|
|
|
let mut cmd = ahab::Ahab::command();
|
|
|
|
for shell in Shell::value_variants() {
|
|
let Some((dir, requested)) = install_dir(*shell) else {
|
|
continue;
|
|
};
|
|
|
|
fs::create_dir_all(&dir)?;
|
|
let path = generate_to(*shell, &mut cmd, env!("CARGO_PKG_NAME"), &dir)?;
|
|
|
|
if requested {
|
|
println!(
|
|
"cargo::warning=installed {} completion: {}",
|
|
shell_name(*shell),
|
|
path.display()
|
|
);
|
|
}
|
|
}
|
|
|
|
Ok(())
|
|
}
|