Skip to content

Commit

Permalink
Add xtask installing man pages
Browse files Browse the repository at this point in the history
  • Loading branch information
cschwan committed Aug 31, 2023
1 parent 4552eea commit 087b7b7
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[alias]
xtask = "run --package xtask --"
10 changes: 10 additions & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ members = [
"pineappl_capi",
"pineappl_cli",
"pineappl_fastnlo",
"pineappl_py"
"pineappl_py",
"xtask",
]
default-members = [
"pineappl",
Expand Down
19 changes: 19 additions & 0 deletions xtask/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "xtask"
publish = false

categories.workspace = true
edition.workspace = true
keywords.workspace = true
license.workspace = true
repository.workspace = true
rust-version.workspace = true
version.workspace = true

[dependencies]
anyhow = "1.0.68"
clap = { features = ["string"], version = "4.2.0" }
clap_mangen = "0.2.7"
#git2 = "0.17.2"
#semver = "1.0.17"
pineappl_cli = { path = "../pineappl_cli", version = "0.6.1" }
55 changes: 55 additions & 0 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use anyhow::Result;
use std::env;
use std::path::Path;

fn main() -> Result<()> {
let args: Vec<_> = env::args().skip(1).collect();

match args.as_slice() {
[cmd, path] if cmd == "install-manpages" => install_manpages(Path::new(path)),
_ => todo!(),
}
}

fn install_manpages(path: &Path) -> Result<()> {
use clap::CommandFactory;
use clap_mangen::Man;
use std::fs::File;
use std::io::BufWriter;

fn render_manpages(path: &Path, cmd: &clap::Command) -> Result<()> {
let name = cmd
.get_bin_name()
.unwrap_or(cmd.get_name())
.replace(" ", "-");
let version = cmd
.get_version()
.map(|version| version.strip_prefix('v').unwrap().to_string());

// set the correct name of the command
let mut man_cmd = cmd.clone().name(name.clone());

// if there is a version, remove the 'v'
if let Some(version) = version {
man_cmd = man_cmd.version(version);
}

Man::new(man_cmd).render(&mut BufWriter::new(File::create(
path.join(format!("{name}.1")),
)?))?;

for subcmd in cmd.get_subcommands() {
render_manpages(path, subcmd)?;
}

Ok(())
}

let mut cmd = pineappl_cli::Opts::command();
// this is needed so subcommands return the correct `bin_name`
cmd.build();

render_manpages(path, &cmd)?;

Ok(())
}

0 comments on commit 087b7b7

Please sign in to comment.