crabidy/crabidy-server/build.rs

60 lines
2.4 KiB
Rust

//! Stages the web client bundle for embedding (feature `web-ui`,
//! architecture/web-client.md): copies `cbd-web/dist` (the trunk
//! output) into `OUT_DIR/webdist`, or generates a placeholder page
//! when the bundle has not been built — a plain `cargo build` must
//! neither fail nor require the wasm toolchain. Deliberately no
//! cargo-in-cargo: this never invokes trunk itself.
use std::path::Path;
fn main() {
// Rerun when the bundle changes (or appears).
println!("cargo:rerun-if-changed=../cbd-web/dist");
if std::env::var_os("CARGO_FEATURE_WEB_UI").is_none() {
return;
}
let out_dir = std::env::var("OUT_DIR").expect("OUT_DIR is set for build scripts");
let staged = Path::new(&out_dir).join("webdist");
// Start fresh so removed assets do not linger across builds.
if staged.exists() {
std::fs::remove_dir_all(&staged).expect("clean staged webdist");
}
std::fs::create_dir_all(&staged).expect("create staged webdist");
let dist = Path::new(env!("CARGO_MANIFEST_DIR")).join("../cbd-web/dist");
if dist.join("index.html").is_file() {
copy_dir(&dist, &staged);
} else {
println!(
"cargo:warning=cbd-web/dist not found - embedding a placeholder web UI \
(build the bundle with: devenv shell -- build-web)"
);
std::fs::write(
staged.join("index.html"),
"<!doctype html><meta charset=\"utf-8\"><title>crabidy</title>\
<body style=\"font:16px system-ui;padding:2rem\">\
<h1>crabidy web UI not built</h1>\
<p>This server binary was compiled without the web bundle. \
Build it with <code>devenv shell -- build-web</code> and \
rebuild the server.</p>",
)
.expect("write placeholder index.html");
}
}
/// Copies `from` into `to` recursively (regular files only — the trunk
/// output contains nothing else).
fn copy_dir(from: &Path, to: &Path) {
for entry in std::fs::read_dir(from).expect("read dist dir") {
let entry = entry.expect("dist dir entry");
let target = to.join(entry.file_name());
let path = entry.path();
if path.is_dir() {
std::fs::create_dir_all(&target).expect("create staged subdir");
copy_dir(&path, &target);
} else {
std::fs::copy(&path, &target).expect("copy dist file");
}
}
}