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

View file

@ -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::<Vec<_>>();
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);
}