From 246cbf82cfc59976d0753cb1fb0ec41ff9e08712 Mon Sep 17 00:00:00 2001 From: Joscha Date: Mon, 7 Aug 2023 02:21:32 +0200 Subject: [PATCH] Fix build script not using $OUT_PATH --- Cargo.lock | 32 +++++++++++++++++++++- Cargo.toml | 2 +- build.rs | 44 ++++++++++++++----------------- {static => scripts}/queue/main.ts | 0 src/web/static.rs | 2 +- tsconfig.json | 4 +-- 6 files changed, 55 insertions(+), 29 deletions(-) rename {static => scripts}/queue/main.ts (100%) diff --git a/Cargo.lock b/Cargo.lock index 2d75a05..b6bfd76 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -547,7 +547,27 @@ version = "5.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9a49173b84e034382284f27f1af4dcbbd231ffa358c0fe316541a7337f376a35" dependencies = [ - "dirs-sys", + "dirs-sys 0.4.1", +] + +[[package]] +name = "dirs" +version = "4.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca3aa72a6f96ea37bbc5aa912f6788242832f75369bdfdadcb0e38423f100059" +dependencies = [ + "dirs-sys 0.3.7", +] + +[[package]] +name = "dirs-sys" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b1d1d91c932ef41c0f2663aa8b0ca0342d444d842c06914aa0a7e352d0bada6" +dependencies = [ + "libc", + "redox_users", + "winapi", ] [[package]] @@ -2250,6 +2270,7 @@ dependencies = [ "proc-macro2", "quote", "rust-embed-utils", + "shellexpand", "syn 2.0.28", "walkdir", ] @@ -2409,6 +2430,15 @@ dependencies = [ "lazy_static", ] +[[package]] +name = "shellexpand" +version = "2.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ccc8076840c4da029af4f87e4e8daeb0fca6b87bbb02e10cb60b791450e11e4" +dependencies = [ + "dirs", +] + [[package]] name = "signal-hook" version = "0.3.17" diff --git a/Cargo.toml b/Cargo.toml index b7771dc..814687a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,7 +15,7 @@ humantime = "2.1.0" humantime-serde = "1.1.1" mime_guess = "2.0.4" rand = "0.8.5" -rust-embed = "6.8.1" +rust-embed = { version = "6.8.1", features = ["interpolate-folder-path"] } serde = { version = "1.0.181", features = ["derive"] } sqlx = { version = "0.7.1", features = ["runtime-tokio", "sqlite", "time"] } time = { version = "0.3.25", features = ["formatting", "macros", "parsing"] } diff --git a/build.rs b/build.rs index 2fb165d..e4346e3 100644 --- a/build.rs +++ b/build.rs @@ -6,38 +6,32 @@ use std::{ use walkdir::WalkDir; -const MIGRATION_DIR: &str = "migrations"; -const STATIC_DIR: &str = "static"; -const TEMPLATE_DIR: &str = "templates"; - -const STATIC_OUT_DIR: &str = "target/static"; -const STATIC_IGNORE_EXT: &[&str] = &["ts"]; - fn watch_dir(path: &Path) { WalkDir::new(path) .into_iter() .for_each(|e| println!("cargo:rerun-if-changed={}", e.unwrap().path().display())); } -fn run_tsc() { - let status = Command::new("tsc").status().unwrap(); +fn run_tsc(static_out_dir: &Path) { + let status = Command::new("tsc") + .arg("--outDir") + .arg(static_out_dir) + .status() + .unwrap(); + assert!(status.success(), "tsc produced errors"); } -fn copy_static_files() { - let files = WalkDir::new(STATIC_DIR) +fn copy_static_files(static_dir: &Path, static_out_dir: &Path) { + let files = WalkDir::new(static_dir) .into_iter() .map(|e| e.unwrap()) - .filter(|e| e.file_type().is_file()) - .filter(|e| { - let extension = e.path().extension().and_then(|s| s.to_str()).unwrap_or(""); - !STATIC_IGNORE_EXT.contains(&extension) - }); + .filter(|e| e.file_type().is_file()); for file in files { let components = file.path().components().collect::>(); let relative_path = components.into_iter().rev().take(file.depth()).rev(); - let mut target = PathBuf::new().join(STATIC_OUT_DIR); + let mut target = static_out_dir.to_path_buf(); target.extend(relative_path); fs::create_dir_all(target.parent().unwrap()).unwrap(); fs::copy(file.path(), target).unwrap(); @@ -49,16 +43,18 @@ fn main() { builder.git_sha(false); builder.emit().unwrap(); - watch_dir(MIGRATION_DIR.as_ref()); - watch_dir(STATIC_DIR.as_ref()); - watch_dir(TEMPLATE_DIR.as_ref()); + let out_dir: PathBuf = std::env::var("OUT_DIR").unwrap().into(); + let static_out_dir = out_dir.join("static"); // Since remove_dir_all fails if the directory doesn't exist, we ensure it // exists before deleting it. This way, we can use the remove_dir_all Result // to ensure the directory was deleted successfully. - fs::create_dir_all(STATIC_OUT_DIR).unwrap(); - fs::remove_dir_all(STATIC_OUT_DIR).unwrap(); + fs::create_dir_all(&static_out_dir).unwrap(); + fs::remove_dir_all(&static_out_dir).unwrap(); - run_tsc(); - copy_static_files(); + watch_dir("scripts".as_ref()); + run_tsc(&static_out_dir); + + watch_dir("static".as_ref()); + copy_static_files("static".as_ref(), &static_out_dir); } diff --git a/static/queue/main.ts b/scripts/queue/main.ts similarity index 100% rename from static/queue/main.ts rename to scripts/queue/main.ts diff --git a/src/web/static.rs b/src/web/static.rs index bbf7ca0..f640b18 100644 --- a/src/web/static.rs +++ b/src/web/static.rs @@ -7,7 +7,7 @@ use axum::{ use rust_embed::RustEmbed; #[derive(RustEmbed)] -#[folder = "target/static"] +#[folder = "$OUT_DIR/static"] pub struct StaticFiles; pub struct StaticFile(T); diff --git a/tsconfig.json b/tsconfig.json index ab457ee..edd10f2 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,10 +1,10 @@ { // See also https://aka.ms/tsconfig - "include": [ "static/**/*" ], + "include": [ "scripts/**/*" ], "compilerOptions": { "target": "ES2015", "module": "ES2015", - "rootDir": "static", + "rootDir": "scripts", "outDir": "target/static", // Misc