Refactor build script
This commit is contained in:
parent
8b5bfdd231
commit
8b0e6fe268
3 changed files with 36 additions and 22 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
|
@ -783,6 +783,7 @@ dependencies = [
|
|||
"mime_guess",
|
||||
"rust-embed",
|
||||
"tokio",
|
||||
"walkdir",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
|
|||
|
|
@ -11,3 +11,6 @@ axum = { version = "0.6.19", features = ["macros"] }
|
|||
mime_guess = "2.0.4"
|
||||
rust-embed = "6.8.1"
|
||||
tokio = { version = "1.29.1", features = ["full"] }
|
||||
|
||||
[build-dependencies]
|
||||
walkdir = "2.3.3"
|
||||
|
|
|
|||
54
build.rs
54
build.rs
|
|
@ -4,27 +4,40 @@ use std::{
|
|||
process::Command,
|
||||
};
|
||||
|
||||
use walkdir::WalkDir;
|
||||
|
||||
const STATIC_DIR: &str = "static";
|
||||
const STATIC_OUT_DIR: &str = "target/static";
|
||||
const STATIC_IGNORE_EXT: &[&str] = &["ts"];
|
||||
|
||||
fn copy_recursively(path: &Path) {
|
||||
let from = PathBuf::new().join(STATIC_DIR).join(path);
|
||||
let to = PathBuf::new().join(STATIC_OUT_DIR).join(path);
|
||||
|
||||
println!("cargo:rerun-if-changed={}", from.display());
|
||||
|
||||
if from.is_file() {
|
||||
if from.extension() == Some("ts".as_ref()) {
|
||||
return;
|
||||
fn watch_dir(path: &Path) {
|
||||
WalkDir::new(path)
|
||||
.into_iter()
|
||||
.for_each(|e| println!("cargo:rerun-if-changed={}", e.unwrap().path().display()));
|
||||
}
|
||||
fs::create_dir_all(to.parent().unwrap()).unwrap();
|
||||
fs::copy(from, to).unwrap();
|
||||
} else if from.is_dir() {
|
||||
for entry in from.read_dir().unwrap() {
|
||||
copy_recursively(&path.join(entry.unwrap().file_name()));
|
||||
|
||||
fn run_tsc() {
|
||||
let status = Command::new("tsc").status().unwrap();
|
||||
assert!(status.success(), "tsc produced errors");
|
||||
}
|
||||
} else {
|
||||
panic!("Unexpected file type at {}", from.display());
|
||||
|
||||
fn copy_static_files() {
|
||||
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)
|
||||
});
|
||||
|
||||
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);
|
||||
target.extend(relative_path);
|
||||
fs::create_dir_all(target.parent().unwrap()).unwrap();
|
||||
fs::copy(file.path(), target).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -35,10 +48,7 @@ fn main() {
|
|||
fs::create_dir_all(STATIC_OUT_DIR).unwrap();
|
||||
fs::remove_dir_all(STATIC_OUT_DIR).unwrap();
|
||||
|
||||
// Run typescript compiler
|
||||
let status = Command::new("tsc").status().unwrap();
|
||||
assert!(status.success(), "tsc produced errors");
|
||||
|
||||
// Copy remaining static files
|
||||
copy_recursively("".as_ref());
|
||||
watch_dir(STATIC_DIR.as_ref());
|
||||
run_tsc();
|
||||
copy_static_files();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue