lavender.software/build_src/main.rs

85 lines
2.1 KiB
Rust

use siru::prelude::*;
use std::{convert::TryInto, fs, path::Path, path::PathBuf, sync::Arc, time::Duration};
type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
mod assets;
mod main_page;
mod webring;
pub struct BuildContext {
source_dir: PathBuf,
output_dir: PathBuf,
write_pipeline: WritePipeline,
}
impl SiruFS for BuildContext {
fn get_source_dir(&self) -> &PathBuf {
&self.source_dir
}
fn get_output_dir(&self) -> &PathBuf {
&self.output_dir
}
fn get_write_pipeline(&self) -> &WritePipeline {
&self.write_pipeline
}
}
pub fn copy_dir_recursive(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> std::io::Result<()> {
fs::create_dir_all(&dst)?;
for entry in fs::read_dir(src)? {
let entry = entry?;
let ty = entry.file_type()?;
if ty.is_dir() {
copy_dir_recursive(entry.path(), dst.as_ref().join(entry.file_name()))?;
} else {
fs::copy(entry.path(), dst.as_ref().join(entry.file_name()))?;
}
}
Ok(())
}
fn build() {
let ctx = BuildContext {
source_dir: "src".try_into().unwrap(),
output_dir: "dist".try_into().unwrap(),
write_pipeline: WritePipeline::new(),
};
let ctx = Arc::new(ctx);
[
main_page::main_page,
assets::copy_assets,
webring::copy_webring,
]
.iter()
.map(|f| {
let ctx = Arc::clone(&ctx);
std::thread::spawn(move || f(&ctx).unwrap())
})
.for_each(|t| t.join().unwrap());
}
fn main() {
build();
if matches!(std::env::args().nth(1).as_deref(), Some("watch")) {
use notify::*;
let (tx, rx) = std::sync::mpsc::channel();
let mut watcher = watcher(tx, Duration::from_millis(100)).unwrap();
watcher.watch("./src", RecursiveMode::Recursive).unwrap();
loop {
match rx.recv() {
Ok(_) => build(),
Err(_) => break,
}
}
}
}