| 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 |
let hash = Command::new("git") |
| 21 |
.args(["rev-parse", "--short", "HEAD"]) |
| 22 |
.output() |
| 23 |
.ok() |
| 24 |
.filter(|o| o.status.success()) |
| 25 |
.and_then(|o| String::from_utf8(o.stdout).ok()) |
| 26 |
.map(|s| s.trim().to_string()) |
| 27 |
.unwrap_or_default(); |
| 28 |
|
| 29 |
println!("cargo::rustc-env=GIT_HASH={hash}"); |
| 30 |
|
| 31 |
println!("cargo::rerun-if-changed=.git/HEAD"); |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
fn fingerprint_assets() { |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
println!("cargo::rerun-if-changed=static/fonts"); |
| 64 |
let static_files = ["static/style.css", "static/htmx.min.js", "static/mt.js"]; |
| 65 |
|
| 66 |
let mut hasher = DefaultHasher::new(); |
| 67 |
for path in &static_files { |
| 68 |
println!("cargo::rerun-if-changed={path}"); |
| 69 |
if let Ok(content) = fs::read(path) { |
| 70 |
content.hash(&mut hasher); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
hash_dir_js(Path::new("static/dist"), &mut hasher); |
| 77 |
let version = &format!("{:016x}", hasher.finish())[..8]; |
| 78 |
|
| 79 |
|
| 80 |
let head = format!( |
| 81 |
r#" <link rel="stylesheet" href="/static/style.css?v={version}"> |
| 82 |
<script src="/static/htmx.min.js?v={version}"></script>"# |
| 83 |
); |
| 84 |
write_if_changed(Path::new("templates/_head_assets.html"), &head); |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
let scripts = format!(r#" <script src="/static/mt.js?v={version}"></script>"#); |
| 90 |
write_if_changed(Path::new("templates/_body_scripts.html"), &scripts); |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
let island = r#"{% macro island(name) -%} |
| 96 |
<script type="module" src="/static/dist/{{ name }}.js?v=__VER__"></script> |
| 97 |
{%- endmacro %} |
| 98 |
"# |
| 99 |
.replace("__VER__", version); |
| 100 |
write_if_changed(Path::new("templates/_island.html"), &island); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
fn write_if_changed(path: &Path, contents: &str) { |
| 105 |
let needs_write = fs::read_to_string(path).map_or(true, |existing| existing != contents); |
| 106 |
if needs_write { |
| 107 |
fs::write(path, contents) |
| 108 |
.unwrap_or_else(|e| panic!("failed to write {}: {e}", path.display())); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
fn hash_dir_js(dir: &Path, hasher: &mut DefaultHasher) { |
| 116 |
let Ok(entries) = fs::read_dir(dir) else { |
| 117 |
return; |
| 118 |
}; |
| 119 |
let mut paths: Vec<_> = entries.flatten().map(|e| e.path()).collect(); |
| 120 |
paths.sort(); |
| 121 |
for path in paths { |
| 122 |
if path.is_dir() { |
| 123 |
hash_dir_js(&path, hasher); |
| 124 |
} else if path.extension().and_then(|e| e.to_str()) == Some("js") |
| 125 |
&& let Ok(content) = fs::read(&path) |
| 126 |
{ |
| 127 |
content.hash(hasher); |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
fn build_frontend() { |
| 151 |
println!("cargo::rerun-if-changed=frontend/src"); |
| 152 |
println!("cargo::rerun-if-changed=frontend/package.json"); |
| 153 |
println!("cargo::rerun-if-changed=frontend/package-lock.json"); |
| 154 |
println!("cargo::rerun-if-changed=frontend/tsconfig.json"); |
| 155 |
|
| 156 |
if std::env::var_os("MT_SKIP_FRONTEND_BUILD").is_some() { |
| 157 |
println!("cargo::warning=frontend build skipped (MT_SKIP_FRONTEND_BUILD set)"); |
| 158 |
return; |
| 159 |
} |
| 160 |
|
| 161 |
if !Path::new("frontend/node_modules").is_dir() { |
| 162 |
match Command::new("npm") |
| 163 |
.args(["ci"]) |
| 164 |
.current_dir("frontend") |
| 165 |
.status() |
| 166 |
{ |
| 167 |
Ok(s) if s.success() => {} |
| 168 |
Ok(s) => { |
| 169 |
println!( |
| 170 |
"cargo::warning=npm ci failed (exit {:?}); skipping frontend build (serving existing static/dist)", |
| 171 |
s.code() |
| 172 |
); |
| 173 |
return; |
| 174 |
} |
| 175 |
Err(e) => { |
| 176 |
println!( |
| 177 |
"cargo::warning=could not run npm ({e}); is Node installed? skipping frontend build (serving existing static/dist)" |
| 178 |
); |
| 179 |
return; |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
match Command::new("npm") |
| 185 |
.args(["run", "build"]) |
| 186 |
.current_dir("frontend") |
| 187 |
.status() |
| 188 |
{ |
| 189 |
Ok(s) if s.success() => {} |
| 190 |
Ok(s) => println!( |
| 191 |
"cargo::warning=frontend build failed (npm run build exit {:?}); serving stale static/dist", |
| 192 |
s.code() |
| 193 |
), |
| 194 |
Err(e) => println!( |
| 195 |
"cargo::warning=could not run npm for the frontend build ({e}); is Node installed? \ |
| 196 |
skipping (serving existing static/dist)" |
| 197 |
), |
| 198 |
} |
| 199 |
} |
| 200 |
|