-
-
Notifications
You must be signed in to change notification settings - Fork 74
/
build.rs
50 lines (46 loc) · 1.18 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#[path = "src/args.rs"]
mod args;
use clap::ValueEnum;
use clap_complete::{self, Shell};
use clap_mangen::Man;
use std::env;
use std::error::Error;
use std::fs::{self, File};
use std::io::Error as IoError;
use std::path::{Path, PathBuf};
fn build_shell_completions(out_dir: &Path) -> Result<(), IoError> {
fs::create_dir_all(out_dir)?;
let mut app = args::get_args();
let shells = Shell::value_variants();
for shell in shells {
clap_complete::generate_to(
*shell,
&mut app,
env!("CARGO_PKG_NAME"),
out_dir,
)?;
}
Ok(())
}
fn build_manpage(out_dir: &Path) -> Result<(), IoError> {
fs::create_dir_all(out_dir)?;
let app = args::get_args();
let file = out_dir.join(format!("{}.8", env!("CARGO_PKG_NAME")));
let mut file = File::create(file)?;
Man::new(app).render(&mut file)?;
Ok(())
}
fn main() -> Result<(), Box<dyn Error>> {
println!("cargo:rerun-if-changed=src/args.rs");
let out_dir = match env::var_os("OUT_DIR").map(PathBuf::from) {
None => return Ok(()),
Some(v) => v
.ancestors()
.nth(4)
.expect("failed to determine out dir")
.to_owned(),
};
build_manpage(&out_dir.join("man"))?;
build_shell_completions(&out_dir.join("completions"))?;
Ok(())
}