forked from langston-barrett/souffle-lint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
87 lines (75 loc) · 2.31 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
83
84
85
86
87
// ReferencelOs:
// https://github.com/sondr3/clap-man-example/blob/main/build.rs
// https://github.com/clap-rs/clap/issues/4231
// https://github.com/clap-rs/clap/discussions/3603
use clap::{Command, CommandFactory};
use clap_mangen::Man;
use glob::glob;
use std::{
env,
fs::File,
io::Write,
path::{Path, PathBuf},
};
include!("src/cli.rs");
// fn build_manpages(outdir: &Path) -> Result<(), Error> {
// let file = Path::new(&outdir).join("souffle-lint.1");
// let mut file = File::create(&file)?;
// Man::new(Args::command()).render(&mut file)?;
// Ok(())
// }
fn print_manpages(dir: &Path) {
fn print(dir: &Path, app: &Command) {
// `get_display_name()` is `Some` for all instances, except the root.
let name = app.get_display_name().unwrap_or_else(|| app.get_name());
let mut out = File::create(dir.join(format!("{name}.1"))).unwrap();
Man::new(app.clone()).render(&mut out).unwrap();
out.flush().unwrap();
for sub in app.get_subcommands() {
print(dir, sub);
}
}
let mut app = Args::command();
app.build();
print(dir, &app)
}
fn target_assets() -> Option<PathBuf> {
let outdir = match env::var_os("OUT_DIR") {
None => return None,
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();
Some(path.to_path_buf())
}
fn clap_man(path: &Path) {
println!("cargo:rerun-if-changed=src/cli.rs");
println!("cargo:rerun-if-changed=man");
print_manpages(path);
}
fn sphinx_man(path: &Path) -> std::process::Output {
for rst in glob("doc/*.rst").expect("Failed to read glob pattern") {
println!(
"cargo:rerun-if-changed=doc{}",
rst.expect("Glob").to_string_lossy()
);
}
std::process::Command::new("make")
.args([
"-C",
"doc",
"man",
&format!("BUILDDIR={}", path.to_string_lossy()),
])
.output()
.expect("failed to execute `make -C doc man`")
}
fn main() {
let mut assets = target_assets().unwrap();
clap_man(&assets);
assets.push("doc");
sphinx_man(&assets);
}