lavender.software/build_src/main.rs

85 lines
2.1 KiB
Rust
Raw Normal View History

2021-07-12 20:47:49 +00:00
use siru::prelude::*;
2022-03-05 14:29:19 +00:00
use std::{convert::TryInto, fs, path::Path, path::PathBuf, sync::Arc, time::Duration};
2021-07-12 20:47:49 +00:00
type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
mod assets;
mod main_page;
2022-03-05 14:26:10 +00:00
mod webring;
2021-07-12 20:47:49 +00:00
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
}
}
2022-03-05 14:29:19 +00:00
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(())
}
2021-07-12 20:47:49 +00:00
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);
2022-03-05 14:26:10 +00:00
[
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());
2021-07-12 20:47:49 +00:00
}
fn main() {
build();
2021-07-12 20:47:49 +00:00
if matches!(std::env::args().nth(1).as_deref(), Some("watch")) {
use notify::*;
2021-07-12 20:47:49 +00:00
let (tx, rx) = std::sync::mpsc::channel();
2021-07-12 20:47:49 +00:00
let mut watcher = watcher(tx, Duration::from_millis(100)).unwrap();
watcher.watch("./src", RecursiveMode::Recursive).unwrap();
2021-07-12 20:47:49 +00:00
loop {
match rx.recv() {
Ok(_) => build(),
Err(_) => break,
}
2021-07-12 20:47:49 +00:00
}
}
}