Skip to content

Commit

Permalink
rage: Build shell completions in a build script
Browse files Browse the repository at this point in the history
  • Loading branch information
str4d committed Jan 11, 2024
1 parent 3f7ab2f commit 0a6cb79
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 141 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,6 @@ jobs:
run: cargo build --release --locked --target ${{ matrix.target }} ${{ matrix.build_flags }}
working-directory: ./rage

- name: Generate completions
run: cargo run --example generate-completions

- name: Generate manpages
run: cargo run --example generate-docs

Expand Down
27 changes: 17 additions & 10 deletions rage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ assets = [
["target/release/rage", "usr/bin/", "755"],
["target/release/rage-keygen", "usr/bin/", "755"],
["target/release/rage-mount", "usr/bin/", "755"],
["../target/completions/rage.bash", "usr/share/bash-completion/completions/rage", "644"],
["../target/completions/rage-keygen.bash", "usr/share/bash-completion/completions/rage-keygen", "644"],
["../target/completions/rage-mount.bash", "usr/share/bash-completion/completions/rage-mount", "644"],
["../target/completions/rage.fish", "usr/share/fish/completions/", "644"],
["../target/completions/rage-keygen.fish", "usr/share/fish/completions/", "644"],
["../target/completions/rage-mount.fish", "usr/share/fish/completions/", "644"],
["../target/completions/rage.zsh", "usr/share/zsh/functions/Completion/Debian/", "644"],
["../target/completions/rage-keygen.zsh", "usr/share/zsh/functions/Completion/Debian/", "644"],
["../target/completions/rage-mount.zsh", "usr/share/zsh/functions/Completion/Debian/", "644"],
["target/release/completions/rage.bash", "usr/share/bash-completion/completions/rage", "644"],
["target/release/completions/rage-keygen.bash", "usr/share/bash-completion/completions/rage-keygen", "644"],
["target/release/completions/rage-mount.bash", "usr/share/bash-completion/completions/rage-mount", "644"],
["target/release/completions/rage.fish", "usr/share/fish/completions/", "644"],
["target/release/completions/rage-keygen.fish", "usr/share/fish/completions/", "644"],
["target/release/completions/rage-mount.fish", "usr/share/fish/completions/", "644"],
["target/release/completions/_rage", "usr/share/zsh/functions/Completion/Debian/", "644"],
["target/release/completions/_rage-keygen", "usr/share/zsh/functions/Completion/Debian/", "644"],
["target/release/completions/_rage-mount", "usr/share/zsh/functions/Completion/Debian/", "644"],
["../target/manpages/rage.1.gz", "usr/share/man/man1/", "644"],
["../target/manpages/rage-keygen.1.gz", "usr/share/man/man1/", "644"],
["../target/manpages/rage-mount.1.gz", "usr/share/man/man1/", "644"],
Expand Down Expand Up @@ -75,8 +75,15 @@ tar = { version = "0.4", optional = true }
time = { version = ">=0.3.7, <0.3.24", optional = true } # time 0.3.24 has MSRV 1.67
zip = { version = "0.6.2", optional = true }

[dev-dependencies]
[build-dependencies]
clap = { workspace = true, features = ["string", "unstable-styles"] }
clap_complete = "4"
i18n-embed = { workspace = true, features = ["desktop-requester"] }
i18n-embed-fl.workspace = true
lazy_static.workspace = true
rust-embed.workspace = true

[dev-dependencies]
flate2 = "1"
man = "0.3"
trycmd = "0.14"
Expand Down
82 changes: 82 additions & 0 deletions rage/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
use std::env;
use std::fs;
use std::io;
use std::path::Path;
use std::path::PathBuf;

use clap::{Command, CommandFactory, ValueEnum};
use clap_complete::{generate_to, Shell};

mod i18n {
include!("src/bin/rage/i18n.rs");
}
mod rage {
include!("src/bin/rage/cli.rs");
}
mod rage_keygen {
include!("src/bin/rage-keygen/cli.rs");
}
mod rage_mount {
include!("src/bin/rage-mount/cli.rs");
}

#[macro_export]
macro_rules! fl {
($message_id:literal) => {{
i18n_embed_fl::fl!($crate::i18n::LANGUAGE_LOADER, $message_id)
}};

($message_id:literal, $($args:expr),* $(,)?) => {{
i18n_embed_fl::fl!($crate::i18n::LANGUAGE_LOADER, $message_id, $($args), *)
}};
}

#[derive(Clone)]
struct Cli {
rage: Command,
rage_keygen: Command,
rage_mount: Command,
}

impl Cli {
fn build() -> Self {
Self {
rage: rage::AgeOptions::command(),
rage_keygen: rage_keygen::AgeOptions::command(),
rage_mount: rage_mount::AgeMountOptions::command(),
}
}

fn generate_completions(&mut self, out_dir: &Path) -> io::Result<()> {
fs::create_dir_all(out_dir)?;

for &shell in Shell::value_variants() {
generate_to(shell, &mut self.rage, "rage", out_dir)?;
generate_to(shell, &mut self.rage_keygen, "rage-keygen", out_dir)?;
generate_to(shell, &mut self.rage_mount, "rage-mount", out_dir)?;
}

Ok(())
}
}

fn main() -> io::Result<()> {
i18n::load_languages();

// `OUT_DIR` is "intentionally opaque as it is only intended for `rustc` interaction"
// (https://github.com/rust-lang/cargo/issues/9858). Peek into the black box and use
// it to figure out where the target directory is.
let out_dir = match env::var_os("OUT_DIR") {
None => return Ok(()),
Some(out_dir) => PathBuf::from(out_dir)
.ancestors()
.nth(3)
.expect("should be absolute path")
.to_path_buf(),
};

let mut cli = Cli::build();
cli.generate_completions(&out_dir.join("completions"))?;

Ok(())
}
127 changes: 0 additions & 127 deletions rage/examples/generate-completions.rs

This file was deleted.

2 changes: 1 addition & 1 deletion supply-chain/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ criteria = "safe-to-deploy"

[[exemptions.clap_complete]]
version = "4.3.2"
criteria = "safe-to-run"
criteria = "safe-to-deploy"

[[exemptions.clap_derive]]
version = "4.3.12"
Expand Down

0 comments on commit 0a6cb79

Please sign in to comment.