-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.rs
More file actions
65 lines (50 loc) · 1.6 KB
/
build.rs
File metadata and controls
65 lines (50 loc) · 1.6 KB
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
use clap::CommandFactory;
use clap_complete::generate_to;
use clap_complete::Shell;
use clap_mangen::Man;
use std::{
env,
fs::File,
io::Error,
path::{Path, PathBuf},
};
include!("src/cli.rs");
fn build_shell_completion(outdir: &Path) -> Result<(), Error> {
let mut app = Cli::command();
app.build();
let outdir = outdir.join("bash-completions/completions");
std::fs::create_dir_all(&outdir).unwrap();
generate_to(Shell::Bash, &mut app, "ctt", outdir)?;
Ok(())
}
fn build_manpages(outdir: &Path) -> Result<(), Error> {
let mut app = Cli::command();
app.build();
let outdir = outdir.join("man/man1");
std::fs::create_dir_all(&outdir).unwrap();
let file = Path::new(&outdir).join("ctt.1");
let mut file = File::create(file)?;
Man::new(app.clone()).render(&mut file)?;
for sub in app.get_subcommands() {
let file = Path::new(&outdir).join(format!("ctt-{}.1", sub));
let mut file = File::create(&file)?;
Man::new(sub.clone()).render(&mut file)?;
}
Ok(())
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("cargo:rerun-if-changed=src/main.rs");
println!("cargo:rerun-if-changed=man");
let outdir = match env::var_os("OUT_DIR") {
None => return Ok(()),
Some(outdir) => outdir,
};
// Create `target/assets/` folder.
let out_path = PathBuf::from(outdir);
let mut path = out_path.ancestors().nth(4).unwrap().to_owned();
path.push("assets");
std::fs::create_dir_all(&path).unwrap();
build_shell_completion(&path)?;
build_manpages(&path)?;
Ok(())
}