Made completions generation more generic

This commit is contained in:
Nathan Fisher 2023-03-26 02:13:13 -04:00
parent e345ca6abb
commit 33955fa836

View File

@ -6,54 +6,40 @@ use {
static PROGNAME: &str = "hpk";
fn gencomp(outdir: PathBuf, gen:&str) -> Result<(), Box<dyn Error>> {
let mut cmd = cli::cli();
let path = match gen {
"bash" => generate_to(shells::Bash, &mut cmd, PROGNAME, outdir)?,
"fish" => generate_to(shells::Fish, &mut cmd, PROGNAME, outdir)?,
"nu" => generate_to(Nushell, &mut cmd, PROGNAME, outdir)?,
"pwsh" => generate_to(shells::PowerShell, &mut cmd, PROGNAME, outdir)?,
"zsh" => generate_to(shells::Zsh, &mut cmd, PROGNAME, outdir)?,
_ => unimplemented!(),
};
println!(" {}", path.display());
Ok(())
}
fn completions<P: Deref<Target = Path>>(dir: P) -> Result<(), Box<dyn Error>> {
println!("Generating completions:");
let mut cmd = cli::cli();
let mut outdir = dir.to_path_buf();
["share", "bash-completion", "completions"]
["bash", "fish", "nu", "pwsh", "zsh"]
.iter()
.for_each(|d| outdir.push(d));
if !outdir.exists() {
fs::create_dir_all(&outdir)?;
}
let path = generate_to(shells::Bash, &mut cmd, PROGNAME, outdir)?;
println!(" {}", path.display());
let mut outdir = dir.to_path_buf();
["share", "zsh", "site-functions"]
.iter()
.for_each(|d| outdir.push(d));
if !outdir.exists() {
fs::create_dir_all(&outdir)?;
}
let path = generate_to(shells::Zsh, &mut cmd, PROGNAME, outdir)?;
println!(" {}", path.display());
let mut outdir = dir.to_path_buf();
["share", "fish", "completions"]
.iter()
.for_each(|d| outdir.push(d));
if !outdir.exists() {
fs::create_dir_all(&outdir)?;
}
let path = generate_to(shells::Fish, &mut cmd, PROGNAME, outdir)?;
println!(" {}", path.display());
let mut outdir = dir.to_path_buf();
["share", "pwsh", "completions"]
.iter()
.for_each(|d| outdir.push(d));
if !outdir.exists() {
fs::create_dir_all(&outdir)?;
}
let path = generate_to(shells::PowerShell, &mut cmd, PROGNAME, outdir)?;
println!(" {}", path.display());
let mut outdir = dir.to_path_buf();
["share", "nu", "completions"]
.iter()
.for_each(|d| outdir.push(d));
if !outdir.exists() {
fs::create_dir_all(&outdir)?;
}
let path = generate_to(Nushell, &mut cmd, PROGNAME, outdir)?;
println!(" {}", path.display());
.try_for_each(|gen| {
let mut outdir = dir.to_path_buf();
let base = match *gen {
"bash" => ["share", "bash-completion", "completions"],
"zsh" => ["share", "zsh", "site-functions"],
"nu" => ["share", "nu", "completions"],
"pwsh" => ["share", "pwsh", "completions"],
"fish" => ["share", "fish", "completions"],
_ => unimplemented!(),
};
base.iter().for_each(|d| outdir.push(d));
if !outdir.exists() {
fs::create_dir_all(&outdir)?;
}
gencomp(outdir, gen)
})?;
Ok(())
}