builder rewrite wip

This commit is contained in:
2025-11-12 22:49:59 +01:00
parent fc98b48601
commit c33428f3e1
4 changed files with 153 additions and 142 deletions

View File

@@ -7,78 +7,104 @@ use notify::{
};
use std::{env, path::Path};
use crate::app::{ build::{DockiBuildResult, docki_build}, commands::build::build, config::config::Config, log::display_status, watcher::watcher};
use crate::app::{ commands::executions::build_execution::{self, BuildExecution, DockiBuildResult}, config::config::Config, log::display_status, watcher::watcher};
pub async fn serve(config: &Config) {
build(config).await;
tokio::join!(watch_and_build(&config.input_dir), start_server(config.port));
let build_execution = BuildExecution::new(config);
let mut server = Server::new(build_execution, config);
server.serve().await;
}
async fn watch_and_build(docs_dir: &str) {
watch(Path::new(docs_dir), docs_dir)
.await
.expect("something went wrong")
struct Server<'a> {
build_execution: BuildExecution<'a>,
config: &'a Config,
}
impl <'a> Server <'a> {
pub fn new(build_execution: BuildExecution<'a>, config: &'a Config) -> Self {
return Self {
build_execution: build_execution,
config: config
}
}
async fn serve(&mut self) {
self.build_execution.prepare().await.expect("could not prepare for build");
self.build_execution.build_dir().expect("build failed");
tokio::join!(self.start_server(), self.watch_and_build());
}
async fn start_server(&self) {
let link = &format!("http://localhost:{}", self.config.port);
let hyperlink = Green.paint(link).hyperlink(link);
println!(
"\nServing at {}",
hyperlink
);
let Ok(()) = listen("localhost", self.config.port, "./dist").await else {
panic!("could not start server")
};
}
async fn watch_and_build(&mut self) {
self.watch()
.await
.expect("something went wrong")
}
async fn watch(&mut self) -> notify::Result<()> {
let path = Path::new(&self.config.input_dir);
let (mut watcher, mut rx) = watcher()?;
watcher.watch(path.as_ref(), RecursiveMode::Recursive)?;
while let Some(res) = rx.next().await {
let event = res.expect("watching failed");
self.file_change(event)
}
Ok(())
}
fn file_change(&mut self, event: Event) {
match event.kind {
EventKind::Modify(ModifyKind::Data(_)) => self.build_file(event.paths),
_ => (),
}
}
fn build_file(&mut self, paths: Vec<std::path::PathBuf>) {
let invalid_path_message = "changed path is invalid";
let in_path = paths
.first()
.expect(invalid_path_message)
.strip_prefix(&current_dir())
.expect(invalid_path_message)
.to_str()
.expect(invalid_path_message);
let in_path = format!("./{}", in_path);
let result = self.build_execution.build_file(&in_path);
match result {
DockiBuildResult::Slide(out_path) => display_rebuilding_status("Slide", &in_path, &out_path),
DockiBuildResult::Doc(out_path) => display_rebuilding_status("Doc", &in_path, &out_path),
DockiBuildResult::Copy(out_path) => display_rebuilding_status("Copy", &in_path, &out_path),
DockiBuildResult::Err(err) => {
display_rebuilding_status("Error", &in_path, "");
println!("{}", err);
},
}
}
}
async fn start_server(port: u16) {
let link = &format!("http://localhost:{}", port);
let hyperlink = Green.paint(link).hyperlink(link);
println!(
"\nServing at {}",
hyperlink
);
let Ok(()) = listen("localhost", port, "./dist").await else {
panic!("could not start server")
};
}
async fn watch(path: &Path, docs_dir: &str) -> notify::Result<()> {
let (mut watcher, mut rx) = watcher()?;
watcher.watch(path.as_ref(), RecursiveMode::Recursive)?;
while let Some(res) = rx.next().await {
let event = res.expect("watching failed");
file_change(event, docs_dir)
}
Ok(())
}
fn file_change(event: Event, docs_dir: &str) {
match event.kind {
EventKind::Modify(ModifyKind::Data(_)) => build_file(event.paths, docs_dir),
_ => (),
}
}
fn build_file(paths: Vec<std::path::PathBuf>, docs_dir: &str) {
let invalid_path_message = "changed path is invalid";
let in_path = paths
.first()
.expect(invalid_path_message)
.strip_prefix(&current_dir())
.expect(invalid_path_message)
.to_str()
.expect(invalid_path_message);
let in_path = format!("./{}", in_path);
let result = docki_build(&in_path, false, docs_dir);
match result {
DockiBuildResult::Slide(out_path) => display_rebuilding_status("Slide", &in_path, &out_path),
DockiBuildResult::Doc(out_path) => display_rebuilding_status("Doc", &in_path, &out_path),
DockiBuildResult::Copy(out_path) => display_rebuilding_status("Copy", &in_path, &out_path),
DockiBuildResult::Err(err) => {
display_rebuilding_status("Error", &in_path, "");
println!("{}", err);
},
}
}
fn display_rebuilding_status(context: &str, in_path: &str, out_path: &str) {
display_status("Rebuildng", context, in_path, out_path)