| 1 |
use std::collections::hash_map::DefaultHasher; |
| 2 |
use std::fs; |
| 3 |
use std::hash::{Hash, Hasher}; |
| 4 |
use std::path::Path; |
| 5 |
use std::process::Command; |
| 6 |
|
| 7 |
fn main() { |
| 8 |
build_frontend(); |
| 9 |
fingerprint_assets(); |
| 10 |
} |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
fn fingerprint_assets() { |
| 28 |
let static_files = ["static/style.css", "static/htmx.min.js", "static/mt.js"]; |
| 29 |
|
| 30 |
let mut hasher = DefaultHasher::new(); |
| 31 |
for path in &static_files { |
| 32 |
println!("cargo::rerun-if-changed={path}"); |
| 33 |
if let Ok(content) = fs::read(path) { |
| 34 |
content.hash(&mut hasher); |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
hash_dir_js(Path::new("static/dist"), &mut hasher); |
| 41 |
let version = &format!("{:016x}", hasher.finish())[..8]; |
| 42 |
|
| 43 |
|
| 44 |
let head = format!( |
| 45 |
r#" <link rel="stylesheet" href="/static/style.css?v={version}"> |
| 46 |
<script src="/static/htmx.min.js?v={version}"></script>"# |
| 47 |
); |
| 48 |
write_if_changed(Path::new("templates/_head_assets.html"), &head); |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
let scripts = format!(r#" <script src="/static/mt.js?v={version}"></script>"#); |
| 54 |
write_if_changed(Path::new("templates/_body_scripts.html"), &scripts); |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
let island = r#"{% macro island(name) -%} |
| 60 |
<script type="module" src="/static/dist/{{ name }}.js?v=__VER__"></script> |
| 61 |
{%- endmacro %} |
| 62 |
"# |
| 63 |
.replace("__VER__", version); |
| 64 |
write_if_changed(Path::new("templates/_island.html"), &island); |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
fn write_if_changed(path: &Path, contents: &str) { |
| 69 |
let needs_write = fs::read_to_string(path).map_or(true, |existing| existing != contents); |
| 70 |
if needs_write { |
| 71 |
fs::write(path, contents) |
| 72 |
.unwrap_or_else(|e| panic!("failed to write {}: {e}", path.display())); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
fn hash_dir_js(dir: &Path, hasher: &mut DefaultHasher) { |
| 80 |
let Ok(entries) = fs::read_dir(dir) else { |
| 81 |
return; |
| 82 |
}; |
| 83 |
let mut paths: Vec<_> = entries.flatten().map(|e| e.path()).collect(); |
| 84 |
paths.sort(); |
| 85 |
for path in paths { |
| 86 |
if path.is_dir() { |
| 87 |
hash_dir_js(&path, hasher); |
| 88 |
} else if path.extension().and_then(|e| e.to_str()) == Some("js") |
| 89 |
&& let Ok(content) = fs::read(&path) |
| 90 |
{ |
| 91 |
content.hash(hasher); |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
fn build_frontend() { |
| 115 |
println!("cargo::rerun-if-changed=frontend/src"); |
| 116 |
println!("cargo::rerun-if-changed=frontend/package.json"); |
| 117 |
println!("cargo::rerun-if-changed=frontend/package-lock.json"); |
| 118 |
println!("cargo::rerun-if-changed=frontend/tsconfig.json"); |
| 119 |
|
| 120 |
if std::env::var_os("MT_SKIP_FRONTEND_BUILD").is_some() { |
| 121 |
println!("cargo::warning=frontend build skipped (MT_SKIP_FRONTEND_BUILD set)"); |
| 122 |
return; |
| 123 |
} |
| 124 |
|
| 125 |
if !Path::new("frontend/node_modules").is_dir() { |
| 126 |
match Command::new("npm") |
| 127 |
.args(["ci"]) |
| 128 |
.current_dir("frontend") |
| 129 |
.status() |
| 130 |
{ |
| 131 |
Ok(s) if s.success() => {} |
| 132 |
Ok(s) => { |
| 133 |
println!( |
| 134 |
"cargo::warning=npm ci failed (exit {:?}); skipping frontend build (serving existing static/dist)", |
| 135 |
s.code() |
| 136 |
); |
| 137 |
return; |
| 138 |
} |
| 139 |
Err(e) => { |
| 140 |
println!( |
| 141 |
"cargo::warning=could not run npm ({e}); is Node installed? skipping frontend build (serving existing static/dist)" |
| 142 |
); |
| 143 |
return; |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
match Command::new("npm") |
| 149 |
.args(["run", "build"]) |
| 150 |
.current_dir("frontend") |
| 151 |
.status() |
| 152 |
{ |
| 153 |
Ok(s) if s.success() => {} |
| 154 |
Ok(s) => println!( |
| 155 |
"cargo::warning=frontend build failed (npm run build exit {:?}); serving stale static/dist", |
| 156 |
s.code() |
| 157 |
), |
| 158 |
Err(e) => println!( |
| 159 |
"cargo::warning=could not run npm for the frontend build ({e}); is Node installed? \ |
| 160 |
skipping (serving existing static/dist)" |
| 161 |
), |
| 162 |
} |
| 163 |
} |
| 164 |
|