forge
Explore
Log in

Don't you dare go Hollow

forge

Clone
★ 0

0 watching0 forksRustAGPL-3.07.1 MiB490f099Tahinli7 hours ago322 commits

build.rs226 lines, 8.3 KiBHistoryRawDownload
1//! Compiles style/main.scss into OUT_DIR and emits FORGE_BUILD_SHA for the binary. Each static
2//! asset also gets a content hash (FORGE_MAIN_CSS_V, FORGE_NAV_JS_V) that the page links use as
3//! their cache-busting query, so an unchanged file keeps its URL and a changed one gets a new one.
4//!
5//! The accent colours and the neutral bases come from `forge-web-base/src/accent_palette.rs`, the
6//! same data the app reads. This script turns it into `_accents.scss` in OUT_DIR, which main.scss
7//! imports.
8
9use std::{env, fs, path::PathBuf};
10
11use hex::encode;
12use sha2::{Digest, Sha256};
13
14#[path = "../forge-web-base/src/accent_palette.rs"]
15#[allow(dead_code)]
16mod accent_palette;
17
18use accent_palette::{
19 BASE_DEFAULT, BASE_TOKEN_VARS, BASES, Base, BaseTokens, DEFAULT, PALETTES, Palette, TOKEN_VARS,
20 Tokens,
21};
22
23include!("../forge-ci/build_identity.rs");
24
25/// First 12 hex characters of the SHA-256 of `bytes`.
26fn content_version(bytes: &[u8]) -> String {
27 encode(Sha256::digest(bytes))[..12].to_string()
28}
29
30/// The `(name, value)` declarations of one accent palette's tokens in one theme.
31fn accent_decls(tokens: &Tokens) -> Vec<(&'static str, &'static str)> {
32 TOKEN_VARS
33 .iter()
34 .map(|token| (token.name, (token.value)(tokens)))
35 .collect()
36}
37
38/// The `(name, value)` declarations of one base's neutral tokens in one theme.
39fn base_decls(tokens: &BaseTokens) -> Vec<(&'static str, &'static str)> {
40 BASE_TOKEN_VARS
41 .iter()
42 .map(|token| (token.name, (token.value)(tokens)))
43 .collect()
44}
45
46/// Appends `selector { … }` with one `--token: value;` line per declaration. `indent` is 0 or 2.
47fn rule(css: &mut String, indent: &str, selector: &str, decls: &[(&str, &str)]) {
48 css.push_str(&format!("{indent}{selector} {{\n"));
49 for (name, value) in decls {
50 css.push_str(&format!("{indent} {name}: {value};\n"));
51 }
52 css.push_str(&format!("{indent}}}\n"));
53}
54
55/// The rules for one choice: its light tokens on `light`, and its dark tokens on `dark` in both
56/// dark states, by the OS and by `data-theme`. Dark selectors carry one more attribute than the
57/// light one, so the dark rules win in dark mode whatever the light choice is.
58fn choice_rules(
59 css: &mut String,
60 light: &str,
61 dark: &str,
62 light_decls: &[(&str, &str)],
63 dark_decls: &[(&str, &str)],
64) {
65 let os_dark = format!("{dark}:not([data-theme=\"light\"])");
66 let forced_dark = format!("{dark}[data-theme=\"dark\"]");
67 rule(css, "", light, light_decls);
68 css.push_str("@media (prefers-color-scheme: dark) {\n");
69 rule(css, " ", &os_dark, dark_decls);
70 css.push_str("}\n");
71 rule(css, "", &forced_dark, dark_decls);
72}
73
74/// The bare fallbacks for pages without the choice attributes: the default accent and base, on
75/// plain `:root`.
76fn fallback_rules(css: &mut String) {
77 let accent = PALETTES
78 .iter()
79 .find(|p| p.id == DEFAULT)
80 .expect("the default accent is one of the palettes");
81 choice_rules(
82 css,
83 ":root",
84 ":root",
85 &accent_decls(&accent.light),
86 &accent_decls(&accent.dark),
87 );
88 let base = BASES
89 .iter()
90 .find(|b| b.id == BASE_DEFAULT)
91 .expect("the default base is one of the bases");
92 choice_rules(
93 css,
94 ":root",
95 ":root",
96 &base_decls(&base.light),
97 &base_decls(&base.dark),
98 );
99}
100
101/// The rules for one accent palette: its light tokens are keyed on `data-accent-light`, its dark
102/// tokens on `data-accent-dark`.
103fn palette_rules(css: &mut String, palette: &Palette) {
104 choice_rules(
105 css,
106 &format!(":root[data-accent-light=\"{}\"]", palette.id),
107 &format!(":root[data-accent-dark=\"{}\"]", palette.id),
108 &accent_decls(&palette.light),
109 &accent_decls(&palette.dark),
110 );
111}
112
113/// The rules for one base: its light tokens are keyed on `data-base-light`, its dark tokens on
114/// `data-base-dark`.
115fn base_rules(css: &mut String, base: &Base) {
116 choice_rules(
117 css,
118 &format!(":root[data-base-light=\"{}\"]", base.id),
119 &format!(":root[data-base-dark=\"{}\"]", base.id),
120 &base_decls(&base.light),
121 &base_decls(&base.dark),
122 );
123}
124
125/// A base's tint made plain enough to see on a chip: the hue of its muted text, with its
126/// saturation raised five times (at most 55%) at a middle lightness. The neutrals themselves are
127/// too close to grey to tell apart at 30px.
128fn base_tint(hex: &str) -> String {
129 let channel = |i: usize| u8::from_str_radix(&hex[i..i + 2], 16).unwrap_or(0) as f64 / 255.0;
130 let (r, g, b) = (channel(1), channel(3), channel(5));
131 let (max, min) = (r.max(g).max(b), r.min(g).min(b));
132 let l = (max + min) / 2.0;
133 let d = max - min;
134 let (h, s) = if d == 0.0 {
135 (0.0, 0.0)
136 } else {
137 let s = d / (1.0 - (2.0 * l - 1.0).abs());
138 let h = if max == r {
139 ((g - b) / d).rem_euclid(6.0)
140 } else if max == g {
141 (b - r) / d + 2.0
142 } else {
143 (r - g) / d + 4.0
144 };
145 (h * 60.0, s)
146 };
147 format!("hsl({:.0} {:.0}% 52%)", h, (s * 5.0).min(0.55) * 100.0)
148}
149
150/// The swatch chips for the settings picker. They show fixed colours, so they do not depend on
151/// the current theme: an accent chip is the accent itself, a base chip is its background and its
152/// tint split diagonally.
153fn swatch_rules(css: &mut String) {
154 for palette in PALETTES {
155 css.push_str(&format!(
156 ".accent-swatch[data-accent-light-swatch=\"{}\"] {{ background: {}; }}\n",
157 palette.id, palette.light.accent
158 ));
159 css.push_str(&format!(
160 ".accent-swatch[data-accent-dark-swatch=\"{}\"] {{ background: {}; }}\n",
161 palette.id, palette.dark.accent
162 ));
163 }
164 for base in BASES {
165 css.push_str(&format!(
166 ".base-swatch[data-base-light-swatch=\"{}\"] {{ background: linear-gradient(135deg, {} 0 50%, {} 50% 100%); }}\n",
167 base.id, base.light.bg, base_tint(base.light.muted)
168 ));
169 css.push_str(&format!(
170 ".base-swatch[data-base-dark-swatch=\"{}\"] {{ background: linear-gradient(135deg, {} 0 50%, {} 50% 100%); }}\n",
171 base.id, base.dark.bg, base_tint(base.dark.muted)
172 ));
173 }
174}
175
176/// The whole generated stylesheet: the bare fallbacks, the accent and base rules, then the swatch
177/// chips.
178fn accents_scss() -> String {
179 let mut css = String::from(
180 "/* Generated by crates/forge-web/build.rs from forge-web-base/src/accent_palette.rs. Edit that file. */\n",
181 );
182 assert_eq!(PALETTES[0].id, DEFAULT, "the default palette comes first");
183 assert_eq!(BASES[0].id, BASE_DEFAULT, "the default base comes first");
184 fallback_rules(&mut css);
185 for palette in PALETTES {
186 palette_rules(&mut css, palette);
187 }
188 for base in BASES {
189 base_rules(&mut css, base);
190 }
191 swatch_rules(&mut css);
192 css
193}
194
195fn main() {
196 println!("cargo:rerun-if-env-changed=FORGE_BUILD_SHA");
197 println!("cargo:rerun-if-changed=build.rs");
198 println!("cargo:rerun-if-changed=../forge-ci/build_identity.rs");
199 emit_identity();
200 println!("cargo:rerun-if-changed=../../style");
201 println!("cargo:rerun-if-changed=../forge-web/assets/nav.js");
202 println!("cargo:rerun-if-changed=../forge-web-base/src/accent_palette.rs");
203
204 let sha = env::var("FORGE_BUILD_SHA").unwrap_or_else(|_| "dev".to_string());
205 println!("cargo:rustc-env=FORGE_BUILD_SHA={sha}");
206
207 let manifest =
208 PathBuf::from(env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR is set by cargo"));
209 let out = PathBuf::from(env::var("OUT_DIR").expect("OUT_DIR is set by cargo"));
210 fs::write(out.join("_accents.scss"), accents_scss())
211 .expect("writing generated accent stylesheet to OUT_DIR");
212 let css = grass::from_path(
213 manifest.join("../../style/main.scss"),
214 &grass::Options::default().load_path(&out),
215 )
216 .unwrap_or_else(|e| panic!("compiling style/main.scss: {e}"));
217 println!(
218 "cargo:rustc-env=FORGE_MAIN_CSS_V={}",
219 content_version(css.as_bytes())
220 );
221 fs::write(out.join("main.css"), css).expect("writing compiled stylesheet to OUT_DIR");
222
223 let nav = fs::read(manifest.join("../forge-web/assets/nav.js"))
224 .expect("reading forge-web/assets/nav.js");
225 println!("cargo:rustc-env=FORGE_NAV_JS_V={}", content_version(&nav));
226}