bottom/build.rs
Clement Tsang 14808b3a2e
ci: completion/manpage generation script spring cleaning (#795)
* ci: spring cleaning of completions autogen

This commit changes a few things/cleans up stuff:
- Completion and manpage generation now drops the files off in
  `./target/tmp/bottom` rather than arbitrarily in the build directory.
  This was originally done because I was lazy and just needed it to work
  in CI, but it's kinda gross if you want to build the manpages in your
  own directory.
- CI was updated to handle this.
- Only run if the `BTM_GENERATE` env var is actually non-empty.

* docs: update for manpage/completion gen

* ci: auto delete autogen comp/manpage dir

* ci: fix incorrect mv for autogen

The mv was too late, should be earlier in the workflow.

* ci: specify shell in autogen delete

* docs: more updates to manpage/comp docs

* ci: unify env vars

* ci: skip autogen on build-msi
2022-08-30 18:25:16 -04:00

62 lines
2 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<()> {
const COMPLETION_DIR: &str = "target/tmp/bottom/completion";
const MANPAGE_DIR: &str = "target/tmp/bottom/manpage";
match env::var_os("BTM_GENERATE") {
Some(var) if !var.is_empty() => {
let completion_out_dir = PathBuf::from(COMPLETION_DIR);
let manpage_out_dir = PathBuf::from(MANPAGE_DIR);
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-changed=.{}", COMPLETION_DIR);
println!("cargo:rerun-if-changed=.{}", MANPAGE_DIR);
println!("cargo:rerun-if-env-changed=BTM_GENERATE");
Ok(())
}