first commit
This commit is contained in:
commit
c30064b531
93 files changed
+26367
No files matched your search
@@ -0,0 +1,176 @@
|
||||
//! Panel asset build (like RustProxy): all CSS from `web/css` and JS from `web/js`
|
||||
//! (preceded by hls.js and the `langs/*.json` language packs) are concatenated into single,
|
||||
//! uniquely named `app.<hash>.css` / `app.<hash>.js` files embedded in the binary and served
|
||||
//! with a long-lived cache.
|
||||
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
fn sorted_files(dir: &Path, ext: &str) -> Vec<PathBuf> {
|
||||
let mut v: Vec<PathBuf> = fs::read_dir(dir)
|
||||
.unwrap_or_else(|e| panic!("{}: {e}", dir.display()))
|
||||
.filter_map(|e| e.ok().map(|e| e.path()))
|
||||
.filter(|p| p.extension().and_then(|x| x.to_str()) == Some(ext))
|
||||
.collect();
|
||||
v.sort();
|
||||
v
|
||||
}
|
||||
|
||||
/// Light CSS minification: strips comments and redundant whitespace (UTF-8 safe).
|
||||
fn minify_css(src: &str) -> String {
|
||||
let chars: Vec<char> = src.chars().collect();
|
||||
let mut out = String::with_capacity(src.len());
|
||||
let mut i = 0;
|
||||
let mut pending_space = false;
|
||||
let mut in_str: Option<char> = None;
|
||||
while i < chars.len() {
|
||||
let c = chars[i];
|
||||
if let Some(q) = in_str {
|
||||
out.push(c);
|
||||
if c == q {
|
||||
in_str = None;
|
||||
}
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
if c == '/' && chars.get(i + 1) == Some(&'*') {
|
||||
i += 2;
|
||||
while i + 1 < chars.len() && !(chars[i] == '*' && chars[i + 1] == '/') {
|
||||
i += 1;
|
||||
}
|
||||
i += 2;
|
||||
continue;
|
||||
}
|
||||
if c.is_whitespace() {
|
||||
pending_space = true;
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
let tight = |x: char| matches!(x, '{' | '}' | ';' | ',' | '>');
|
||||
if pending_space && !out.is_empty() && !tight(c) && !out.ends_with(tight) {
|
||||
out.push(' ');
|
||||
}
|
||||
pending_space = false;
|
||||
if c == '"' || c == '\'' {
|
||||
in_str = Some(c);
|
||||
}
|
||||
out.push(c);
|
||||
i += 1;
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Loads and validates `langs/*.json` and returns them as a minified JSON array.
|
||||
///
|
||||
/// Each pack: `{"meta": {"locale", "name", "native_name", "direction"},
|
||||
/// "translations": {english: translated}, "patterns": [{"match", "replace"}]}`.
|
||||
/// The file name must equal `meta.locale` (e.g. `pl_PL.json`) and `en_US.json` is required,
|
||||
/// because English is both the source text and the fallback.
|
||||
fn language_packs(dir: &Path) -> String {
|
||||
println!("cargo:rerun-if-changed=langs");
|
||||
let mut packs = Vec::new();
|
||||
let mut have_en = false;
|
||||
for f in sorted_files(dir, "json") {
|
||||
let name = f.file_stem().unwrap().to_string_lossy().to_string();
|
||||
let src = fs::read_to_string(&f).unwrap();
|
||||
let v: serde_json::Value = serde_json::from_str(&src)
|
||||
.unwrap_or_else(|e| panic!("{}: invalid JSON: {e}", f.display()));
|
||||
let locale = v["meta"]["locale"]
|
||||
.as_str()
|
||||
.unwrap_or_else(|| panic!("{}: meta.locale is missing", f.display()));
|
||||
assert_eq!(
|
||||
locale,
|
||||
name,
|
||||
"{}: meta.locale must match the file name",
|
||||
f.display()
|
||||
);
|
||||
assert!(
|
||||
v["meta"]["name"].is_string(),
|
||||
"{}: meta.name is missing",
|
||||
f.display()
|
||||
);
|
||||
let tr = v["translations"]
|
||||
.as_object()
|
||||
.unwrap_or_else(|| panic!("{}: translations must be an object", f.display()));
|
||||
for (k, val) in tr {
|
||||
assert!(
|
||||
val.is_string(),
|
||||
"{}: translation of {k:?} must be a string",
|
||||
f.display()
|
||||
);
|
||||
}
|
||||
if let Some(p) = v.get("patterns") {
|
||||
for x in p
|
||||
.as_array()
|
||||
.unwrap_or_else(|| panic!("{}: patterns must be an array", f.display()))
|
||||
{
|
||||
assert!(
|
||||
x["match"].is_string() && x["replace"].is_string(),
|
||||
"{}: every pattern needs string \"match\" and \"replace\"",
|
||||
f.display()
|
||||
);
|
||||
}
|
||||
}
|
||||
have_en |= name == "en_US";
|
||||
packs.push(v);
|
||||
}
|
||||
assert!(have_en, "langs/en_US.json is required (default language)");
|
||||
serde_json::to_string(&packs).unwrap()
|
||||
}
|
||||
|
||||
fn hash8(data: &[u8]) -> String {
|
||||
let d = Sha256::digest(data);
|
||||
d.iter()
|
||||
.take(5)
|
||||
.map(|b| format!("{b:02x}"))
|
||||
.collect::<String>()[..10]
|
||||
.to_string()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let root = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());
|
||||
let web = root.join("web");
|
||||
println!("cargo:rerun-if-changed=web");
|
||||
println!("cargo:rerun-if-changed=migrations");
|
||||
|
||||
let mut css = String::new();
|
||||
for f in sorted_files(&web.join("css"), "css") {
|
||||
let s = fs::read_to_string(&f).unwrap();
|
||||
css.push_str(&format!(
|
||||
"/* {} */\n",
|
||||
f.file_name().unwrap().to_string_lossy()
|
||||
));
|
||||
css.push_str(&s);
|
||||
css.push('\n');
|
||||
}
|
||||
let css = minify_css(&css);
|
||||
|
||||
let mut js = String::new();
|
||||
js.push_str(&fs::read_to_string(web.join("vendor/hls.min.js")).unwrap());
|
||||
js.push_str("\n;\n");
|
||||
js.push_str("window.SDVB_LANGS = ");
|
||||
js.push_str(&language_packs(&root.join("langs")));
|
||||
js.push_str(";\n");
|
||||
for f in sorted_files(&web.join("js"), "js") {
|
||||
js.push_str(&format!(
|
||||
"/* ---- {} ---- */\n",
|
||||
f.file_name().unwrap().to_string_lossy()
|
||||
));
|
||||
js.push_str(&fs::read_to_string(&f).unwrap());
|
||||
js.push_str("\n;\n");
|
||||
}
|
||||
|
||||
let out = PathBuf::from(std::env::var("OUT_DIR").unwrap());
|
||||
let css_name = format!("app.{}.css", hash8(css.as_bytes()));
|
||||
let js_name = format!("app.{}.js", hash8(js.as_bytes()));
|
||||
fs::write(out.join("app.css"), &css).unwrap();
|
||||
fs::write(out.join("app.js"), &js).unwrap();
|
||||
fs::write(
|
||||
out.join("asset_names.rs"),
|
||||
format!(
|
||||
"pub const CSS_NAME: &str = {css_name:?};\npub const JS_NAME: &str = {js_name:?};\n"
|
||||
),
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
Reference in new issue
Block a user