Fix build script not using $OUT_PATH

This commit is contained in:
Joscha 2023-08-07 02:21:32 +02:00
parent 794787a4be
commit 246cbf82cf
6 changed files with 55 additions and 29 deletions

32
Cargo.lock generated
View file

@ -547,7 +547,27 @@ version = "5.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a49173b84e034382284f27f1af4dcbbd231ffa358c0fe316541a7337f376a35" checksum = "9a49173b84e034382284f27f1af4dcbbd231ffa358c0fe316541a7337f376a35"
dependencies = [ 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]] [[package]]
@ -2250,6 +2270,7 @@ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"rust-embed-utils", "rust-embed-utils",
"shellexpand",
"syn 2.0.28", "syn 2.0.28",
"walkdir", "walkdir",
] ]
@ -2409,6 +2430,15 @@ dependencies = [
"lazy_static", "lazy_static",
] ]
[[package]]
name = "shellexpand"
version = "2.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ccc8076840c4da029af4f87e4e8daeb0fca6b87bbb02e10cb60b791450e11e4"
dependencies = [
"dirs",
]
[[package]] [[package]]
name = "signal-hook" name = "signal-hook"
version = "0.3.17" version = "0.3.17"

View file

@ -15,7 +15,7 @@ humantime = "2.1.0"
humantime-serde = "1.1.1" humantime-serde = "1.1.1"
mime_guess = "2.0.4" mime_guess = "2.0.4"
rand = "0.8.5" 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"] } serde = { version = "1.0.181", features = ["derive"] }
sqlx = { version = "0.7.1", features = ["runtime-tokio", "sqlite", "time"] } sqlx = { version = "0.7.1", features = ["runtime-tokio", "sqlite", "time"] }
time = { version = "0.3.25", features = ["formatting", "macros", "parsing"] } time = { version = "0.3.25", features = ["formatting", "macros", "parsing"] }

View file

@ -6,38 +6,32 @@ use std::{
use walkdir::WalkDir; 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) { fn watch_dir(path: &Path) {
WalkDir::new(path) WalkDir::new(path)
.into_iter() .into_iter()
.for_each(|e| println!("cargo:rerun-if-changed={}", e.unwrap().path().display())); .for_each(|e| println!("cargo:rerun-if-changed={}", e.unwrap().path().display()));
} }
fn run_tsc() { fn run_tsc(static_out_dir: &Path) {
let status = Command::new("tsc").status().unwrap(); let status = Command::new("tsc")
.arg("--outDir")
.arg(static_out_dir)
.status()
.unwrap();
assert!(status.success(), "tsc produced errors"); assert!(status.success(), "tsc produced errors");
} }
fn copy_static_files() { fn copy_static_files(static_dir: &Path, static_out_dir: &Path) {
let files = WalkDir::new(STATIC_DIR) let files = WalkDir::new(static_dir)
.into_iter() .into_iter()
.map(|e| e.unwrap()) .map(|e| e.unwrap())
.filter(|e| e.file_type().is_file()) .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)
});
for file in files { for file in files {
let components = file.path().components().collect::<Vec<_>>(); let components = file.path().components().collect::<Vec<_>>();
let relative_path = components.into_iter().rev().take(file.depth()).rev(); 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); target.extend(relative_path);
fs::create_dir_all(target.parent().unwrap()).unwrap(); fs::create_dir_all(target.parent().unwrap()).unwrap();
fs::copy(file.path(), target).unwrap(); fs::copy(file.path(), target).unwrap();
@ -49,16 +43,18 @@ fn main() {
builder.git_sha(false); builder.git_sha(false);
builder.emit().unwrap(); builder.emit().unwrap();
watch_dir(MIGRATION_DIR.as_ref()); let out_dir: PathBuf = std::env::var("OUT_DIR").unwrap().into();
watch_dir(STATIC_DIR.as_ref()); let static_out_dir = out_dir.join("static");
watch_dir(TEMPLATE_DIR.as_ref());
// Since remove_dir_all fails if the directory doesn't exist, we ensure it // 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 // exists before deleting it. This way, we can use the remove_dir_all Result
// to ensure the directory was deleted successfully. // to ensure the directory was deleted successfully.
fs::create_dir_all(STATIC_OUT_DIR).unwrap(); fs::create_dir_all(&static_out_dir).unwrap();
fs::remove_dir_all(STATIC_OUT_DIR).unwrap(); fs::remove_dir_all(&static_out_dir).unwrap();
run_tsc(); watch_dir("scripts".as_ref());
copy_static_files(); run_tsc(&static_out_dir);
watch_dir("static".as_ref());
copy_static_files("static".as_ref(), &static_out_dir);
} }

View file

@ -7,7 +7,7 @@ use axum::{
use rust_embed::RustEmbed; use rust_embed::RustEmbed;
#[derive(RustEmbed)] #[derive(RustEmbed)]
#[folder = "target/static"] #[folder = "$OUT_DIR/static"]
pub struct StaticFiles; pub struct StaticFiles;
pub struct StaticFile<T>(T); pub struct StaticFile<T>(T);

View file

@ -1,10 +1,10 @@
{ // See also https://aka.ms/tsconfig { // See also https://aka.ms/tsconfig
"include": [ "static/**/*" ], "include": [ "scripts/**/*" ],
"compilerOptions": { "compilerOptions": {
"target": "ES2015", "target": "ES2015",
"module": "ES2015", "module": "ES2015",
"rootDir": "static", "rootDir": "scripts",
"outDir": "target/static", "outDir": "target/static",
// Misc // Misc