-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.rs
82 lines (67 loc) · 2.08 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use std::path::{Path, PathBuf};
use std::{env, fs, io};
use clap_complete::Shell;
#[allow(dead_code)]
#[path = "src/cli.rs"]
mod cli;
fn main() -> io::Result<()> {
println!("cargo:rerun-if-env-changed=DIST_DIR");
println!("cargo:rerun-if-env-changed=PROFILE");
println!("cargo:rerun-if-changed=src/cli.rs");
let profile = env::var_os("PROFILE");
if profile != Some("release".into()) {
println!(
"cargo:warning=\
generating man page and shell completions only in release mode, \
current: PROFILE={}",
profile.unwrap_or_default().to_string_lossy()
);
} else {
let name = env::var("CARGO_PKG_NAME")
.ok()
.ok_or(io::ErrorKind::NotFound)?;
let destination = env::var_os("DIST_DIR")
.or_else(|| env::var_os("OUT_DIR"))
.ok_or(io::ErrorKind::NotFound)?;
let destination = PathBuf::from(destination);
if !destination.exists() {
fs::create_dir_all(&destination)?;
}
println!(
"cargo:warning=\
generating man page and shell completions to {}",
destination.display()
);
gen_man(&name, &destination)?;
gen_completion(&name, &destination, Shell::Bash)?;
gen_completion(&name, &destination, Shell::Fish)?;
gen_completion(&name, &destination, Shell::Elvish)?;
gen_completion(&name, &destination, Shell::Zsh)?;
}
Ok(())
}
fn gen_man(
name: impl AsRef<str>,
destination: impl AsRef<Path>,
) -> io::Result<()> {
let man = clap_mangen::Man::new(cli::build(true));
let mut buffer: Vec<u8> = Default::default();
man.render(&mut buffer)?;
fs::write(
destination.as_ref().join(format!("{}.1", name.as_ref())),
buffer,
)
}
fn gen_completion(
name: impl AsRef<str>,
destination: impl AsRef<Path>,
shell: Shell,
) -> io::Result<()> {
clap_complete::generate_to(
shell,
&mut cli::build(true),
name.as_ref(),
destination.as_ref(),
)
.map(|_| ())
}