diff --git a/src/app/builder/asciidoctor.rs b/src/app/builder/asciidoctor.rs new file mode 100644 index 0000000..13b48b9 --- /dev/null +++ b/src/app/builder/asciidoctor.rs @@ -0,0 +1,33 @@ +use std::process; + +use super::Builder; + +pub struct AsciiDoctorBuilder; + +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)); + } + } else { + return Err("command failed to execute".to_string()); + } + } +} + +impl AsciiDoctorBuilder { + fn from_utf8(input: Vec) -> String { + return match String::from_utf8(input) { + Ok(m) => m, + Err(e) => panic!("could not print error message: {}", e), + }; + } +} diff --git a/src/app/builder/mod.rs b/src/app/builder/mod.rs new file mode 100644 index 0000000..6f4cdff --- /dev/null +++ b/src/app/builder/mod.rs @@ -0,0 +1,5 @@ +pub mod asciidoctor; + +pub trait Builder { + fn build(&self, in_path: &str, out_path: &str) -> Result<(), String>; +} diff --git a/src/app/commands/build.rs b/src/app/commands/build.rs index 42361af..f19af2a 100644 --- a/src/app/commands/build.rs +++ b/src/app/commands/build.rs @@ -1,45 +1,47 @@ -use std::{collections::HashMap, env, fs, process}; +use std::{collections::HashMap, env, fs}; + +use crate::app::builder::{Builder, asciidoctor::AsciiDoctorBuilder}; use super::traits::Command; -pub struct Build; +pub struct Build { + builder: Box +} + impl Build { - fn build_dir(&self, path: String) -> Result<(), String> { + fn build_dir(&self, path: String) -> Vec> { + let mut results = vec![]; let Ok(dirs) = fs::read_dir(path) else { - return Err("could not read file system".to_string()); + return vec![Err("could not read file system".to_string())] }; for result in dirs { let Ok(entry) = result else { - return Err("could not read entry".to_string()); - }; + return vec![Err("could not read entry".to_string())]; + }; + + let path = entry.path().to_str() + .expect("could not get text path"); if entry.path().is_dir() { - self.build_dir(entry.path().to_str().unwrap().to_string()); + results = [ + results, vec![self.build_file(&path)] + ].concat() } else { - self.build_file(entry.path().to_str().unwrap().to_string()); + results.push(self.build_file(&path)); } } - return Err("No files to build".to_string()); + return results; } - fn build_file(&self, path: String) -> Result<(), String> { + fn build_file(&self, path: &str) -> Result<(), String> { let out_path = path .clone() .replace("docs", "dist") .replace(".adoc", ".html"); - println!("asciidoctor {path} --out-file={out_path}"); - let _result = process::Command::new("asciidoctor") - .arg(format!("{path}")) - .arg(format!("--out-file={out_path}")) - .output() - .expect("fuck"); - - dbg!(_result); - - return Ok(()); + return self.builder.build(&path, &out_path); } } @@ -54,14 +56,31 @@ impl Command for Build { }; let path = format!("{project_cwd}/docs/"); + let mut error_count = 0; - return self.build_dir(path); + for result in self.build_dir(path) { + match result { + Err(e) => { + error_count += 1; + println!("{e}"); + }, + Ok(()) => println!("success") + }; + } + + if error_count > 0{ + return Err(format!("failed with {} errors", error_count)) + } + + return Ok(()); } fn new() -> Self where Self: Sized, { - return Build {}; + return Build { + builder: Box::new(AsciiDoctorBuilder {}) + }; } } diff --git a/src/app/mod.rs b/src/app/mod.rs index 0fef376..c8212e7 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -1,4 +1,5 @@ mod commands; +pub mod builder; use std::collections::HashMap;