From 90b604a850d44c7b53159f9b747576ea46b4b5c9 Mon Sep 17 00:00:00 2001 From: Caleb Maclennan Date: Thu, 21 Sep 2023 16:34:58 +0300 Subject: [PATCH] feat(cli): Generate man page --- Cargo.lock | 17 +++++++++++++++++ Cargo.toml | 3 +++ build.rs | 21 +++++++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 7c3f7cee..d42fdd81 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -177,6 +177,7 @@ dependencies = [ "assert_cmd", "clap", "clap_complete", + "clap_mangen", "colored", "config", "fluent", @@ -262,6 +263,16 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2da6da31387c7e4ef160ffab6d5e7f00c42626fe39aea70a7b0f1773f7dd6c1b" +[[package]] +name = "clap_mangen" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b44f35c514163027542f7147797ff930523eea288e03642727348ef1a9666f6b" +dependencies = [ + "clap", + "roff", +] + [[package]] name = "clru" version = "0.6.1" @@ -1720,6 +1731,12 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "roff" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b833d8d034ea094b1ea68aa6d5c740e0d04bad9d16568d08ba6f76823a114316" + [[package]] name = "rust-embed" version = "6.8.1" diff --git a/Cargo.toml b/Cargo.toml index 17e1061a..2b2e4b37 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,6 +31,9 @@ unic-langid = "0.9" version = "4.3" features = [ "derive", "wrap_help" ] + [build-dependencies.clap_mangen] + version = "0.2" + [dependencies.git2] version = "0.17" default-features = false diff --git a/build.rs b/build.rs index c0e6c306..9caab759 100644 --- a/build.rs +++ b/build.rs @@ -1,6 +1,7 @@ use clap::Command; use clap_complete::generator::generate_to; use clap_complete::shells::{Bash, Elvish, Fish, PowerShell, Zsh}; +use clap_mangen::Man; use std::{collections, env, fs}; use vergen::EmitBuilder; @@ -16,9 +17,29 @@ fn main() { }; builder.emit().expect("Unable to generate the cargo keys!"); pass_on_configure_details(); + generate_manpage(); generate_shell_completions(); } +/// Generate man page +fn generate_manpage() { + let out_dir = match env::var_os("OUT_DIR") { + None => return, + Some(out_dir) => out_dir, + }; + let manpage_dir = path::Path::new(&out_dir); + fs::create_dir_all(manpage_dir).expect("Unable to create directory for generated manpages"); + let bin_name: &str = "casile"; + let cli = Command::new("casile"); + let cli = Cli::augment_args(cli); + let man = Man::new(cli); + let mut buffer: Vec = Default::default(); + man.render(&mut buffer) + .expect("Unable to render man page to UTF-8 string"); + fs::write(manpage_dir.join(format!("{bin_name}.1")), buffer) + .expect("Unable to write manepage to file"); +} + /// Generate shell completion files from CLI interface fn generate_shell_completions() { let out_dir = match env::var_os("OUT_DIR") {