| 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 |
stamp_git_hash(); |
| 9 |
build_frontend(); |
| 10 |
fingerprint_assets(); |
| 11 |
} |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
fn stamp_git_hash() { |
| 20 |
println!("cargo::rustc-env=GIT_HASH={}", git_hash()); |
| 21 |
} |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
fn fingerprint_assets() { |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
println!("cargo::rerun-if-changed=static/fonts"); |
| 53 |
let static_files = ["static/style.css", "static/htmx.min.js", "static/mt.js"]; |
| 54 |
|
| 55 |
let mut hasher = DefaultHasher::new(); |
| 56 |
for path in &static_files { |
| 57 |
println!("cargo::rerun-if-changed={path}"); |
| 58 |
if let Ok(content) = fs::read(path) { |
| 59 |
content.hash(&mut hasher); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
hash_dir_js(Path::new("static/dist"), &mut hasher); |
| 66 |
let version = &format!("{:016x}", hasher.finish())[..8]; |
| 67 |
|
| 68 |
|
| 69 |
let head = format!( |
| 70 |
r#" <link rel="stylesheet" href="/static/style.css?v={version}"> |
| 71 |
<script src="/static/htmx.min.js?v={version}"></script>"# |
| 72 |
); |
| 73 |
write_if_changed(Path::new("templates/_head_assets.html"), &head); |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
let scripts = format!(r#" <script src="/static/mt.js?v={version}"></script>"#); |
| 79 |
write_if_changed(Path::new("templates/_body_scripts.html"), &scripts); |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
let island = r#"{% macro island(name) -%} |
| 85 |
<script type="module" src="/static/dist/{{ name }}.js?v=__VER__"></script> |
| 86 |
{%- endmacro %} |
| 87 |
"# |
| 88 |
.replace("__VER__", version); |
| 89 |
write_if_changed(Path::new("templates/_island.html"), &island); |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
fn write_if_changed(path: &Path, contents: &str) { |
| 94 |
let needs_write = fs::read_to_string(path).map_or(true, |existing| existing != contents); |
| 95 |
if needs_write { |
| 96 |
fs::write(path, contents) |
| 97 |
.unwrap_or_else(|e| panic!("failed to write {}: {e}", path.display())); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
fn hash_dir_js(dir: &Path, hasher: &mut DefaultHasher) { |
| 105 |
let Ok(entries) = fs::read_dir(dir) else { |
| 106 |
return; |
| 107 |
}; |
| 108 |
let mut paths: Vec<_> = entries.flatten().map(|e| e.path()).collect(); |
| 109 |
paths.sort(); |
| 110 |
for path in paths { |
| 111 |
if path.is_dir() { |
| 112 |
hash_dir_js(&path, hasher); |
| 113 |
} else if path.extension().and_then(|e| e.to_str()) == Some("js") |
| 114 |
&& let Ok(content) = fs::read(&path) |
| 115 |
{ |
| 116 |
content.hash(hasher); |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
fn build_frontend() { |
| 140 |
println!("cargo::rerun-if-changed=frontend/src"); |
| 141 |
println!("cargo::rerun-if-changed=frontend/package.json"); |
| 142 |
println!("cargo::rerun-if-changed=frontend/package-lock.json"); |
| 143 |
println!("cargo::rerun-if-changed=frontend/tsconfig.json"); |
| 144 |
|
| 145 |
if std::env::var_os("MT_SKIP_FRONTEND_BUILD").is_some() { |
| 146 |
println!("cargo::warning=frontend build skipped (MT_SKIP_FRONTEND_BUILD set)"); |
| 147 |
return; |
| 148 |
} |
| 149 |
|
| 150 |
if !Path::new("frontend/node_modules").is_dir() { |
| 151 |
match Command::new("npm") |
| 152 |
.args(["ci"]) |
| 153 |
.current_dir("frontend") |
| 154 |
.status() |
| 155 |
{ |
| 156 |
Ok(s) if s.success() => {} |
| 157 |
Ok(s) => { |
| 158 |
println!( |
| 159 |
"cargo::warning=npm ci failed (exit {:?}); skipping frontend build (serving existing static/dist)", |
| 160 |
s.code() |
| 161 |
); |
| 162 |
return; |
| 163 |
} |
| 164 |
Err(e) => { |
| 165 |
println!( |
| 166 |
"cargo::warning=could not run npm ({e}); is Node installed? skipping frontend build (serving existing static/dist)" |
| 167 |
); |
| 168 |
return; |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
match Command::new("npm") |
| 174 |
.args(["run", "build"]) |
| 175 |
.current_dir("frontend") |
| 176 |
.status() |
| 177 |
{ |
| 178 |
Ok(s) if s.success() => {} |
| 179 |
Ok(s) => println!( |
| 180 |
"cargo::warning=frontend build failed (npm run build exit {:?}); serving stale static/dist", |
| 181 |
s.code() |
| 182 |
), |
| 183 |
Err(e) => println!( |
| 184 |
"cargo::warning=could not run npm for the frontend build ({e}); is Node installed? \ |
| 185 |
skipping (serving existing static/dist)" |
| 186 |
), |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
fn git_hash() -> String { |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
println!("cargo::rerun-if-env-changed=MNW_GIT_HASH"); |
| 219 |
if let Ok(h) = std::env::var("MNW_GIT_HASH") { |
| 220 |
let h = h.trim().to_string(); |
| 221 |
if !h.is_empty() { |
| 222 |
return h; |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
watch_if_exists(git_path("HEAD").as_deref()); |
| 231 |
if let Some(r) = git_output(&["symbolic-ref", "-q", "HEAD"]) { |
| 232 |
watch_if_exists(git_path(&r).as_deref()); |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
watch_if_exists(git_path("packed-refs").as_deref()); |
| 237 |
|
| 238 |
git_output(&["rev-parse", "--short", "HEAD"]).unwrap_or_default() |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
fn git_output(args: &[&str]) -> Option<String> { |
| 243 |
Command::new("git") |
| 244 |
.args(args) |
| 245 |
.output() |
| 246 |
.ok() |
| 247 |
.filter(|o| o.status.success()) |
| 248 |
.and_then(|o| String::from_utf8(o.stdout).ok()) |
| 249 |
.map(|s| s.trim().to_string()) |
| 250 |
.filter(|s| !s.is_empty()) |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
fn git_path(name: &str) -> Option<String> { |
| 257 |
git_output(&["rev-parse", "--git-path", name]) |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
fn watch_if_exists(path: Option<&str>) { |
| 265 |
if let Some(p) = path |
| 266 |
&& Path::new(p).exists() |
| 267 |
{ |
| 268 |
println!("cargo::rerun-if-changed={p}"); |
| 269 |
} |
| 270 |
} |
| 271 |
|