Files
fpv-rid-workbench/build.rs
T

122 lines
4.7 KiB
Rust

use std::{
env, fs,
path::{Path, PathBuf},
};
fn collect(root: &Path, folder: &Path, url_prefix: &str, output: &mut String) {
let mut files: Vec<_> = fs::read_dir(folder)
.expect("frontend assets")
.map(|f| f.unwrap().path())
.collect();
files.sort();
for path in files {
if path.file_name().unwrap().to_string_lossy().starts_with('.') {
continue;
}
assert!(!path.is_symlink(), "frontend assets must not be symlinks");
if path.is_dir() {
collect(root, &path, url_prefix, output);
continue;
}
let relative = path
.strip_prefix(root)
.unwrap()
.to_string_lossy()
.replace('\\', "/");
let mime = match path.extension().and_then(|e| e.to_str()).unwrap_or("") {
"html" => "text/html; charset=utf-8",
"js" | "mjs" => "text/javascript; charset=utf-8",
"wasm" => "application/wasm",
"css" => "text/css; charset=utf-8",
"png" => "image/png",
"ico" => "image/x-icon",
"svg" => "image/svg+xml",
"json" => "application/json",
"wav" => "audio/wav",
"txt" => "text/plain; charset=utf-8",
_ => continue,
};
output.push_str(&format!(
"{:?} => Some((include_bytes!({:?}), {:?})),\n",
format!("{url_prefix}/{relative}"),
path.to_string_lossy(),
mime
));
}
}
#[cfg(windows)]
fn embed_windows_icon(workbench: &Path, output: &Path) {
if env::var("CARGO_CFG_TARGET_ENV").as_deref() != Ok("msvc") {
return;
}
let arch = env::var("CARGO_CFG_TARGET_ARCH").expect("Windows target architecture");
let sdk = find_msvc_tools::find_windows_sdk(&arch).expect("Windows SDK required for RID icon");
let compiler = sdk
.path()
.map(|path| path.join("rc.exe"))
.find(|path| path.is_file())
.expect("Windows SDK resource compiler (rc.exe)");
fs::copy(
workbench.join("assets/rid-app.ico"),
output.join("rid-app.ico"),
)
.expect("copy Windows application icon");
fs::write(output.join("rid-app.rc"), "1 ICON \"rid-app.ico\"\r\n")
.expect("write application icon resource script");
let status = std::process::Command::new(compiler)
.current_dir(output)
.args(["/nologo", "/fo", "rid-app.res", "rid-app.rc"])
.status()
.expect("run Windows resource compiler");
assert!(
status.success(),
"Windows application icon compilation failed"
);
println!(
"cargo:rustc-link-arg-bin=fpv-rid-registry-fullstack={}",
output.join("rid-app.res").display()
);
}
fn main() {
let project = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
let root = project.join("dist/web-ui");
let workbench = project.join("crates/web-ui/workbench");
let output_directory = PathBuf::from(env::var("OUT_DIR").unwrap());
println!("cargo:rerun-if-changed={}", root.display());
println!("cargo:rerun-if-changed={}", workbench.display());
#[cfg(windows)]
embed_windows_icon(&workbench, &output_directory);
if env::var_os("CARGO_FEATURE_PORTABLE").is_some() {
println!("cargo:rerun-if-env-changed=RID_MAP_DOWNLOADER");
let downloader = PathBuf::from(
env::var_os("RID_MAP_DOWNLOADER")
.expect("Build the map downloader for the portable target"),
);
println!("cargo:rerun-if-changed={}", downloader.display());
fs::copy(downloader, output_directory.join("rid-map-downloader.bin"))
.expect("embed the map downloader");
}
assert!(
root.join("index.html").is_file(),
"Build Rust frontend first: cargo xtask web"
);
let mut output = r#"pub fn resource(path: &str) -> Option<(&'static [u8], &'static str)> { let path = match path { "/" | "/realtime" | "/workbench/module.html" => "/index.html", "/workbench" | "/workbench/" => "/workbench/login.html", _ => path }; match path {
"#.to_owned();
collect(&root, &root, "", &mut output);
collect(&workbench, &workbench, "/workbench", &mut output);
if env::var_os("CARGO_FEATURE_PORTABLE").is_some() {
let license = project.join("scripts/map-downloader/LICENSE-upstream.txt");
println!("cargo:rerun-if-changed={}", license.display());
output.push_str(&format!(
"\"/map-downloader-LICENSE.txt\" => Some((include_bytes!({:?}), \"text/plain; charset=utf-8\")),\n",
license.to_string_lossy(),
));
}
output.push_str("_ => None } }\n");
fs::write(output_directory.join("web_assets.rs"), output).unwrap();
}