mirror of
https://github.com/scratchfoundation/scratch-tech-explorations.git
synced 2025-08-28 21:38:45 -04:00
48 lines
1.5 KiB
Rust
48 lines
1.5 KiB
Rust
|
use std::{path::Path, fs::{self, DirEntry}, io, ffi::OsStr};
|
||
|
|
||
|
fn main() {
|
||
|
copy_fonts();
|
||
|
}
|
||
|
|
||
|
fn copy_files<P: AsRef<Path>, Q: AsRef<Path>, R>(from_dir: P, to_dir: Q, predicate: R) -> io::Result<()>
|
||
|
where R: Fn(&DirEntry) -> bool
|
||
|
{
|
||
|
let dir_entries = fs::read_dir(from_dir.as_ref())?;
|
||
|
for dir_entry in dir_entries {
|
||
|
if let Ok(dir_entry) = dir_entry {
|
||
|
let src_path = dir_entry.path();
|
||
|
if let Some(file_name) = src_path.file_name() {
|
||
|
if predicate(&dir_entry) {
|
||
|
let dest_path = Path::new(to_dir.as_ref()).join(file_name);
|
||
|
eprintln!("copy_files copying {0} to {1}", src_path.display(), dest_path.display());
|
||
|
fs::copy(src_path, dest_path)?;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Ok(())
|
||
|
}
|
||
|
|
||
|
fn copy_fonts() {
|
||
|
let font_extensions = vec![
|
||
|
OsStr::new("ttf"),
|
||
|
OsStr::new("otf"),
|
||
|
];
|
||
|
let src_path = Path::new("node_modules/scratch-render-fonts/src");
|
||
|
let dst_path = Path::new("assets/fonts");
|
||
|
println!("cargo:rerun-if-changed={:?}/*.ttf", src_path);
|
||
|
fs::create_dir_all(dst_path).expect("failed to create fonts directory");
|
||
|
copy_files(src_path, dst_path, |dir_entry| {
|
||
|
let src_path = dir_entry.path();
|
||
|
eprintln!("copy_fonts checking {0}", src_path.display());
|
||
|
if !src_path.is_file() {
|
||
|
return false;
|
||
|
}
|
||
|
match src_path.extension() {
|
||
|
Some(ext) => font_extensions.contains(&ext),
|
||
|
None => false
|
||
|
}
|
||
|
}).expect("failed to copy fonts");
|
||
|
}
|