Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

devenv: add shell completions #1324

Merged
merged 1 commit into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions package.nix
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,16 @@ pkgs.rustPlatform.buildRustPackage {
# Generate manpages
cargo xtask generate-manpages --out-dir man
installManPage man/*

# Generate shell completions
compdir=./completions
for shell in bash fish zsh; do
cargo xtask generate-shell-completion $shell --out-dir $compdir
done

installShellCompletion --cmd devenv \
--bash $compdir/devenv.bash \
--fish $compdir/devenv.fish \
--zsh $compdir/_devenv
'';
}
1 change: 1 addition & 0 deletions xtask/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ edition.workspace = true

[dependencies]
clap.workspace = true
clap_complete = "4.5.7"
clap_mangen = "0.2.22"
devenv = { path = "../devenv" }
miette.workspace = true
1 change: 1 addition & 0 deletions xtask/src/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include!("../../devenv/src/cli.rs");
3 changes: 3 additions & 0 deletions xtask/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub(crate) mod cli;
pub mod manpage;
pub mod shell_completion;
20 changes: 17 additions & 3 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use clap::Parser;
use miette::Result;

mod manpage;
use xtask::{manpage, shell_completion};

#[derive(clap::Parser)]
struct Cli {
Expand All @@ -20,12 +19,27 @@ enum Command {
)]
out_dir: std::path::PathBuf,
},
GenerateShellCompletion {
#[clap(value_enum)]
shell: clap_complete::Shell,

#[clap(
long,
value_parser,
value_hint = clap::ValueHint::DirPath,
default_value_os_t = shell_completion::default_out_dir()
)]
out_dir: std::path::PathBuf,
},
}

fn main() -> Result<()> {
let cli = Cli::parse();

match cli.command {
Command::GenerateManpages { out_dir } => manpage::generate_manpages(out_dir),
Command::GenerateManpages { out_dir } => manpage::generate(out_dir),
Command::GenerateShellCompletion { shell, out_dir } => {
shell_completion::generate(shell, out_dir)
}
}
}
11 changes: 4 additions & 7 deletions xtask/src/manpage.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
use super::cli::Cli;
use clap::CommandFactory;
use miette::{IntoDiagnostic, Result};
use std::fs;
use std::path::{Path, PathBuf};

mod cli {
include!("../../devenv/src/cli.rs");
}

pub fn generate_manpages(out_dir: impl AsRef<Path>) -> Result<()> {
pub fn generate(out_dir: impl AsRef<Path>) -> Result<()> {
fs::create_dir_all(&out_dir).into_diagnostic()?;
clap_mangen::generate_to(cli::Cli::command(), &out_dir).into_diagnostic()?;
println!("Generated man pages to {}", out_dir.as_ref().display());
clap_mangen::generate_to(Cli::command(), &out_dir).into_diagnostic()?;
eprintln!("Generated man pages to {}", out_dir.as_ref().display());
Ok(())
}

Expand Down
23 changes: 23 additions & 0 deletions xtask/src/shell_completion.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use crate::cli::Cli;
use clap::CommandFactory;
use miette::{IntoDiagnostic, Result};
use std::fs;
use std::path::{Path, PathBuf};

pub fn generate(shell: clap_complete::Shell, out_dir: impl AsRef<Path>) -> Result<()> {
fs::create_dir_all(&out_dir).into_diagnostic()?;
let mut cmd = Cli::command();
let bin_name = cmd.get_name().to_string();
let completion_path = clap_complete::generate_to(shell, &mut cmd, bin_name, out_dir.as_ref())
.into_diagnostic()?;
eprintln!(
"Generated {} completions to {}",
shell,
completion_path.display()
);
Ok(())
}

pub fn default_out_dir() -> PathBuf {
std::env::current_dir().unwrap()
}
Loading