Skip to content

Commit d7acbd3

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 d7acbd3

File tree

1 file changed

+76
-1
lines changed

1 file changed

+76
-1
lines changed

glib-build-tools/src/lib.rs

Lines changed: 76 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,73 @@ 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+
/// None
75+
/// );
76+
/// ```
77+
pub fn compile_schemas(schemas_dir: &[&str], target_dir: Option<&str>) {
78+
// Target Directory
79+
// Unix - $HOME/.local/share/glib-2.0/schemas/
80+
// Windows - C:/ProgramData/glib-2.0/schemas/
81+
82+
let target_dir = match target_dir {
83+
Some(base) => PathBuf::from(base),
84+
None => {
85+
let prefix = if cfg!(windows) {
86+
PathBuf::from("C:/ProgramData")
87+
} else {
88+
glib::user_data_dir()
89+
};
90+
91+
Path::new(&prefix).join("glib-2.0").join("schemas")
92+
}
93+
};
94+
95+
// Ensure target_dir exists
96+
std::fs::create_dir_all(&target_dir).expect("Failed to create target directory");
97+
98+
// Recursively copy all files with .gschema.xml extension from schema_dir to target_dir
99+
for schema_dir in schemas_dir {
100+
let entries = Path::new(schema_dir)
101+
.read_dir()
102+
.expect("Failed to read schema directory")
103+
.flatten();
104+
105+
for entry in entries {
106+
let path = entry.path();
107+
let file_name = path.file_name().unwrap().to_str().unwrap();
108+
109+
if path.is_file() && file_name.ends_with(".gschema.xml") {
110+
let target_path = target_dir.join(path.file_name().unwrap());
111+
std::fs::copy(&path, &target_path).expect("Failed to copy schema file");
112+
}
113+
}
114+
}
115+
116+
let mut command = Command::new("glib-compile-schemas");
117+
command.arg("--strict");
118+
command.arg(target_dir);
119+
120+
let output = command
121+
.output()
122+
.expect("Failed to execute glib-compile-schemas");
123+
124+
assert!(
125+
output.status.success(),
126+
"glib-compile-schemas failed with exit status {} and stderr:\n{}",
127+
output.status,
128+
String::from_utf8_lossy(&output.stderr)
129+
);
130+
131+
for schema_dir in schemas_dir {
132+
println!("cargo:rerun-if-changed={}", schema_dir);
133+
}
134+
}

0 commit comments

Comments
 (0)