reworked code and added ability to build slides

This commit is contained in:
2023-01-24 14:12:37 +01:00
parent f76521f6cf
commit f13d24bee0
13 changed files with 187 additions and 84 deletions

1
.gitignore vendored
View File

@@ -1,3 +1,2 @@
/target
dist
docs

0
docs/core/functions.adoc Normal file
View File

0
docs/core/index.adoc Normal file
View File

0
docs/fail.txt Normal file
View File

0
docs/functions.adoc Normal file
View File

0
docs/index.adoc Normal file
View File

View File

@@ -2,28 +2,34 @@ use std::process;
use super::Builder;
pub struct AsciiDoctorBuilder;
fn asciidoctor(postfix: &str, in_path: &str, out_path: &str) -> Result<(), String> {
let result = process::Command::new(format!("asciidoctor{postfix}"))
.arg(format!("{in_path}"))
.arg(format!("--out-file={out_path}"))
.output();
impl Builder for AsciiDoctorBuilder {
fn build(&self, in_path: &str, out_path: &str) -> Result<(), String> {
let result = process::Command::new("asciidoctor")
.arg(format!("{in_path}"))
.arg(format!("--out-file={out_path}"))
.output();
if let Ok(success) = result {
if success.stderr.len() == 0 {
return Ok(());
} else {
return Err(AsciiDoctorBuilder::from_utf8(success.stderr));
}
if let Ok(success) = result {
if success.stderr.len() == 0 {
return Ok(());
} else {
return Err("command failed to execute".to_string());
println!("something went wrong");
return Err(AsciiDoctorDocsBuilder::from_utf8(success.stderr));
}
} else {
println!("{}", result.unwrap_err());
return Err("asciidoctor not installed. You may need to run docki setup!".to_string());
}
}
impl AsciiDoctorBuilder {
pub struct AsciiDoctorDocsBuilder;
impl Builder for AsciiDoctorDocsBuilder {
fn build(&self, in_path: &str, out_path: &str) -> Result<(), String> {
return asciidoctor("", in_path, out_path);
}
}
impl AsciiDoctorDocsBuilder {
fn from_utf8(input: Vec<u8>) -> String {
return match String::from_utf8(input) {
Ok(m) => m,
@@ -31,3 +37,11 @@ impl AsciiDoctorBuilder {
};
}
}
pub struct AsciiDoctorSlideBuilder;
impl Builder for AsciiDoctorSlideBuilder {
fn build(&self, in_path: &str, out_path: &str) -> Result<(), String> {
return asciidoctor("-revealjs-linux", in_path, out_path);
}
}

View File

@@ -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(),
);
}
}

74
src/app/fs_util/mod.rs Normal file
View File

@@ -0,0 +1,74 @@
use std::fs;
struct RecursivePathFetch {
paths: Vec<String>,
ends_with: String,
path: String
}
impl RecursivePathFetch {
pub fn new_with_extension_filter(path: String, ends_with: String) -> Self {
return Self {
paths: vec![],
ends_with,
path
}
}
pub fn new(path: String) -> Self {
return Self {
paths: vec![],
ends_with: "".to_string(),
path
}
}
pub fn fetch(&mut self) -> Result<Vec<String>, String> {
if let Err(error) = self.read_dir(self.path.clone()) {
return Err(error);
} else {
return Ok(self.paths.clone());
}
}
fn read_dir(&mut self, path: String) -> Result<(), String> {
let Ok(entries) = fs::read_dir(path) else {
return self.dir_not_found();
};
for result in entries {
let entry = result.unwrap();
let path = entry.path();
let str_path = path.to_str().unwrap();
if path.is_file() {
if str_path.ends_with(&self.ends_with) {
self.paths.push(str_path.to_string())
}
} else if path.is_dir() {
let read_result = self.read_dir(str_path.to_string());
if read_result.is_err() {
return read_result;
}
}
}
return Ok(())
}
fn dir_not_found(&self) -> Result<(), String> {
return Err(format!(
"directory {} was not found or was changed while building",
self.path
))
}
}
pub fn fetch_paths_recursive(path: &str, ends_with: &str) -> Result<Vec<String>, String> {
let mut path_fetch = RecursivePathFetch::new_with_extension_filter(
path.to_string(),
ends_with.to_string()
);
return path_fetch.fetch();
}

View File

@@ -1,6 +1,7 @@
mod commands;
pub mod builder;
pub mod rx;
pub mod fs_util;
use std::collections::HashMap;

9
src/test/fs_util.rs Normal file
View File

@@ -0,0 +1,9 @@
use crate::app::fs_util;
#[test]
fn test_fetch_asciidoctor_paths_recursive() {
let paths = fs_util::fetch_paths_recursive("docs", ".adoc").unwrap();
let len = paths.len();
dbg!(paths);
assert_eq!(len, 4);
}

View File

@@ -1,13 +1,2 @@
use crate::app::rx::Observable;
#[test]
fn test_observable() {
let mut observable: Observable<u32> = Observable::new();
observable.subscribe(|value| {
assert_eq!(5, value);
});
observable.next(5);
}
mod rx;
mod fs_util;

13
src/test/rx.rs Normal file
View File

@@ -0,0 +1,13 @@
use crate::app::rx::Observable;
#[test]
fn test_observable() {
let mut observable: Observable<u32> = Observable::new();
observable.subscribe(|value| {
assert_eq!(5, value);
});
observable.next(5);
}