refactored building of documents

This commit is contained in:
2023-03-12 23:13:08 +01:00
parent f18ce995bb
commit 96c6c8d94d
7 changed files with 119 additions and 153 deletions

View File

@@ -1,18 +1,16 @@
use std::{fs, path::{Path, PathBuf}, io::Cursor};
use std::{
io::Cursor,
path::PathBuf
};
use crate::app::{
builder::{
asciidoctor::{AsciiDoctorDocsBuilder, AsciiDoctorSlideBuilder},
Builder,
},
build::{docki_build, DockiBuildResult},
fs_util,
};
pub struct BuildExecution {
progress: usize,
goal: usize,
doc_builder: Box<dyn Builder>,
slide_builder: Box<dyn Builder>,
}
impl BuildExecution {
@@ -20,95 +18,35 @@ impl BuildExecution {
return BuildExecution {
progress: 0,
goal: 0,
slide_builder: Box::new(AsciiDoctorSlideBuilder {}),
doc_builder: Box::new(AsciiDoctorDocsBuilder {}),
};
}
pub async fn execute(&mut self) -> Result<(), String> {
let path = "./docs/".to_string();
if !Self::directory_exists(&path) {
return Err("docs directory does not exist it. Create it or use the template".to_string())
if !fs_util::directory_exists(&path) {
return Err(
"docs directory does not exist it. Create it or use the template".to_string(),
);
}
if let Err(error) = Self::prepare().await {
return Err(error);
}
}
return self.build_dir(&path);
}
fn build_file(
&self,
builder: &Box<dyn Builder>,
in_path: &str,
out_path: &str,
) -> Result<(), String> {
return builder.build(&in_path, &out_path);
}
fn build_file_and_status(
&self,
builder: &Box<dyn Builder>,
in_path: &str,
out_path: &str,
conversion_type: &str,
) {
let result = self.build_file(builder, in_path, out_path);
if result.is_ok() {
self.display_status(in_path, out_path, conversion_type)
} else {
self.display_status(in_path, out_path, "error");
let error = result.unwrap_err();
println!("{error}");
}
}
fn copy(&self, in_path: &str, out_path: &str) {
let segments: &Vec<&str> = &out_path.split("/").collect();
let parent_dir = &segments[0..segments.len() - 1].join("/");
Self::create_dir_recursive(parent_dir);
let result = fs::copy(in_path, out_path);
if result.is_ok() {
self.display_status(in_path, out_path, "copy");
} else {
self.display_status(in_path, out_path, "error");
let error = result.unwrap_err();
println!("{error}");
}
}
fn create_dir_recursive(path: &str) {
let mut validated_path = "./".to_string();
for segment in path.split("/") {
validated_path.push_str(format!("{segment}/").as_str());
if !Self::directory_exists(&validated_path) {
fs::create_dir(&validated_path).unwrap()
}
}
}
fn display_status(&self, in_path: &str, out_path: &str, conversion_type: &str) -> () {
println!(
"({} / {}) [{}] {} -> {}",
self.progress, self.goal, conversion_type, in_path, out_path
);
}
fn build_doc(&self, in_path: &str, out_path: &str) {
self.build_file_and_status(&self.doc_builder, in_path, out_path, "doc");
}
async fn prepare() -> Result<(), String> {
let reveal_version = "3.9.2";
let target = format!("https://github.com/hakimel/reveal.js/archive/{reveal_version}.zip");
let Ok(response) = reqwest::blocking::get(target) else {
let Ok(response) = reqwest::get(target).await else {
return Err("could not downlaod revealjs".to_string())
};
let Ok(bytes) = response.bytes() else {
let Ok(bytes) = response.bytes().await else {
return Err("could not extract bytes".to_string())
};
@@ -118,15 +56,7 @@ impl BuildExecution {
return Err("could not write extracted archive to disk".to_string());
}
return Ok(())
}
fn build_slide(&self, in_path: &str, out_path: &str) {
self.build_file_and_status(&self.slide_builder, in_path, out_path, "slide");
}
fn directory_exists(path: &String) -> bool {
Path::new(path).is_dir()
return Ok(());
}
fn build_dir(&mut self, path: &str) -> Result<(), String> {
@@ -136,28 +66,30 @@ impl BuildExecution {
return Err(result.unwrap_err())
};
for (index, path) in paths.iter().enumerate() {
for (index, in_path) in paths.iter().enumerate() {
self.progress = index + 1;
self.goal = paths.len();
let result = docki_build(&in_path);
if path.ends_with(".adoc") && path.starts_with("./docs/slides") {
let out_path = path
.clone()
.replace("adoc", "html")
.replace("/docs/", "/dist/");
self.build_slide(&path, &out_path)
} else if path.ends_with(".adoc") {
let out_path = path
.clone()
.replace("adoc", "html")
.replace("/docs/", "/dist/");
self.build_doc(&path, &out_path)
} else {
let out_path = path.clone().replace("/docs/", "/dist/");
self.copy(&path, &out_path)
match result {
DockiBuildResult::Err(err) => {
self.display_status("Error", in_path, "");
println!("{}", err)
},
DockiBuildResult::Copy(out_path) => self.display_status("Copy", &in_path, &out_path),
DockiBuildResult::Slide(out_path) => self.display_status("Slide", &in_path, &out_path),
DockiBuildResult::Doc(out_path) => self.display_status("Doc", &in_path, &out_path)
}
}
return Ok(());
}
fn display_status(&self, status_type: &str, in_path: &str, out_path: &str) -> () {
println!(
"({} / {}) [{}] {} -> {}",
self.progress, self.goal, status_type, in_path, out_path
);
}
}

View File

@@ -7,10 +7,7 @@ use notify::{
};
use std::{env, path::Path};
use crate::app::{builder::{
asciidoctor::{AsciiDoctorDocsBuilder, AsciiDoctorSlideBuilder},
Builder,
}, watcher::watcher};
use crate::app::{ watcher::watcher, build::docki_build};
pub async fn serve() {
@@ -63,19 +60,11 @@ fn build_file(paths: Vec<std::path::PathBuf>) -> Result<(), String> {
.expect(invalid_path_message)
.replace(&current_dir(), "")
.replace("/./", "./");
let out_path = in_path
.replace("./docs/", "./dist/")
.replace(".adoc", ".html");
println!("{} {}", "[Rebuilding]".green(), in_path);
if in_path.starts_with("./docs/slides") {
let slide_builder = AsciiDoctorSlideBuilder {};
slide_builder.build(&in_path, &out_path)
} else {
let doc_builder = AsciiDoctorDocsBuilder {};
doc_builder.build(&in_path, &out_path)
}
docki_build(&in_path);
return Ok(())
}
fn current_dir() -> String {