From 33955fa8362eaf3b25565a77d118c6827f38b27d Mon Sep 17 00:00:00 2001 From: Nathan Fisher Date: Sun, 26 Mar 2023 02:13:13 -0400 Subject: [PATCH] Made completions generation more generic --- bootstrap/src/lib.rs | 76 ++++++++++++++++++-------------------------- 1 file changed, 31 insertions(+), 45 deletions(-) diff --git a/bootstrap/src/lib.rs b/bootstrap/src/lib.rs index d40d72c..e1c1ec8 100644 --- a/bootstrap/src/lib.rs +++ b/bootstrap/src/lib.rs @@ -6,54 +6,40 @@ use { static PROGNAME: &str = "hpk"; +fn gencomp(outdir: PathBuf, gen:&str) -> Result<(), Box> { + 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>(dir: P) -> Result<(), Box> { 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(()) }