reworked code and added ability to build slides
This commit is contained in:
@@ -1,37 +1,54 @@
|
||||
use std::{collections::HashMap, fs, path::Path};
|
||||
|
||||
use crate::app::builder::{asciidoctor::AsciiDoctorBuilder, Builder};
|
||||
use crate::app::{
|
||||
builder::{asciidoctor::{AsciiDoctorDocsBuilder, AsciiDoctorSlideBuilder}, Builder},
|
||||
fs_util,
|
||||
rx::Observable,
|
||||
};
|
||||
|
||||
use super::traits::Command;
|
||||
|
||||
pub struct Build {
|
||||
builder: Box<dyn Builder>,
|
||||
slides_builder: Box<dyn Builder>,
|
||||
docs_builder: Box<dyn Builder>
|
||||
}
|
||||
|
||||
impl Command for Build {
|
||||
fn execute(&self, _args: &HashMap<String, String>) -> Result<(), String> {
|
||||
let path = format!("./docs/");
|
||||
let mut error_count = 0;
|
||||
let path = "./docs/".to_string();
|
||||
|
||||
if !self.docs_directory_exists(&path) {
|
||||
error_count += 1;
|
||||
println!(
|
||||
"docs directory does not exist. Either create it or clone the template from gitlab"
|
||||
)
|
||||
} else {
|
||||
for result in self.build_dir(&path) {
|
||||
match result {
|
||||
Err(e) => {
|
||||
error_count += 1;
|
||||
println!("{e}");
|
||||
}
|
||||
Ok(()) => println!("success"),
|
||||
};
|
||||
}
|
||||
if !Self::docs_directory_exists(&path) {
|
||||
return Self::docs_directory_missing();
|
||||
}
|
||||
|
||||
if error_count > 0 {
|
||||
return Err(format!("failed with {} errors", error_count));
|
||||
let result = fs_util::fetch_paths_recursive(&path, ".adoc");
|
||||
|
||||
let Ok(paths) = result else {
|
||||
return Err(result.unwrap_err())
|
||||
};
|
||||
|
||||
for (index, path) in paths.iter().enumerate() {
|
||||
if path.starts_with("./docs/slides") {
|
||||
if self.build_slide(&path).is_ok() {
|
||||
println!(
|
||||
"({} / {}) {} -> {}",
|
||||
index,
|
||||
paths.len(),
|
||||
path,
|
||||
path.replace(".adoc", ".html")
|
||||
);
|
||||
}
|
||||
} else {
|
||||
if self.build_doc(&path).is_ok() {
|
||||
println!(
|
||||
"({} / {}) {} -> {}",
|
||||
index,
|
||||
paths.len(),
|
||||
path,
|
||||
path.replace(".adoc", ".html")
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Ok(());
|
||||
@@ -42,51 +59,38 @@ impl Command for Build {
|
||||
Self: Sized,
|
||||
{
|
||||
return Build {
|
||||
builder: Box::new(AsciiDoctorBuilder {}),
|
||||
slides_builder: Box::new(AsciiDoctorSlideBuilder {}),
|
||||
docs_builder: Box::new(AsciiDoctorDocsBuilder {}),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
impl Build {
|
||||
fn build_dir(&self, path: &str) -> Vec<Result<(), String>> {
|
||||
let mut results = vec![];
|
||||
let Ok(dirs) = fs::read_dir(path) else {
|
||||
return vec![Err(format!("direcotry {path} was not found. The filesystem was maybe updated while build"))]
|
||||
};
|
||||
|
||||
for result in dirs {
|
||||
let Ok(entry) = result else {
|
||||
return vec![Err("could not read entry".to_string())];
|
||||
};
|
||||
|
||||
let path = entry
|
||||
.path()
|
||||
.to_str()
|
||||
.expect("could not get text path")
|
||||
.to_string()
|
||||
.clone();
|
||||
|
||||
if entry.path().is_dir() {
|
||||
results = [results, self.build_dir(&path)].concat()
|
||||
} else {
|
||||
results.push(self.build_file(&path));
|
||||
}
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
fn build_file(&self, path: &str) -> Result<(), String> {
|
||||
fn build_file(&self, builder: &Box<dyn Builder>, path: &str) -> Result<(), String> {
|
||||
let out_path = path
|
||||
.clone()
|
||||
.replace("docs", "dist")
|
||||
.replace(".adoc", ".html");
|
||||
|
||||
return self.builder.build(&path, &out_path);
|
||||
return builder.build(&path, &out_path);
|
||||
}
|
||||
|
||||
fn docs_directory_exists(&self, path: &String) -> bool {
|
||||
fn build_doc(&self, path: &str) -> Result<(), String> {
|
||||
return self.build_file(&self.docs_builder, path);
|
||||
}
|
||||
|
||||
fn build_slide(&self, path: &str) -> Result<(), String> {
|
||||
return self.build_file(&self.slides_builder, path);
|
||||
}
|
||||
|
||||
fn docs_directory_exists(path: &String) -> bool {
|
||||
Path::new(path).is_dir()
|
||||
}
|
||||
}
|
||||
|
||||
fn docs_directory_missing() -> Result<(), String> {
|
||||
return Err(
|
||||
"direcotry {path} was not found. The filesystem was maybe updated while build"
|
||||
.to_string(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user