makeover_build/lib.rs
1//! Build-script support for the make-family design system.
2//!
3//! <!-- wiki: makeover-geometry -->
4//!
5//! Every consumer materialises the same generated files from a `build.rs`, and
6//! until now every consumer wrote that code itself. GoingsOn and Balanced
7//! Breakfast grew byte-identical copies of the theme materialiser during the
8//! makeover-geometry adoption, and the layout stylesheet would have been the
9//! third and fourth copies. This is that code, once.
10//!
11//! # The geometry emitter, and why it took a decision to land
12//!
13//! [`geometry_css`] was deliberately absent at first. GoingsOn and Balanced
14//! Breakfast did not agree on it: GO scoped the touch preset to a
15//! `ui-mode-mobile` class set by a bootstrap script, BB hung it off
16//! `@media (hover: none)`, and audiofiles had no switch at all. Extracting it
17//! then would have meant picking one of those policies by accident, inside a
18//! shared crate, without anyone deciding.
19//!
20//! Density selection was settled instead -- touch is a capability, so it hangs
21//! off `(hover: none), (pointer: coarse)` and never off a user-agent string or
22//! a breakpoint -- and the emitter followed. Recording an agreement rather than
23//! manufacturing one is the whole point, and it is why the order was that way
24//! round.
25//!
26//! # Why these files are generated rather than checked in
27//!
28//! Tauri's resource globs are read by its CLI against the crate directory, so
29//! they cannot point into a registry checkout or `OUT_DIR`. Materialising into
30//! the crate keeps the source crate authoritative without vendoring a second
31//! copy that drifts. Every path written here is expected to be gitignored.
32
33#![forbid(unsafe_code)]
34
35use std::path::Path;
36
37/// Re-exported so a consumer's `build.rs` needs one dependency rather than
38/// three. Nothing here wraps it; the emitter's options are the emitter's.
39pub use makeover_webview::Emit;
40
41/// Write the themes `makeover` ships into `dir`, as `<id>.toml`.
42///
43/// Clears stale `.toml` files first, so a theme removed or renamed upstream
44/// does not linger in the bundle from an earlier build. That detail is the
45/// reason this is worth sharing rather than retyping: it is easy to omit and
46/// its absence shows up as a theme that will not go away.
47///
48/// # Panics
49///
50/// If the directory cannot be created, read, or written. A build script has
51/// nowhere useful to return an error to, and a half-materialised theme set is
52/// worse than a failed build.
53pub fn themes(dir: impl AsRef<Path>) {
54 let dir = dir.as_ref();
55 std::fs::create_dir_all(dir).expect("create themes dir");
56
57 for entry in std::fs::read_dir(dir).expect("read themes dir").flatten() {
58 let path = entry.path();
59 if path.extension().is_some_and(|e| e == "toml") {
60 std::fs::remove_file(&path).expect("remove stale theme");
61 }
62 }
63
64 for (id, source) in makeover::embedded_themes() {
65 std::fs::write(dir.join(format!("{id}.toml")), source).expect("write theme");
66 }
67}
68
69/// Write `makeover-webview`'s component stylesheet to `path`.
70///
71/// Baked at build time rather than applied from JS the way the intent layer
72/// is, because composition never changes at runtime: no theme may reach it, so
73/// there is nothing to re-apply and no second pass over `:root` to pay for on
74/// load.
75///
76/// # Panics
77///
78/// If the file cannot be written.
79pub fn layout_css(path: impl AsRef<Path>, opts: &makeover_webview::Emit) {
80 std::fs::write(path, makeover_webview::stylesheet(opts)).expect("write layout css");
81}
82
83/// Write `makeover-geometry`'s spacing layer, with its canonical density
84/// selection, to `path`.
85///
86/// The policy is the crate's, not this one's: touch hangs off
87/// `(hover: none), (pointer: coarse)` because density is a capability rather
88/// than a device or a width, and `explicit_touch` names a selector an app sets
89/// when the user has chosen. See [`makeover_geometry::density_css`]. All this
90/// adds is the generated-file banner and the write.
91///
92/// Both spacing axes land here, in the order the crate defines them.
93/// [`makeover_geometry::size_class_css`] follows the density block because it
94/// is the narrower claim: density says what is pointing at the screen, size
95/// class says how much screen there is, and on a compact window the two shells
96/// tighten regardless of which density selected them. Shipped in
97/// makeover-geometry 0.7.0 and emitted by nobody until 2026-08-10, which meant
98/// the axis existed in the crate and reached no stylesheet.
99///
100/// # Panics
101///
102/// If the file cannot be written.
103pub fn geometry_css(path: impl AsRef<Path>, explicit_touch: Option<&str>) {
104 let mut css = String::from(
105 "/* Generated by makeover-build from makeover-geometry. Do not edit.\n \
106 Spacing is named by relationship, not by size. Touch density is a\n \
107 capability question: a narrow desktop window still has a pointer, a\n \
108 full-width tablet still has a finger. Window width is the separate\n \
109 question below it: on a compact window the two shells tighten. */\n",
110 );
111 css.push_str(&makeover_geometry::density_css(explicit_touch));
112 css.push('\n');
113 css.push_str(&makeover_geometry::size_class_css());
114 std::fs::write(path, css).expect("write geometry css");
115}
116
117/// All three generated files at the layout every Tauri consumer already uses:
118/// `themes/` beside the manifest, and `frontend/css/{geometry,layout}.css`
119/// under it.
120///
121/// Pass `env!("CARGO_MANIFEST_DIR")`. Consumers that want different paths call
122/// [`themes`] and [`layout_css`] directly.
123///
124/// # Panics
125///
126/// If either file cannot be written.
127pub fn tauri_frontend(
128 manifest_dir: impl AsRef<Path>,
129 opts: &makeover_webview::Emit,
130 explicit_touch: Option<&str>,
131) {
132 let root = manifest_dir.as_ref();
133 let css = root.join("frontend").join("css");
134 themes(root.join("themes"));
135 geometry_css(css.join("geometry.css"), explicit_touch);
136 layout_css(css.join("layout.css"), opts);
137}
138
139#[cfg(test)]
140mod tests {
141 use super::*;
142
143 /// A scratch directory keyed by process id, so a parallel test run does
144 /// not collide. No timestamp: the pid is enough and is deterministic
145 /// within a run.
146 fn scratch(name: &str) -> std::path::PathBuf {
147 let dir =
148 std::env::temp_dir().join(format!("makeover-build-{}-{name}", std::process::id()));
149 let _ = std::fs::remove_dir_all(&dir);
150 std::fs::create_dir_all(&dir).expect("create scratch");
151 dir
152 }
153
154 #[test]
155 fn themes_are_written_one_file_per_id() {
156 let dir = scratch("themes");
157 themes(&dir);
158 let count = std::fs::read_dir(&dir).unwrap().count();
159 assert_eq!(count, makeover::embedded_themes().count());
160 assert!(count > 0, "makeover ships no themes?");
161 }
162
163 #[test]
164 fn a_theme_removed_upstream_does_not_linger() {
165 // The detail that makes this worth sharing rather than retyping.
166 let dir = scratch("stale");
167 std::fs::write(dir.join("gone-upstream.toml"), "# stale").unwrap();
168 themes(&dir);
169 assert!(!dir.join("gone-upstream.toml").exists());
170 }
171
172 #[test]
173 fn a_non_theme_file_is_left_alone() {
174 // Only .toml is cleared, so a README or a .gitignore in the bundle
175 // directory survives a rebuild.
176 let dir = scratch("keep");
177 std::fs::write(dir.join("README.md"), "not a theme").unwrap();
178 themes(&dir);
179 assert!(dir.join("README.md").exists());
180 }
181
182 #[test]
183 fn the_stylesheet_lands_and_names_no_colour() {
184 let dir = scratch("css");
185 let path = dir.join("layout.css");
186 layout_css(&path, &makeover_webview::Emit::default());
187 let css = std::fs::read_to_string(&path).unwrap();
188 assert!(css.contains("--bevel-raised"));
189 assert!(
190 !css.contains('#'),
191 "a colour literal reached a build output"
192 );
193 }
194
195 #[test]
196 fn the_geometry_file_carries_the_crates_policy_and_a_banner() {
197 // The policy itself is tested in makeover-geometry. What is this
198 // crate's job is that the banner is there and the policy reached the
199 // file at all.
200 let dir = scratch("geometry");
201 let path = dir.join("geometry.css");
202 geometry_css(&path, Some(".ui-mode-mobile"));
203 let css = std::fs::read_to_string(&path).unwrap();
204 assert!(css.starts_with("/* Generated by makeover-build"));
205 assert!(css.contains("@media (hover: none), (pointer: coarse)"));
206 assert!(css.contains(".ui-mode-mobile"));
207 // The width axis rides along, and only the shells are in it: a gap
208 // between two controls in a width query is the bug size_class_css
209 // exists to keep out.
210 assert!(css.contains("--gap-pane"), "no compact shell override");
211 let compact = css
212 .split("@media (max-width")
213 .nth(1)
214 .expect("compact block");
215 assert!(
216 !compact.contains("--gap-peer"),
217 "a control gap crept into a width query"
218 );
219 }
220
221 #[test]
222 fn the_tauri_layout_puts_all_three_where_the_apps_look() {
223 let root = scratch("tauri");
224 std::fs::create_dir_all(root.join("frontend").join("css")).unwrap();
225 tauri_frontend(&root, &makeover_webview::Emit::default(), None);
226 assert!(
227 root.join("frontend")
228 .join("css")
229 .join("geometry.css")
230 .exists()
231 );
232 assert!(
233 root.join("frontend")
234 .join("css")
235 .join("layout.css")
236 .exists()
237 );
238 assert!(root.join("themes").is_dir());
239 }
240}