nearly finished rewrite
This commit is contained in:
@@ -7,30 +7,33 @@ use notify::{
|
||||
};
|
||||
use std::{env, path::Path};
|
||||
|
||||
use crate::app::{ commands::executions::build_execution::{self, BuildExecution, DockiBuildResult}, config::config::Config, log::display_status, watcher::watcher};
|
||||
use crate::app::watcher::watcher;
|
||||
use crate::app::log::display_status;
|
||||
use crate::app::config::config::Config;
|
||||
use crate::app::build::{DockiBuildResult, DockiBuilder};
|
||||
|
||||
pub async fn serve(config: &Config) {
|
||||
let build_execution = BuildExecution::new(config);
|
||||
let mut server = Server::new(build_execution, config);
|
||||
let builder = DockiBuilder::new(config);
|
||||
let mut server = Server::new(builder, config);
|
||||
server.serve().await;
|
||||
}
|
||||
|
||||
struct Server<'a> {
|
||||
build_execution: BuildExecution<'a>,
|
||||
builder: DockiBuilder<'a>,
|
||||
config: &'a Config,
|
||||
}
|
||||
|
||||
impl <'a> Server <'a> {
|
||||
pub fn new(build_execution: BuildExecution<'a>, config: &'a Config) -> Self {
|
||||
fn new(builder: DockiBuilder<'a>, config: &'a Config) -> Self {
|
||||
return Self {
|
||||
build_execution: build_execution,
|
||||
builder,
|
||||
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");
|
||||
self.builder.prepare().await.expect("could not prepare for build");
|
||||
self.builder.build_docs().expect("build failed");
|
||||
tokio::join!(self.start_server(), self.watch_and_build());
|
||||
}
|
||||
|
||||
@@ -48,13 +51,13 @@ impl <'a> Server <'a> {
|
||||
};
|
||||
}
|
||||
|
||||
async fn watch_and_build(&mut self) {
|
||||
async fn watch_and_build(&self) {
|
||||
self.watch()
|
||||
.await
|
||||
.expect("something went wrong")
|
||||
}
|
||||
|
||||
async fn watch(&mut self) -> notify::Result<()> {
|
||||
async fn watch(&self) -> notify::Result<()> {
|
||||
let path = Path::new(&self.config.input_dir);
|
||||
let (mut watcher, mut rx) = watcher()?;
|
||||
|
||||
@@ -69,54 +72,50 @@ impl <'a> Server <'a> {
|
||||
|
||||
}
|
||||
|
||||
fn file_change(&mut self, event: Event) {
|
||||
fn file_change(&self, event: Event) {
|
||||
match event.kind {
|
||||
EventKind::Modify(ModifyKind::Data(_)) => self.build_file(event.paths),
|
||||
EventKind::Modify(ModifyKind::Data(_)) => self.build_valid_files(event.paths),
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
|
||||
fn build_file(&mut self, paths: Vec<std::path::PathBuf>) {
|
||||
fn build_valid_files(&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(¤t_dir())
|
||||
.strip_prefix(&Self::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);
|
||||
let result = self.builder.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::Slide(out_path) => Self::display_rebuilding_status("Slide", &in_path, &out_path),
|
||||
DockiBuildResult::Doc(out_path) => Self::display_rebuilding_status("Doc", &in_path, &out_path),
|
||||
DockiBuildResult::Copy(out_path) => Self::display_rebuilding_status("Copy", &in_path, &out_path),
|
||||
DockiBuildResult::Err(err) => {
|
||||
display_rebuilding_status("Error", &in_path, "");
|
||||
Self::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)
|
||||
}
|
||||
|
||||
fn current_dir() -> String {
|
||||
let err_message = "something went wrong";
|
||||
return String::from(
|
||||
env::current_dir()
|
||||
.expect(err_message)
|
||||
.to_str()
|
||||
.expect(err_message),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
fn display_rebuilding_status(context: &str, in_path: &str, out_path: &str) {
|
||||
display_status("Rebuildng", context, in_path, out_path)
|
||||
}
|
||||
|
||||
fn current_dir() -> String {
|
||||
let err_message = "something went wrong";
|
||||
return String::from(
|
||||
env::current_dir()
|
||||
.expect(err_message)
|
||||
.to_str()
|
||||
.expect(err_message),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user