Add a separate completion generation

To be able to include zsh bindings in packaging and not add a subcommand
to arch-rebuild-order add a new binary for generating completions to
'OUT_DIR'.

Closes: #9
This commit is contained in:
Jelle van der Waa 2021-01-08 16:26:04 +01:00
parent 603972214e
commit c6970a8e7c
No known key found for this signature in database
GPG Key ID: C06086337C50773E
7 changed files with 57 additions and 24 deletions

View File

@ -12,6 +12,7 @@ license-file = "LICENSE"
keywords = ["archlinux", "build", "alpm"]
categories = ["command-line-utilities"]
publish = false
default-run = "arch-rebuild-order"
[dependencies]
alpm = "1"

View File

@ -1,6 +1,7 @@
PREFIX ?= /usr/local
BINDIR ?= $(PREFIX)/bin
MANDIR ?= $(PREFIX)/share/man
OUT_DIR ?= target
# Tools
@ -20,8 +21,13 @@ doc: $(DOCDIR)/$(MANPAGE)
build:
$(CARGO) build --release --locked
completions:
OUT_DIR=$(OUT_DIR) $(CARGO) run --bin completions
.PHONY: install
install: doc
install: build completions doc
install -Dm755 target/release/$(BIN) $(DESTDIR)$(BINDIR)/$(BIN)
install -Dm644 $(DOCDIR)/$(MANPAGE) $(DESTDIR)$(MANDIR)/man1/$(MANPAGE)
install -Dm644 target/_$(BIN) $(DESTDIR)$(PREFIX)/share/zsh/site-functions/_$(BIN)
install -Dm644 target/$(BIN).bash $(DESTDIR)$(PREFIX)/share/bash-completion/$(BIN)
install -Dm644 target/$(BIN).fish $(DESTDIR)$(PREFIX)/share/fish/vendor_completions.d/$(BIN)

View File

@ -38,3 +38,8 @@ dot -Tpng opencolorio.dot > opencolorio.png
* `testing` and `community-testing` repositories are not included.
* Rebuilder expects an up-to-date syncdb and does not provide warning if it is not.
## Completions
Shell completions can be created with `cargo run --bin completions` in a
directory specified by the env variable `OUT_DIR`.

25
src/args.rs Normal file
View File

@ -0,0 +1,25 @@
use structopt::StructOpt;
#[derive(Debug, StructOpt)]
#[structopt(name = "arch-rebuild-order", about, author)]
pub struct Args {
/// List of input packages
#[structopt(min_values = 1, required = true)]
pub pkgnames: Vec<String>,
/// Repositories
#[structopt(
default_value = "core,extra,community,multilib",
long,
use_delimiter = true
)]
pub repos: Vec<String>,
/// The path to the pacman database, default ( /var/lib/pacman )
#[structopt(long)]
pub dbpath: Option<String>,
/// Write a dotfile into the given file
#[structopt(short, long)]
pub dotfile: Option<String>,
}

16
src/bin/completions.rs Normal file
View File

@ -0,0 +1,16 @@
use std::{env, str::FromStr};
use structopt::clap::Shell;
use structopt::StructOpt;
use arch_rebuild_order::args::Args;
fn main() {
// https://doc.rust-lang.org/cargo/reference/environment-variables.html
let out_dir = env::var_os("OUT_DIR").expect("out dir not set");
let mut app = Args::clap();
for variant in &Shell::variants() {
let variant = Shell::from_str(variant).unwrap();
app.gen_completions("arch-rebuild-order", variant, &out_dir);
}
println!("completion scripts generated in {:?}", out_dir);
}

View File

@ -7,6 +7,8 @@ use std::error::Error;
use std::fs::File;
use std::io::{BufWriter, Write};
pub mod args;
const ROOT_DIR: &str = "/";
const DB_PATH: &str = "/var/lib/pacman/";

View File

@ -1,28 +1,6 @@
use structopt::StructOpt;
#[derive(Debug, StructOpt)]
#[structopt(name = "arch-rebuild-order", about, author)]
struct Args {
/// List of input packages
#[structopt(min_values = 1, required = true)]
pkgnames: Vec<String>,
/// Repositories
#[structopt(
default_value = "core,extra,community,multilib",
long,
use_delimiter = true
)]
repos: Vec<String>,
/// The path to the pacman database, default ( /var/lib/pacman )
#[structopt(long)]
dbpath: Option<String>,
/// Write a dotfile into the given file
#[structopt(short, long)]
dotfile: Option<String>,
}
use arch_rebuild_order::args::Args;
fn main() {
let args = Args::from_args();