-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.rs
More file actions
262 lines (199 loc) · 7.5 KB
/
build.rs
File metadata and controls
262 lines (199 loc) · 7.5 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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
//! The building process.
//!
//! This script does two steps when building `gourd`.
//! 1. The shell completions are compiled and placed in
//! `[output_dir]/completions/`.
//! 2. If the feature `documentation` is on, user and maintainer documentation
//! will be compiled into `[output_dir]/manpages/`.
#![allow(unused)]
#![allow(clippy::missing_docs_in_private_items)]
use std::env;
use std::fmt::format;
use std::fs;
use std::fs::canonicalize;
use std::fs::File;
use std::fs::Permissions;
use std::io::Write;
#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;
use std::path::Path;
use std::process::Command as StdCommand;
use std::ptr::copy;
use anyhow::anyhow;
use anyhow::bail;
use anyhow::Context;
use anyhow::Result;
use clap::Command;
use clap::CommandFactory;
use clap_complete::generate_to;
use clap_complete::shells::Bash;
use clap_complete::shells::Fish;
use clap_complete::shells::PowerShell;
use clap_complete::shells::Zsh;
#[cfg(feature = "builtin-examples")]
include!("src/resources/build_builtin_examples.rs");
include!("src/gourd/cli/def.rs");
const GOURD_MANPAGE: &str = "docs/user/gourd.1.tex";
const GOURD_TOML_MANPAGE: &str = "docs/user/gourd.toml.5.tex";
const GOURD_TUTORIAL_MANPAGE: &str = "docs/user/gourd-tutorial.7.tex";
const MAINTAINER_DOCS: &str = "./maintainer.tex";
const MAINTAINER_DOCS_WORKDIR: &str = "docs/maintainer/";
const PREAMBLE: &str = include_str!("docs/user/html/preamble.html");
const POSTAMBLE: &str = include_str!("docs/user/html/postamble.html");
const STYLE: &str = include_str!("docs/user/html/manpage.css");
const INSTALLER: &str = include_str!("src/resources/install.sh");
const XETEX_OPTS: [&str; 3] = [
"-halt-on-error",
"-shell-escape",
"-interaction=nonstopmode",
];
const MANDOC_OPTS: [&str; 5] = [
"-I",
"os=\"rendered by mandoc\"",
"-Kutf-8",
"-Ofragment,toc",
"-Thtml",
];
fn main() -> Result<()> {
let outdir: PathBuf = match env::var_os("OUT_DIR") {
None => return Ok(()),
Some(outdir) => outdir,
}
.into();
let triple: String = match env::var_os("TARGET") {
None => return Ok(()),
Some(outdir) => outdir.to_str().map(|x| x.to_string()).unwrap(),
};
let target_dir = outdir.parent().unwrap().parent().unwrap().parent().unwrap();
let completions = target_dir.join("completions/");
let docs = target_dir.join("manpages/");
let _ = fs::create_dir(&completions);
let mut completions_command = Cli::command();
#[cfg(feature = "builtin-examples")]
{
let tars = target_dir.join("tarballs/");
completions_command = build_builtin_examples(&tars, completions_command)?;
}
generate_to(Bash, &mut completions_command, "gourd", &completions)?;
generate_to(Fish, &mut completions_command, "gourd", &completions)?;
generate_to(PowerShell, &mut completions_command, "gourd", &completions)?;
generate_to(Zsh, &mut completions_command, "gourd", &completions)?;
#[cfg(feature = "documentation")]
{
let _ = fs::create_dir(&docs);
println!("cargo::rerun-if-changed=docs/");
println!("cargo::rerun-if-changed=src/resources/install.sh");
println!("cargo::rerun-if-changed=src/resources/uninstall.sh");
let gourd = generate_man(GOURD_MANPAGE.parse()?, &docs)?;
#[cfg(feature = "documentation-latex")]
generate_pdf(GOURD_MANPAGE.parse()?, &docs)?;
generate_html(gourd, &docs)?;
let gourd_toml = generate_man(GOURD_TOML_MANPAGE.parse()?, &docs)?;
#[cfg(feature = "documentation-latex")]
generate_pdf(GOURD_TOML_MANPAGE.parse()?, &docs)?;
generate_html(gourd_toml, &docs)?;
let gourd_tutorial = generate_man(GOURD_TUTORIAL_MANPAGE.parse()?, &docs)?;
#[cfg(feature = "documentation-latex")]
generate_pdf(GOURD_TUTORIAL_MANPAGE.parse()?, &docs)?;
generate_html(gourd_tutorial, &docs)?;
#[cfg(feature = "documentation-latex")]
generate_latex(
MAINTAINER_DOCS.parse()?,
&docs,
Some(MAINTAINER_DOCS_WORKDIR.parse()?),
)?;
let installer = target_dir.join("generate-installer.sh");
let uninstaller = target_dir.join("uninstall.sh");
fs::write(&installer, INSTALLER.replace("{{triple}}", &triple));
#[cfg(unix)]
fs::set_permissions(&installer, Permissions::from_mode(0o755));
}
Ok(())
}
fn generate_man(doc_path: PathBuf, out_folder: &Path) -> Result<PathBuf> {
let output = out_folder.join(doc_path.with_extension("man").file_name().unwrap());
run_command(
"latex2man",
&vec![
"-t./docs/user/latex2man.trans",
"-M",
doc_path.to_str().unwrap(),
output.to_str().unwrap(),
],
None,
)?;
Ok(output)
}
fn generate_latex(
doc_path: PathBuf,
out_folder: &Path,
workdir: Option<PathBuf>,
) -> Result<PathBuf> {
let xetex_workdir = out_folder.join("xetex/");
let _ = fs::create_dir(&xetex_workdir);
let output_expected = xetex_workdir.join(doc_path.with_extension("pdf").file_name().unwrap());
let output_actual = out_folder.join(doc_path.with_extension("pdf").file_name().unwrap());
let mut opts = XETEX_OPTS.to_vec();
let output_dir_arg = format!("-output-directory={}", xetex_workdir.to_str().unwrap());
opts.push(&output_dir_arg);
opts.push(doc_path.to_str().unwrap());
run_command("xelatex", &opts, workdir.clone())?;
run_command("xelatex", &opts, workdir)?;
fs::copy(output_expected, &output_actual)?;
let _ = fs::remove_dir_all(&xetex_workdir);
Ok(output_actual)
}
fn generate_pdf(doc_path: PathBuf, out_folder: &Path) -> Result<PathBuf> {
let xetex_workdir = out_folder.join("xetex/");
let output_intr = xetex_workdir.join(doc_path.with_extension("tex").file_name().unwrap());
let _ = fs::create_dir(&xetex_workdir);
run_command(
"latex2man",
&vec![
"-L",
doc_path.to_str().unwrap(),
output_intr.to_str().unwrap(),
],
None,
)?;
generate_latex(output_intr, out_folder, None)
}
fn generate_html(man_path: PathBuf, out_folder: &Path) -> Result<PathBuf> {
let mut opts = MANDOC_OPTS.to_vec();
opts.push(man_path.to_str().unwrap());
let mut html = run_command("mandoc", &opts, None)?;
html = html.replace(
"gourd-tutorial(7)",
"<a class=\"manref\" href=\"./gourd-tutorial.7.html\">gourd-tutorial(7)</a>",
);
html = html.replace(
"gourd(1)",
"<a class=\"manref\" href=\"./gourd.1.html\">gourd(1)</a>",
);
html = html.replace(
"gourd.toml(5)",
"<a class=\"manref\" href=\"./gourd.toml.5.html\">gourd.toml(5)</a>",
);
let out_path = out_folder.join(man_path.with_extension("html").file_name().unwrap());
let style_path = out_folder.join("manpage.css");
fs::write(&out_path, format!("{PREAMBLE}{html}{POSTAMBLE}"))?;
fs::write(style_path, STYLE)?;
Ok(out_path)
}
fn run_command(cmd: &str, args: &Vec<&str>, workdir: Option<PathBuf>) -> Result<String> {
let mut actual = StdCommand::new(cmd);
if let Some(direr) = workdir {
actual.current_dir(direr);
}
actual.args(args);
println!("running {actual:?}");
let out = actual.output()?;
println!("command output: {}", String::from_utf8(out.stdout.clone())?);
if !out.status.success() {
panic!(
"Running {actual:?} failed, \nerr: {}",
String::from_utf8(out.stderr)?
);
}
String::from_utf8(out.stdout).context("")
}