Skip to content

Commit 7f4cd2f

Browse files
committed
build-tools: Implement compile_schemas helper for compiling GSettings schemas
It generates the `gschemas.compiled` file by copying all `.gschema.xml` files from the specified directories into the `glib-2.0/schemas` cache directory and then running `glib-compile-schemas --strict` to compile them.
1 parent 93a073e commit 7f4cd2f

1 file changed

Lines changed: 70 additions & 1 deletion

File tree

glib-build-tools/src/lib.rs

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22

33
#![doc = include_str!("../README.md")]
44

5-
use std::{env, path::Path, process::Command};
5+
use gio::glib;
6+
use std::{
7+
env,
8+
path::{Path, PathBuf},
9+
process::Command,
10+
};
611

712
// rustdoc-stripper-ignore-next
813
/// Call to run `glib-compile-resources` to generate compiled gresources to embed
@@ -57,3 +62,67 @@ pub fn compile_resources<P: AsRef<Path>>(source_dirs: &[P], gresource: &str, tar
5762
println!("cargo:rerun-if-changed={dep}");
5863
}
5964
}
65+
66+
// rustdoc-stripper-ignore-next
67+
/// Call to run `glib-compile-schemas` to generate compiled gschemas from `.gschema.xml` schemas
68+
/// files from specified directories and store `gschemas.compiled` into `glib-2.0` schema cache
69+
/// directory.
70+
///
71+
/// ```no_run
72+
/// glib_build_tools::compile_schemas(
73+
/// &["schemas"],
74+
/// );
75+
/// ```
76+
pub fn compile_schemas(schemas_dir: &[&str]) {
77+
// Target Directory
78+
// Unix - $HOME/.local/share/glib-2.0/schemas/
79+
// Windows - C:/ProgramData/glib-2.0/schemas/
80+
81+
let prefix = if cfg!(windows) {
82+
PathBuf::from("C:/ProgramData")
83+
} else {
84+
glib::user_data_dir()
85+
};
86+
87+
let target_dir = Path::new(&prefix).join("glib-2.0").join("schemas");
88+
89+
// Ensure target_dir exists
90+
std::fs::create_dir_all(&target_dir).expect("Failed to create target directory");
91+
92+
// Recursively copy all files with .gschema.xml extension from schema_dir to target_dir
93+
for schema_dir in schemas_dir {
94+
let entries = Path::new(schema_dir)
95+
.read_dir()
96+
.expect("Failed to read schema directory")
97+
.flatten();
98+
99+
for entry in entries {
100+
let path = entry.path();
101+
let file_name = path.file_name().unwrap().to_str().unwrap();
102+
103+
if path.is_file() && file_name.ends_with(".gschema.xml") {
104+
let target_path = target_dir.join(path.file_name().unwrap());
105+
std::fs::copy(&path, &target_path).expect("Failed to copy schema file");
106+
}
107+
}
108+
}
109+
110+
let mut command = Command::new("glib-compile-schemas");
111+
command.arg("--strict");
112+
command.arg(target_dir);
113+
114+
let output = command
115+
.output()
116+
.expect("Failed to execute glib-compile-schemas");
117+
118+
assert!(
119+
output.status.success(),
120+
"glib-compile-schemas failed with exit status {} and stderr:\n{}",
121+
output.status,
122+
String::from_utf8_lossy(&output.stderr)
123+
);
124+
125+
for schema_dir in schemas_dir {
126+
println!("cargo:rerun-if-changed={}", schema_dir);
127+
}
128+
}

0 commit comments

Comments
 (0)