//! Two build-time jobs for the daemon. //! //! **Build stamp.** Records the commit the binary was built from, so //! `GET /api/info` can tell clients which release they are talking to. Two //! sources, in order: //! //! 1. `MAESTRO_BUILD_COMMIT` / `MAESTRO_BUILD_DATE` / `MAESTRO_BUILD_DIRTY` in //! the environment. The Nix build has no `.git` in its source tree (crane //! filters it out), so `flake.nix` passes the flake's own revision this way. //! 2. The git checkout the crate is being compiled in. //! //! Neither available leaves the values empty; clients treat that as "unknown //! release" rather than an error. //! //! **SPA bundle.** Under the `web` feature only, resolve the directory //! `static_bundle.rs` embeds via `include_dir!` and publish it as //! `MAESTRO_WEB_BUNDLE_DIR`. Two modes: //! //! * `MAESTRO_WEB_DIST` set — the bundle was built elsewhere (Nix, CI, a //! release pipeline) and that directory is embedded as-is. No node, no pnpm. //! * unset — the default developer path: run `pnpm install --frozen-lockfile //! && pnpm build` in `web/` and embed `web/dist`. pnpm and node are HARD //! requirements here; a missing toolchain fails with an actionable message //! rather than silently shipping the placeholder. use std::path::PathBuf; use std::process::Command; fn main() { stamp_build(); bundle_spa(); } // --------------------------------------------------------------------------- // Build stamp // --------------------------------------------------------------------------- fn stamp_build() { println!("cargo:rerun-if-env-changed=MAESTRO_BUILD_COMMIT"); println!("cargo:rerun-if-env-changed=MAESTRO_BUILD_DATE"); println!("cargo:rerun-if-env-changed=MAESTRO_BUILD_DIRTY"); // Rebuild when HEAD moves, so a plain `cargo build` after a commit or a // branch switch restamps instead of serving the previous hash. for f in ["HEAD", "ORIG_HEAD"] { if let Some(p) = git_dir().map(|d| d.join(f)) { if p.exists() { println!("cargo:rerun-if-changed={}", p.display()); } } } let commit = env_or_git("MAESTRO_BUILD_COMMIT", &["rev-parse", "--short=12", "HEAD"]); let date = env_or_git("MAESTRO_BUILD_DATE", &["log", "-1", "--format=%cI"]); let dirty = match std::env::var("MAESTRO_BUILD_DIRTY") { Ok(v) => v == "1" || v.eq_ignore_ascii_case("true"), Err(_) => !git(&["status", "--porcelain"]) .unwrap_or_default() .is_empty(), }; println!("cargo:rustc-env=MAESTRO_BUILD_COMMIT={commit}"); println!("cargo:rustc-env=MAESTRO_BUILD_DATE={date}"); println!("cargo:rustc-env=MAESTRO_BUILD_DIRTY={dirty}"); } /// The environment variable if set and non-empty, else what git reports, else /// an empty string. fn env_or_git(var: &str, args: &[&str]) -> String { match std::env::var(var) { Ok(v) if !v.trim().is_empty() => v.trim().to_string(), _ => git(args).unwrap_or_default(), } } /// Run `git` inside the crate's source tree. `None` when git is missing, the /// tree is not a checkout, or the command fails — every one of which is a /// legitimate way to build Maestro. fn git(args: &[&str]) -> Option { let out = Command::new("git") .args(args) .current_dir(env!("CARGO_MANIFEST_DIR")) .output() .ok()?; if !out.status.success() { return None; } Some(String::from_utf8_lossy(&out.stdout).trim().to_string()) } /// Absolute path of the `.git` directory (a worktree's `.git` is a file /// pointing elsewhere, and `--git-common-dir` resolves both). fn git_dir() -> Option { let dir = git(&["rev-parse", "--path-format=absolute", "--git-common-dir"])?; if dir.is_empty() { return None; } Some(PathBuf::from(dir)) } // --------------------------------------------------------------------------- // SPA bundle // --------------------------------------------------------------------------- fn bundle_spa() { println!("cargo:rerun-if-env-changed=MAESTRO_WEB_DIST"); // Cargo exports CARGO_FEATURE_ to build scripts for each enabled // feature. Without `web` the bundle module isn't compiled, so building it // would only cost every daemon build a pnpm run. if std::env::var_os("CARGO_FEATURE_WEB").is_none() { return; } let dist_dir = match std::env::var_os("MAESTRO_WEB_DIST") { Some(prebuilt) => PathBuf::from(prebuilt), None => build_with_pnpm(), }; // Sanity-check that the directory really holds a bundle. A green build // that embeds an empty/placeholder dir is worse than a loud failure here. let index_html = dist_dir.join("index.html"); let produced = std::fs::metadata(&index_html) .map(|m| m.is_file() && m.len() > 0) .unwrap_or(false); if !produced { panic!( "no SPA bundle at {} (missing or empty index.html) — \ check the vite `outDir`, or the MAESTRO_WEB_DIST override.", index_html.display() ); } // `include_dir!` expands `$VAR` in its path argument against the env rustc // is invoked with, which is what `cargo:rustc-env` sets. println!( "cargo:rustc-env=MAESTRO_WEB_BUNDLE_DIR={}", dist_dir.display() ); } /// The default path: build `web/` in-tree with pnpm and return `web/dist`. fn build_with_pnpm() -> PathBuf { let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")); let web_dir = manifest_dir.join("../../web"); // Rebuild the bundle only when a frontend input changes — not on every // unrelated Rust recompile. for rel in [ "src", "index.html", "package.json", "pnpm-lock.yaml", "vite.config.ts", ] { println!("cargo:rerun-if-changed={}", web_dir.join(rel).display()); } // Also watch the embedded OUTPUT directory. The input watches above decide // whether THIS script re-runs and regenerates `web/dist`; but `include_dir!` // in `static_bundle.rs` reads that directory, and cargo has no other // declared dependency on those output files. Without this watch, an // incremental build could regenerate `web/dist` yet keep the OLD bundle // embedded. Tying the crate's recompilation to the regenerated output makes // the fresh assets actually get re-embedded. let dist_dir = web_dir.join("dist"); println!("cargo:rerun-if-changed={}", dist_dir.display()); require_tool("pnpm"); require_tool("node"); run( Command::new("pnpm") .args(["install", "--frozen-lockfile"]) .current_dir(&web_dir), "pnpm install --frozen-lockfile", ); run( Command::new("pnpm").arg("build").current_dir(&web_dir), "pnpm build", ); dist_dir } fn require_tool(tool: &str) { let ok = Command::new(tool) .arg("--version") .output() .map(|o| o.status.success()) .unwrap_or(false); if !ok { panic!( "the `web` feature requires `{tool}` to build the embedded SPA bundle. \ Install pnpm + node (https://pnpm.io), point MAESTRO_WEB_DIST at a \ prebuilt bundle, or build with --no-default-features." ); } } fn run(cmd: &mut Command, label: &str) { let status = cmd .status() .unwrap_or_else(|e| panic!("failed to spawn `{label}`: {e}")); if !status.success() { panic!("`{label}` failed with {status}"); } }