-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbuild.rs
175 lines (149 loc) · 4.99 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
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
// Copyright Sebastian Wiesner <[email protected]>
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
use std::io::{Error, ErrorKind, Result};
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
fn glob_io(pattern: &str) -> Result<Vec<PathBuf>> {
glob::glob(pattern)
.map_err(|err| Error::new(ErrorKind::Other, err))?
.map(|item| item.map_err(|err| Error::new(ErrorKind::Other, err)))
.collect::<Result<Vec<PathBuf>>>()
}
trait CommandExt {
fn checked(&mut self);
}
impl CommandExt for Command {
fn checked(&mut self) {
let status = self.status().unwrap();
if !status.success() {
panic!("Command {:?} failed with status {status}", self);
}
}
}
/// Compile all blueprint files.
fn compile_blueprint() -> Vec<PathBuf> {
let blueprint_files = glob_io("resources/**/*.blp").unwrap();
if let Some("1") | Some("true") = std::env::var("SKIP_BLUEPRINT").ok().as_deref() {
println!("cargo::warning=Skipping blueprint compilation, falling back to committed files.");
} else {
Command::new("blueprint-compiler")
.args(["batch-compile", "resources", "resources"])
.args(&blueprint_files)
.checked();
}
blueprint_files
}
/// Run `msgfmt` over a template file to merge translations with the template.
fn msgfmt_template<P: AsRef<Path>>(template: P) {
let target = template.as_ref().with_extension("");
let mode = match target.extension().and_then(|e| e.to_str()) {
Some("desktop") => "--desktop",
Some("xml") => "--xml",
other => panic!("Unsupported template extension: {:?}", other),
};
Command::new("msgfmt")
.args([mode, "--template"])
.arg(template.as_ref())
.args(["-d", "po", "--output"])
.arg(target)
.checked();
}
fn msgfmt() -> Vec<PathBuf> {
let po_files: Vec<PathBuf> = glob::glob("po/*.po")
.unwrap()
.collect::<std::result::Result<_, _>>()
.unwrap();
let msgfmt_exists = Command::new("msgfmt")
.arg("--version")
.status()
.is_ok_and(|status| status.success());
let templates = &[
Path::new("resources/de.swsnr.turnon.metainfo.xml.in").to_owned(),
Path::new("de.swsnr.turnon.desktop.in").to_owned(),
];
if msgfmt_exists {
for file in templates {
msgfmt_template(file);
}
} else {
println!("cargo::warning=msgfmt not found; using untranslated desktop and metainfo file.");
for file in templates {
std::fs::copy(file, file.with_extension("")).unwrap();
}
}
let mut sources = po_files;
sources.push("po/LINGUAS".into());
sources.extend_from_slice(templates);
sources
}
pub fn compile_resources<P: AsRef<Path>>(
source_dirs: &[P],
gresource: &str,
target: &str,
) -> Vec<PathBuf> {
let out_dir = std::env::var("OUT_DIR").unwrap();
let out_dir = Path::new(&out_dir);
let mut command = Command::new("glib-compile-resources");
for source_dir in source_dirs {
command.arg("--sourcedir").arg(source_dir.as_ref());
}
command
.arg("--target")
.arg(out_dir.join(target))
.arg(gresource)
.checked();
let mut command = Command::new("glib-compile-resources");
for source_dir in source_dirs {
command.arg("--sourcedir").arg(source_dir.as_ref());
}
let output = command
.arg("--generate-dependencies")
.arg(gresource)
.stderr(Stdio::inherit())
.output()
.unwrap()
.stdout;
let mut sources = vec![Path::new(gresource).into()];
for line in String::from_utf8(output).unwrap().lines() {
if line.ends_with(".ui") {
// We build UI files from blueprint, so adapt the dependency
sources.push(Path::new(line).with_extension("blp"))
} else if line.ends_with(".metainfo.xml") {
sources.push(Path::new(line).with_extension("xml.in"));
} else {
sources.push(line.into());
}
}
sources
}
fn glib_compile_schemas() -> Vec<PathBuf> {
let schemas = glob_io("schemas/*.gschema.xml").unwrap();
Command::new("glib-compile-schemas")
.args(["--strict", "schemas"])
.checked();
schemas
}
fn main() {
let tasks = [
std::thread::spawn(compile_blueprint),
std::thread::spawn(glib_compile_schemas),
std::thread::spawn(msgfmt),
];
let mut sources = tasks
.into_iter()
.flat_map(|task| task.join().unwrap())
.collect::<Vec<_>>();
sources.extend_from_slice(
compile_resources(
&["resources"],
"resources/resources.gresource.xml",
"turnon.gresource",
)
.as_slice(),
);
for source in sources {
println!("cargo:rerun-if-changed={}", source.display());
}
}