v6/src/main.rs

88 lines
2.5 KiB
Rust
Raw Normal View History

2022-12-10 13:15:32 -05:00
#![feature(let_chains)]
mod generator;
use clap::{Command, arg, command};
2024-12-31 18:58:47 -05:00
use debounced::debounced;
use futures::FutureExt;
use generator::{FileWatcher, content_base_path};
2022-12-10 13:15:32 -05:00
use log::info;
2024-12-31 18:58:47 -05:00
use std::cell::RefCell;
use std::rc::Rc;
use std::time::Duration;
use tokio::pin;
use tokio_stream::StreamExt;
use tokio_stream::wrappers::UnboundedReceiverStream;
2022-12-10 13:15:32 -05:00
#[tokio::main]
async fn main() {
env_logger::init();
let matches = command!()
.subcommand_required(true)
.arg_required_else_help(true)
2024-12-31 18:58:47 -05:00
.subcommand(
Command::new("gen")
.arg(arg!(--watch "Watch the site directory and regenerate on changes")),
)
2022-12-10 13:15:32 -05:00
.subcommand(
Command::new("serve")
.arg(arg!(--watch "Watch the site directory and regenerate on changes")),
)
.get_matches();
2023-01-06 00:23:26 -05:00
if cfg!(debug_assertions) {
2023-01-06 00:17:41 -05:00
info!("Running in dev mode");
2023-01-06 00:26:21 -05:00
} else {
info!("Running in release mode");
2023-01-06 00:17:41 -05:00
}
2022-12-10 13:15:32 -05:00
match matches.subcommand() {
2024-12-31 18:58:47 -05:00
Some(("gen", matches)) => {
let watcher = Rc::new(RefCell::new(FileWatcher::new()));
let mut graph = generator::generate(Rc::clone(&watcher))
.await
.expect("generating");
if matches.contains_id("watch") {
let (tx, rx) = tokio::sync::mpsc::unbounded_channel::<()>();
2025-01-01 23:31:50 -05:00
watcher.borrow_mut().watch(content_base_path(), move || {
2024-12-31 18:58:47 -05:00
tx.send(()).expect("sending regenerate signal");
});
let mut debounced =
2025-01-01 23:31:50 -05:00
debounced(UnboundedReceiverStream::new(rx), Duration::from_millis(100));
2024-12-31 18:58:47 -05:00
2025-01-01 23:31:50 -05:00
let watch = FileWatcher::start(watcher).fuse();
2024-12-31 18:58:47 -05:00
let regenerate = async move {
while let Some(_) = debounced.next().await {
info!("Regenerating");
graph.evaluate_async().await;
}
}
.fuse();
pin!(regenerate, watch);
loop {
futures::select! {
watcher_res = watch => {
watcher_res.expect("watching files");
}
_ = regenerate => {
info!("regenerate channel closed");
}
}
}
}
2022-12-10 13:15:32 -05:00
}
Some(("serve", _matches)) => {
todo!()
2022-12-10 13:15:32 -05:00
}
_ => unreachable!(),
}
}