bootstrap: recreate completion function to use

clap_completion::generator::Generator trait to specify generic parameter
This commit is contained in:
Nathan Fisher 2022-12-22 19:09:44 -05:00
parent 0cc2bd507b
commit 480960ce2b

View File

@ -1,8 +1,13 @@
use crate::cmd; use crate::cmd;
use clap::{value_parser, Arg, ArgMatches, Command, ArgAction}; use clap::{value_parser, Arg, ArgAction, ArgMatches, Command};
use clap_complete::{generate_to, shells}; use clap_complete::{generate_to, shells, Generator};
use clap_complete_nushell::Nushell;
use clap_mangen::Man; use clap_mangen::Man;
use std::{fs, io, path::{Path, PathBuf}, process}; use std::{
fs, io,
path::{Path, PathBuf},
process,
};
const COMMANDS: [fn() -> clap::Command; 8] = [ const COMMANDS: [fn() -> clap::Command; 8] = [
cmd::bootstrap::cli, cmd::bootstrap::cli,
@ -109,26 +114,22 @@ fn manpage(prefix: &str, f: &dyn Fn() -> Command) -> Result<(), io::Error> {
fn manpages(prefix: &str) -> Result<(), io::Error> { fn manpages(prefix: &str) -> Result<(), io::Error> {
println!("Generating Unix man pages:"); println!("Generating Unix man pages:");
COMMANDS.iter().try_for_each(|cmd| { COMMANDS.iter().try_for_each(|cmd| manpage(prefix, &cmd))?;
manpage(prefix, &cmd)
})?;
Ok(()) Ok(())
} }
fn gen_bash_completion(outdir: &Path, f: &dyn Fn() -> Command) -> Result<(), io::Error> { fn generate_completions(
outdir: &Path,
f: &dyn Fn() -> Command,
gen: impl Generator,
) -> Result<(), io::Error> {
let cmd = f(); let cmd = f();
let name = cmd.get_name(); let name = cmd.get_name();
let mut cmd = cmd.clone(); let mut cmd = cmd.clone();
let path = generate_to(shells::Bash, &mut cmd, name, outdir)?; if !outdir.exists() {
println!(" {}", path.display()); fs::create_dir_all(&outdir)?;
Ok(())
} }
let path = generate_to(gen, &mut cmd, name, outdir)?;
fn gen_fish_completion(outdir: &Path, f: &dyn Fn() -> Command) -> Result<(), io::Error> {
let cmd = f();
let name = cmd.get_name();
let mut cmd = cmd.clone();
let path = generate_to(shells::Fish, &mut cmd, name, outdir)?;
println!(" {}", path.display()); println!(" {}", path.display());
Ok(()) Ok(())
} }
@ -139,25 +140,33 @@ fn completions(prefix: &str, matches: &ArgMatches) -> Result<(), io::Error> {
let outdir: PathBuf = [prefix, "share", "bash-completion", "completion"] let outdir: PathBuf = [prefix, "share", "bash-completion", "completion"]
.iter() .iter()
.collect(); .collect();
if !outdir.exists() { COMMANDS
fs::create_dir_all(&outdir)?; .iter()
} .try_for_each(|cmd| generate_completions(&outdir, &cmd, shells::Bash))?;
COMMANDS.iter().try_for_each(|cmd| gen_bash_completion(&outdir, &cmd) )?;
} }
if matches.get_flag("fish") || matches.get_flag("all") { if matches.get_flag("fish") || matches.get_flag("all") {
let outdir: PathBuf = [prefix, "share", "fish", "completions"] let outdir: PathBuf = [prefix, "share", "fish", "completions"].iter().collect();
COMMANDS
.iter() .iter()
.collect(); .try_for_each(|cmd| generate_completions(&outdir, &cmd, shells::Fish))?;
if !outdir.exists() {
fs::create_dir_all(&outdir)?;
}
COMMANDS.iter().try_for_each(|cmd| gen_fish_completion(&outdir, &cmd) )?;
} }
if matches.get_flag("nu") || matches.get_flag("all") { if matches.get_flag("nu") || matches.get_flag("all") {
let outdir: PathBuf = [prefix, "share", "nu", "completions"].iter().collect();
COMMANDS
.iter()
.try_for_each(|cmd| generate_completions(&outdir, &cmd, Nushell))?;
} }
if matches.get_flag("pwsh") || matches.get_flag("all") { if matches.get_flag("pwsh") || matches.get_flag("all") {
let outdir: PathBuf = [prefix, "share", "pwsh", "completions"].iter().collect();
COMMANDS
.iter()
.try_for_each(|cmd| generate_completions(&outdir, &cmd, shells::PowerShell))?;
} }
if matches.get_flag("zsh") || matches.get_flag("all") { if matches.get_flag("zsh") || matches.get_flag("all") {
let outdir: PathBuf = [prefix, "share", "zsh", "site-functions"].iter().collect();
COMMANDS
.iter()
.try_for_each(|cmd| generate_completions(&outdir, &cmd, shells::Zsh))?;
} }
Ok(()) Ok(())
} }