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