-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathbuild.rs
109 lines (104 loc) · 3.4 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//! This file is executed during compilation.
//! It builds shell completion scripts and the man page
//!
//! Note: to see the eprintln messages, run cargo with
//! cargo -vv build --release
use {
dysk_cli::args::Args,
clap::CommandFactory,
clap_complete::{Generator, Shell},
serde::Deserialize,
std::{
env,
ffi::OsStr,
fs,
path::PathBuf,
},
};
fn write_completions_file<G: Generator + Copy, P: AsRef<OsStr>>(generator: G, out_dir: P) {
let mut args = Args::command();
clap_complete::generate_to(
generator,
&mut args,
"dysk".to_string(),
&out_dir,
).expect("clap complete generation failed");
}
/// write the shell completion scripts which will be added to
/// the release archive
fn build_completion_scripts() {
let out_dir = env::var_os("OUT_DIR").expect("out dir not set");
write_completions_file(Shell::Bash, &out_dir);
write_completions_file(Shell::Elvish, &out_dir);
write_completions_file(Shell::Fish, &out_dir);
write_completions_file(Shell::Zsh, &out_dir);
eprintln!("completion scripts generated in {out_dir:?}");
}
/// generate the man page from the Clap configuration
fn build_man_page() -> std::io::Result<()> {
let out_dir = env::var_os("OUT_DIR").expect("out dir not set");
let out_dir = PathBuf::from(out_dir);
let cmd = Args::command();
let man = clap_mangen::Man::new(cmd);
let mut buffer = Vec::<u8>::default();
man.render(&mut buffer)?;
let file_path = out_dir.join("dysk.1");
std::fs::write(&file_path, buffer)?;
eprintln!("map page generated in {file_path:?}");
Ok(())
}
/// Check that all dysk versions are the same
///
/// See <https://github.com/Canop/dysk/issues/65>
fn check_version_consistency() -> std::io::Result<()> {
#[derive(Deserialize)]
struct Package {
version: String,
}
#[derive(Deserialize)]
struct DependencyRef {
version: String,
}
#[derive(Deserialize)]
struct Dependencies {
#[serde(alias = "dysk-cli")]
dysk_cli: DependencyRef,
}
#[derive(Deserialize)]
struct MainCargo {
package: Package,
dependencies: Dependencies,
#[serde(alias = "build-dependencies")]
build_dependencies: Dependencies,
}
#[derive(Deserialize)]
struct CliCargo {
package: Package,
}
let version = env::var("CARGO_PKG_VERSION").expect("cargo pkg version not available");
let s = fs::read_to_string("Cargo.toml").unwrap();
let main_cargo: MainCargo = toml::from_str(&s).unwrap();
let Ok(s) = fs::read_to_string("cli/Cargo.toml") else {
// won't be visible unless run with -vv
eprintln!("No local cli/Cargo.toml -- Assuming a cargo publish compilation");
return Ok(());
};
let cli_cargo: CliCargo = toml::from_str(&s).unwrap();
let ok =
(version == main_cargo.package.version)
&& (version == main_cargo.dependencies.dysk_cli.version)
&& (version == main_cargo.build_dependencies.dysk_cli.version)
&& (version == cli_cargo.package.version);
if ok {
eprintln!("Checked consistency of dysk and dysk-cli versions: OK");
} else {
panic!("VERSION MISMATCH - All dysk and dysk-cli versions must be the same");
}
Ok(())
}
fn main() -> std::io::Result<()> {
check_version_consistency()?;
build_completion_scripts();
build_man_page()?;
Ok(())
}