Skip to content

Commit e6ec9aa

Browse files
committed
List the editor's QML in a manifest the website can build from
blockwork-qt/qml/module.json groups the module's files into the editor (EditorPage and everything it reaches), the window chrome a host replaces, and the rest of the desktop app, plus the BlockworkStyle module and the editor's icon. build.rs builds the app from it and fails when the editor group misses a file or style module it uses. cmake/BlockworkEditorQml.cmake packages the editor half for CMake hosts like the website's demo. Moves blockstitch to fe7bb7a, which adds its own manifest and CMake entry point, and brings the flatpak's blockstitch source back in step with Cargo.toml.
1 parent 988af1b commit e6ec9aa

8 files changed

Lines changed: 379 additions & 61 deletions

File tree

‎Cargo.lock‎

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ version = "0.6.0"
2020
edition = "2024"
2121

2222
[workspace.dependencies]
23-
blockstitch-qml = { git = "https://github.com/Blockworked/blockstitch", rev = "0be3cd86a1bf569cb2ea542dc3a48ca32f7eb625" }
23+
blockstitch-qml = { git = "https://github.com/Blockworked/blockstitch", rev = "fe7bb7ad196387089ea348519ca635768c340844" }
2424
blockstitch-core = {
2525
git = "https://github.com/Blockworked/blockstitch",
26-
rev = "0be3cd86a1bf569cb2ea542dc3a48ca32f7eb625",
26+
rev = "fe7bb7ad196387089ea348519ca635768c340844",
2727
#path = "../blockstitch/crates/blockstitch-core"
2828
}
2929
tracing = "0.1.44"

‎blockwork-qt/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ cxx-qt-build = { version = "0.10", features = ["link_qt_object_files"] }
2828
lucide-qt = { git = "https://github.com/Blockworked/lucide-qt", tag = "v0.1.0" }
2929
qt-build-utils = { version = "0.10", features = ["qt_minimal"] }
3030
qt-version = { version = "0.1", features = ["qt_version_at_least_6_10"] }
31+
serde_json.workspace = true
3132

3233
[target.'cfg(windows)'.build-dependencies]
3334
winres = "0.1"

‎blockwork-qt/build.rs‎

Lines changed: 186 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use cxx_qt_build::{CxxQtBuilder, QmlFile, QmlModule};
22
use qt_build_utils::{QResource, QResourceFile, QResources};
3+
use serde_json::Value;
4+
use std::collections::BTreeSet;
5+
use std::path::Path;
36

47
fn main() {
58
#[cfg(windows)]
@@ -14,52 +17,63 @@ fn main() {
1417
res.compile().expect("failed to embed Windows icon");
1518
}
1619

17-
let blockwork = [
18-
"qml/Main.qml",
19-
"qml/EditorPage.qml",
20-
"qml/SettingsPage.qml",
21-
"qml/OnboardingPage.qml",
22-
"qml/SectionCard.qml",
23-
"qml/WindowControls.qml",
24-
"qml/TitleBarDrag.qml",
25-
"qml/WheelPan.qml",
26-
"qml/MakeBlockDialog.qml",
27-
"qml/AppSelectorDialog.qml",
28-
"qml/ActivityPanel.qml",
29-
"qml/RecordingOverlay.qml",
30-
"qml/TemplateList.qml",
31-
"qml/ScreenOverlay.qml",
32-
"qml/LibraryPanel.qml",
33-
"qml/MacroFiles.qml",
34-
"qml/PromptWindow.qml",
35-
"qml/BlockLibrary.qml",
36-
"qml/AppDialog.qml",
37-
"qml/GlobalPicker.qml",
38-
"qml/NamePicker.qml",
39-
"qml/SchedulePicker.qml",
40-
];
20+
// qml/module.json lists the module's files; cmake/BlockworkEditorQml.cmake
21+
// packages its editor half for the website from the same list.
22+
let qml = Manifest::read("qml/module.json");
4123

4224
// Shared Lucide icons (lean set): staged under qml/Lucide/ (gitignored,
4325
// rewritten only when changed) and compiled into this module, so every
4426
// page resolves LucideIcon/Icons locally with no import.
4527
let (lucide_icons, lucide_item) = lucide_qt::stage_qml_here("qml/Lucide");
28+
assert_eq!(
29+
list(&qml.json["lucide"]["files"], "lucide.files"),
30+
[&lucide_icons, &lucide_item].map(|p| p.file_name().unwrap().to_string_lossy().into_owned()),
31+
"qml/module.json: `lucide.files` differs from what lucide-qt stages"
32+
);
4633

47-
let builder = CxxQtBuilder::new_qml_module(
48-
QmlModule::new("com.blockworked.Blockwork")
49-
.version(1, 0)
50-
.qml_file(QmlFile::from(lucide_icons).singleton(true))
51-
.qml_file(QmlFile::from("qml/Schedules.qml").singleton(true))
52-
.qml_files(blockwork)
53-
.qml_files([lucide_item]),
54-
)
55-
.qrc_resources(QResources::new().resource(
56-
QResource::new()
57-
.prefix("/qt/qml/BlockworkStyle")
58-
.file(QResourceFile::new("qml/BlockworkStyle/qmldir").alias("qmldir"))
59-
.file(QResourceFile::new("qml/BlockworkStyle/TextField.qml").alias("TextField.qml"))
60-
.file(QResourceFile::new("qml/BlockworkStyle/TextArea.qml").alias("TextArea.qml"))
61-
.file(QResourceFile::new("qml/BlockworkStyle/TextEditingMenu.qml").alias("TextEditingMenu.qml")),
62-
))
34+
let singletons = qml.strings("singletons");
35+
let module = qml
36+
.desktop_files()
37+
.iter()
38+
.fold(
39+
QmlModule::new(qml.string("uri").as_str())
40+
.version(qml.version().0, qml.version().1)
41+
.qml_file(QmlFile::from(lucide_icons).singleton(true)),
42+
|module, name| module.qml_file(QmlFile::from(format!("qml/{name}")).singleton(singletons.contains(name))),
43+
)
44+
.qml_files([lucide_item]);
45+
46+
let mut style = QResources::new();
47+
for (name, files) in qml.json["style_modules"].as_object().expect("`style_modules` must be an object") {
48+
let resource = list(files, name).iter().fold(
49+
QResource::new().prefix(format!("/qt/qml/{name}")),
50+
|resource, file| resource.file(QResourceFile::new(format!("qml/{name}/{file}")).alias(file.as_str())),
51+
);
52+
style = style.resource(resource);
53+
}
54+
// Runtime window icon (see src/app_icon.cpp, addressed as ":/icons/..."
55+
// in C++) beside the editor's own resources; kept under /icons so its
56+
// resource path stays stable.
57+
let mut resources = QResources::new();
58+
for (prefix, files) in qml.json["editor_resources"].as_object().expect("`editor_resources` must be an object") {
59+
let mut resource = QResource::new().prefix(prefix.as_str());
60+
for file in list(files, prefix) {
61+
let path = format!("qml/{file}");
62+
let alias = Path::new(&path).file_name().unwrap().to_string_lossy().into_owned();
63+
resource = resource.file(QResourceFile::new(path).alias(alias));
64+
}
65+
if prefix == "/icons" {
66+
resource = resource.file(QResourceFile::new("../res/icons/window-shadow.png").alias("window-shadow.png"));
67+
}
68+
resources = resources.resource(resource);
69+
}
70+
71+
let builder = qml
72+
.strings("qt_modules")
73+
.iter()
74+
.fold(CxxQtBuilder::new_qml_module(module), |builder, qt| builder.qt_module(qt))
75+
.qrc_resources(style)
76+
.qrc_resources(resources)
6377
.file("src/app_bridge.rs")
6478
.file("src/prompt_bridge.rs")
6579
.file("src/translator.rs")
@@ -68,18 +82,8 @@ fn main() {
6882
.file("src/application.rs")
6983
.file("src/qt_diagnostics.rs")
7084
.file("src/window_frame.rs")
71-
// Runtime window icon (see src/app_icon.cpp, addressed as ":/icons/..."
72-
// in C++); kept under /icons so its resource path stays stable.
73-
.qrc_resources(QResources::new().resource(
74-
QResource::new()
75-
.prefix("/icons")
76-
.file(QResourceFile::new("../res/icons/blockwork.png").alias("blockwork.png"))
77-
.file(QResourceFile::new("../res/icons/window-shadow.png").alias("window-shadow.png")),
78-
))
7985
// UI translations, built from i18n/*.ts by `just translations`.
8086
.qrc_resources(translation_resources())
81-
.qt_module("QuickControls2")
82-
.qt_module("QuickDialogs2")
8387
.qt_module("Network")
8488
.qt_module("Widgets");
8589

@@ -145,3 +149,135 @@ fn qt_private_includes() -> Vec<std::path::PathBuf> {
145149
.filter(|dir| dir.exists())
146150
.collect()
147151
}
152+
153+
/// `qml/module.json`: the module's files in three groups. `editor` is
154+
/// EditorPage and everything it reaches, `host_provided` the window chrome an
155+
/// embedding host replaces, `desktop` the rest of the app.
156+
struct Manifest {
157+
json: Value,
158+
}
159+
160+
fn list(value: &Value, what: &str) -> Vec<String> {
161+
value
162+
.as_array()
163+
.unwrap_or_else(|| panic!("qml/module.json: `{what}` must be a list"))
164+
.iter()
165+
.map(|v| v.as_str().unwrap_or_else(|| panic!("qml/module.json: `{what}` holds strings")).to_owned())
166+
.collect()
167+
}
168+
169+
impl Manifest {
170+
fn read(path: &str) -> Self {
171+
println!("cargo:rerun-if-changed={path}");
172+
println!("cargo:rerun-if-changed=qml");
173+
let text = std::fs::read_to_string(path).unwrap_or_else(|e| panic!("cannot read {path}: {e}"));
174+
let manifest = Self { json: serde_json::from_str(&text).unwrap_or_else(|e| panic!("{path}: {e}")) };
175+
manifest.check();
176+
manifest
177+
}
178+
179+
fn string(&self, key: &str) -> String {
180+
self.json[key].as_str().unwrap_or_else(|| panic!("qml/module.json: `{key}` missing")).to_owned()
181+
}
182+
183+
fn strings(&self, key: &str) -> Vec<String> {
184+
list(&self.json[key], key)
185+
}
186+
187+
fn version(&self) -> (usize, usize) {
188+
self.string("version")
189+
.split_once('.')
190+
.and_then(|(a, b)| Some((a.parse().ok()?, b.parse().ok()?)))
191+
.expect("qml/module.json: `version` must be MAJOR.MINOR")
192+
}
193+
194+
fn desktop_files(&self) -> Vec<String> {
195+
["editor", "host_provided", "desktop"].iter().flat_map(|group| self.strings(group)).collect()
196+
}
197+
198+
/// Fails the build when the manifest and qml/ disagree, so the website's
199+
/// CMake build can't silently miss a file the editor needs.
200+
fn check(&self) {
201+
let mut problems = Vec::new();
202+
let files = self.desktop_files();
203+
let listed: BTreeSet<&str> = files.iter().map(String::as_str).collect();
204+
if listed.len() != files.len() {
205+
problems.push("a file is listed twice".to_owned());
206+
}
207+
let on_disk = qml_names("qml");
208+
for name in &on_disk {
209+
if !listed.contains(name.as_str()) {
210+
problems.push(format!("{name} is in qml/ but in no group"));
211+
}
212+
}
213+
for name in &listed {
214+
if !on_disk.contains(*name) {
215+
problems.push(format!("{name} is listed but not in qml/"));
216+
}
217+
}
218+
for name in self.strings("singletons") {
219+
if !listed.contains(name.as_str()) {
220+
problems.push(format!("singleton {name} is in no group"));
221+
}
222+
}
223+
let styles = self.json["style_modules"].as_object().expect("`style_modules` must be an object");
224+
for (name, style_files) in styles {
225+
let mut want: Vec<String> = list(style_files, name);
226+
let mut have: Vec<String> = std::fs::read_dir(format!("qml/{name}"))
227+
.into_iter()
228+
.flatten()
229+
.flatten()
230+
.filter_map(|e| e.file_name().into_string().ok())
231+
.collect();
232+
want.sort();
233+
have.sort();
234+
if want != have {
235+
problems.push(format!("style module {name} lists {want:?} but holds {have:?}"));
236+
}
237+
}
238+
239+
// Every local type or module an editor file uses must be packaged with the editor.
240+
let editor: BTreeSet<String> = self.strings("editor").into_iter().collect();
241+
let reachable: BTreeSet<String> = editor.iter().chain(&self.strings("host_provided")).cloned().collect();
242+
for file in &editor {
243+
let text = std::fs::read_to_string(format!("qml/{file}")).unwrap_or_default();
244+
let code: String = text.lines().filter(|l| !l.trim_start().starts_with("//")).collect::<Vec<_>>().join("\n");
245+
for name in &on_disk {
246+
let ty = name.trim_end_matches(".qml");
247+
if name != file && !reachable.contains(name) && uses_type(&code, ty) {
248+
problems.push(format!("editor file {file} uses {ty}, which is not in `editor` or `host_provided`"));
249+
}
250+
}
251+
for line in code.lines() {
252+
let Some(module) = line.trim().strip_prefix("import ").and_then(|r| r.split_whitespace().next()) else {
253+
continue;
254+
};
255+
if Path::new("qml").join(module).is_dir() && module != "Lucide" && !styles.contains_key(module) {
256+
problems.push(format!("editor file {file} imports {module}, which is not in `style_modules`"));
257+
}
258+
}
259+
}
260+
if !problems.is_empty() {
261+
panic!("qml/module.json is out of step with qml/:\n {}", problems.join("\n "));
262+
}
263+
}
264+
}
265+
266+
fn qml_names(dir: &str) -> BTreeSet<String> {
267+
std::fs::read_dir(dir)
268+
.unwrap_or_else(|e| panic!("cannot read {dir}: {e}"))
269+
.flatten()
270+
.filter_map(|e| e.file_name().into_string().ok())
271+
.filter(|name| name.ends_with(".qml"))
272+
.collect()
273+
}
274+
275+
/// `Type {` or `Type.member` with no identifier character or dot before it.
276+
fn uses_type(code: &str, ty: &str) -> bool {
277+
code.match_indices(ty).any(|(at, _)| {
278+
let before = code[..at].chars().next_back();
279+
let rest = code[at + ty.len()..].trim_start_matches([' ', '\t']);
280+
!before.is_some_and(|c| c.is_alphanumeric() || c == '_' || c == '.')
281+
&& (rest.starts_with('{') || rest.starts_with('.'))
282+
})
283+
}

0 commit comments

Comments
 (0)