-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
90 lines (72 loc) · 3 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
use git_version::git_version;
use std::{env, fs, path::Path, process::Command};
fn render_schema(gschema: &str, out_dir: &Path) {
let gettext_package = env::var("GETTEXT_PACKAGE").unwrap();
let app_id = env::var("APP_ID").unwrap();
let contents = fs::read_to_string(gschema).unwrap();
let new = contents.replace("@gettext-package@", &gettext_package).replace("@app-id@", &app_id);
let filename = Path::new(gschema).file_name().unwrap();
let outfile = Path::new(filename).file_stem().unwrap();
let outpath = out_dir.join(outfile);
fs::write(outpath.clone(), new).unwrap();
let outpath_file = outpath.to_string_lossy();
println!("cargo:rerun-if-changed={gschema}");
println!("cargo:rerun-if-changed={outpath_file}");
}
pub fn compile_schemas<P: AsRef<Path>>(gschemas: &[P]) {
let out_dir = env::var("OUT_DIR").unwrap();
let out_dir = Path::new(&out_dir);
for gschema in gschemas {
render_schema(&gschema.as_ref().to_string_lossy(), out_dir);
}
let schema_dir = env::var("GSETTINGS_SCHEMA_DIR").unwrap();
let output = Command::new("glib-compile-schemas")
.arg("--strict")
.arg("--targetdir")
.arg(schema_dir)
.arg(out_dir)
.output()
.unwrap();
assert!(
output.status.success(),
"glib-compile-schemas failed with exit status {} and stderr:\n{}",
output.status,
String::from_utf8_lossy(&output.stderr)
);
}
fn quote(input: &str) -> String {
format!(r#""{}""#, input)
}
fn render_config() {
let app_id = env::var("APP_ID").unwrap();
let gettext_package = env::var("GETTEXT_PACKAGE").unwrap();
let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
let src_dir = Path::new(&manifest_dir).join("src");
let locale_dir = Path::new(&manifest_dir).join("po");
let data_dir = Path::new(&manifest_dir).join("data");
let out_dir = env::var("OUT_DIR").unwrap();
let config_in_path = src_dir.join("config.rs.in");
let config_out_path = src_dir.join("config.rs");
let contents = fs::read_to_string(&config_in_path).unwrap();
let new = contents
.replace("@APP_ID@", "e(&app_id))
.replace("@GETTEXT_PACKAGE@", "e(&gettext_package))
.replace("@LOCALEDIR@", "e(&locale_dir.to_string_lossy()))
.replace("@PKGDATADIR@", "e(&data_dir.to_string_lossy()))
.replace("@RESOURCEDIR@", "e(&out_dir))
.replace("@PROFILE@", "Devel")
.replace("@VERSION@", "e(git_version!()));
fs::write(config_out_path, new).unwrap();
let config_in = config_in_path.to_string_lossy();
println!("cargo:rerun-if-changed={config_in}");
}
fn compile_glib() {
glib_build_tools::compile_resources(&["data/resources/"], "data/resources/resources.gresource.xml", "resources.gresource");
compile_schemas(&["data/io.github.vhdirk.Terms.gschema.xml.in"]);
}
fn main() {
if !env::var("BUILD_IS_MESON").is_ok_and(|val| val == "true") {
compile_glib();
render_config();
}
}