bottom/build.rs
Clement Tsang f2b8b4e842
other: add manpage file to cargo deb config, move back to build script (#693)
Adds the asset for the manpage to cargo deb config. Also moves the generated manpage file to a .1.gz file. Also, moves back to a build script since that was causing some issues for the automatic Cargo.toml fields detection for manpage and completion generation.

To prevent compilation from happening every time, and only in CI, we use an env var to avoid generation steps.
2022-03-11 06:45:05 -05:00

55 lines
1.7 KiB
Rust

use clap_complete::{generate_to, shells::Shell};
use std::{
env, fs,
io::Result,
path::{Path, PathBuf},
};
include!("src/clap.rs");
fn create_dir(dir: &Path) -> Result<()> {
let res = fs::create_dir_all(&dir);
match &res {
Ok(()) => {}
Err(err) => {
eprintln!(
"Failed to create a directory at location {:?}, encountered error {:?}. Aborting...",
dir, err
);
}
}
res
}
fn main() -> Result<()> {
if env::var_os("GENERATE").is_some() {
// OUT_DIR is where extra build files are written to for Cargo.
let completion_out_dir = PathBuf::from("completion");
let manpage_out_dir = PathBuf::from("manpage");
create_dir(&completion_out_dir)?;
create_dir(&manpage_out_dir)?;
// Generate completions
let mut app = build_app();
generate_to(Shell::Bash, &mut app, "btm", &completion_out_dir)?;
generate_to(Shell::Zsh, &mut app, "btm", &completion_out_dir)?;
generate_to(Shell::Fish, &mut app, "btm", &completion_out_dir)?;
generate_to(Shell::PowerShell, &mut app, "btm", &completion_out_dir)?;
generate_to(Shell::Elvish, &mut app, "btm", &completion_out_dir)?;
// Generate manpage
let app = app.name("btm");
let man = clap_mangen::Man::new(app);
let mut buffer: Vec<u8> = Default::default();
man.render(&mut buffer)?;
std::fs::write(manpage_out_dir.join("btm.1"), buffer)?;
}
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=./src/clap.rs");
println!("cargo:rerun-if-env-changed=GENERATE");
Ok(())
}