refined display for building

This commit is contained in:
2023-01-25 22:03:49 +01:00
parent 6153df1474
commit 90955bdc40
4 changed files with 82 additions and 47 deletions

2
Cargo.lock generated
View File

@@ -4,4 +4,4 @@ version = 3
[[package]] [[package]]
name = "docki" name = "docki"
version = "0.1.0" version = "0.1.1"

View File

@@ -1,4 +1,4 @@
use std::{collections::HashMap, path::Path}; use std::{collections::HashMap, path::Path, fs};
use crate::app::{ use crate::app::{
builder::{ builder::{
@@ -19,11 +19,11 @@ impl Command for Build {
fn execute(&self, _args: &HashMap<String, String>) -> Result<(), String> { fn execute(&self, _args: &HashMap<String, String>) -> Result<(), String> {
let path = "./docs/".to_string(); let path = "./docs/".to_string();
if !Self::docs_directory_exists(&path) { if !Self::directory_exists(&path) {
return Self::docs_directory_missing(); return Self::docs_directory_missing();
} }
let result = fs_util::fetch_paths_recursive(&path, ".adoc"); let result = fs_util::fetch_paths_recursive(&path);
let Ok(paths) = result else { let Ok(paths) = result else {
return Err(result.unwrap_err()) return Err(result.unwrap_err())
@@ -31,14 +31,17 @@ impl Command for Build {
for (index, path) in paths.iter().enumerate() { for (index, path) in paths.iter().enumerate() {
let progress = index + 1; let progress = index + 1;
if path.starts_with("./docs/slides") { let goal = paths.len();
if self.build_slide(&path).is_ok() {
Self::display_status(paths.len(), progress, &path, "slide") 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, goal, progress)
} else if path.ends_with(".adoc") {
let out_path = path.clone().replace("adoc", "html").replace("/docs/", "/dist/");
self.build_doc(&path, &out_path, goal, progress)
} else { } else {
if self.build_doc(&path).is_ok() { let out_path = path.clone().replace("/docs/", "/dist/");
Self::display_status(paths.len(), progress, &path, "doc") self.copy(&path, &out_path, goal, progress)
}
} }
} }
@@ -57,35 +60,73 @@ impl Command for Build {
} }
impl Build { impl Build {
fn build_file(&self, builder: &Box<dyn Builder>, path: &str) -> Result<(), String> { fn build_file(&self, builder: &Box<dyn Builder>, in_path: &str, out_path: &str) -> Result<(), String> {
let out_path = path return builder.build(&in_path, &out_path);
.clone()
.replace("docs", "dist")
.replace(".adoc", ".html");
return builder.build(&path, &out_path);
} }
fn display_status(goal: usize, progress: usize, path: &str, conversion_type: &str) -> () { fn build_file_and_status(
&self,
builder: &Box<dyn Builder>,
in_path: &str,
out_path: &str,
goal: usize,
progress: usize,
conversion_type: &str,
) {
let result = self.build_file(builder, in_path, out_path);
if result.is_ok() {
Self::display_status(goal, progress, in_path, out_path, conversion_type)
} else {
Self::display_status(goal, progress, in_path, out_path, "error");
let error = result.unwrap_err();
println!("{error}");
}
}
fn copy(&self, in_path: &str, out_path: &str, goal: usize, progress: usize) {
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(goal, progress, in_path, out_path, "copy")
} else {
Self::display_status(goal, progress, 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(goal: usize, progress: usize, in_path: &str, out_path: &str, conversion_type: &str) -> () {
println!( println!(
"({} / {}) [{}] {} -> {}", "({} / {}) [{}] {} -> {}",
progress, progress,
goal, goal,
conversion_type, conversion_type,
path, in_path,
path.replace(".adoc", ".html") out_path
); );
} }
fn build_doc(&self, path: &str) -> Result<(), String> { fn build_doc(&self, in_path: &str, out_path: &str, goal: usize, progress: usize) {
return self.build_file(&self.docs_builder, path); self.build_file_and_status(&self.docs_builder, in_path, out_path, goal, progress, "doc");
} }
fn build_slide(&self, path: &str) -> Result<(), String> { fn build_slide(&self, path: &str, out_path: &str, goal: usize, progress: usize) {
return self.build_file(&self.slides_builder, path); return self.build_file_and_status(&self.slides_builder, path, out_path, goal, progress, "slide");
} }
fn docs_directory_exists(path: &String) -> bool { fn directory_exists(path: &String) -> bool {
Path::new(path).is_dir() Path::new(path).is_dir()
} }

View File

@@ -2,17 +2,15 @@ use std::fs;
struct RecursivePathFetch { struct RecursivePathFetch {
paths: Vec<String>, paths: Vec<String>,
ends_with: String, path: String,
path: String
} }
impl RecursivePathFetch { impl RecursivePathFetch {
pub fn new_with_extension_filter(path: String, ends_with: String) -> Self { pub fn new_with_extension_filter(path: String) -> Self {
return Self { return Self {
paths: vec![], paths: vec![],
ends_with, path,
path };
}
} }
pub fn fetch(&mut self) -> Result<Vec<String>, String> { pub fn fetch(&mut self) -> Result<Vec<String>, String> {
@@ -34,9 +32,7 @@ impl RecursivePathFetch {
let str_path = path.to_str().unwrap(); let str_path = path.to_str().unwrap();
if path.is_file() { if path.is_file() {
if str_path.ends_with(&self.ends_with) {
self.paths.push(str_path.to_string()) self.paths.push(str_path.to_string())
}
} else if path.is_dir() { } else if path.is_dir() {
let read_result = self.read_dir(str_path.to_string()); let read_result = self.read_dir(str_path.to_string());
if read_result.is_err() { if read_result.is_err() {
@@ -45,22 +41,20 @@ impl RecursivePathFetch {
} }
} }
return Ok(()) return Ok(());
} }
fn dir_not_found(&self) -> Result<(), String> { fn dir_not_found(&self) -> Result<(), String> {
return Err(format!( return Err(format!(
"directory {} was not found or was changed while building", "directory {} was not found or was changed while building",
self.path self.path
)) ));
} }
} }
pub fn fetch_paths_recursive(path: &str, ends_with: &str) -> Result<Vec<String>, String> { pub fn fetch_paths_recursive(path: &str) -> Result<Vec<String>, String> {
let mut path_fetch = RecursivePathFetch::new_with_extension_filter( let mut path_fetch =
path.to_string(), RecursivePathFetch::new_with_extension_filter(path.to_string());
ends_with.to_string()
);
return path_fetch.fetch(); return path_fetch.fetch();
} }

View File

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