-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
95 lines (79 loc) · 2.78 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
use clap::CommandFactory;
use clap_complete::{self, shells::Shell};
use clap_mangen::Man;
use std::{env, error, fmt, fs, io, process::ExitCode};
include!("src/cli.rs");
const BINARY_NAME: &str = "schoenerd";
const MAN_FILENAME: &str = "schoenerd.1";
const SUPPORTED_SHELLS: [Shell; 3] = [Shell::Bash, Shell::Fish, Shell::Zsh];
#[derive(Debug)]
enum BuildError {
OutDirVar,
CreateDir { path: PathBuf, error: io::Error },
CompletionGen { shell: Shell, error: io::Error },
ManualRender(io::Error),
WriteManualFile { path: PathBuf, error: io::Error },
}
impl fmt::Display for BuildError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
BuildError::OutDirVar => f.write_str("OUT_DIR environment variable not set"),
BuildError::CreateDir { path, error } => write!(
f,
"unable to create '{path}' directory: {error}",
path = path.display()
),
BuildError::CompletionGen { shell, error } => {
write!(f, "unable to generate {shell} shell completions: {error}")
}
BuildError::ManualRender(error) => {
write!(f, "unable to render man page: {error}")
}
BuildError::WriteManualFile { path, error } => write!(
f,
"unable to write man file to '{path}': {error}",
path = path.display()
),
}
}
}
impl error::Error for BuildError {}
fn build() -> Result<(), BuildError> {
let out_dir = match env::var_os("OUT_DIR") {
Some(path) => PathBuf::from(path),
None => return Err(BuildError::OutDirVar),
};
// ---- COMPLETIONS ----
let comp_path = out_dir.join("completions");
fs::create_dir_all(&comp_path).map_err(|error| BuildError::CreateDir {
path: comp_path.clone(),
error,
})?;
let mut cmd = Cli::command();
for shell in SUPPORTED_SHELLS {
clap_complete::generate_to(shell, &mut cmd, BINARY_NAME, &comp_path)
.map_err(|error| BuildError::CompletionGen { shell, error })?;
}
// ---- MANUAL ----
let mut man_path = out_dir.join("man");
fs::create_dir_all(&man_path).map_err(|error| BuildError::CreateDir {
path: man_path.clone(),
error,
})?;
let man = Man::new(cmd);
let mut buffer: Vec<u8> = Default::default();
man.render(&mut buffer).map_err(BuildError::ManualRender)?;
man_path.push(MAN_FILENAME);
fs::write(&man_path, buffer).map_err(|error| BuildError::WriteManualFile {
path: man_path.clone(),
error,
})?;
Ok(())
}
fn main() -> ExitCode {
if let Err(error) = build() {
eprintln!("error: {error}");
return ExitCode::FAILURE;
}
ExitCode::SUCCESS
}