extracted the execution code of the command
This commit is contained in:
@@ -1,139 +1,19 @@
|
|||||||
use std::{collections::HashMap, path::Path, fs};
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use crate::app::{
|
use super::{executions::build_execution::BuildExecution, traits::Command};
|
||||||
builder::{
|
|
||||||
asciidoctor::{AsciiDoctorDocsBuilder, AsciiDoctorSlideBuilder},
|
|
||||||
Builder,
|
|
||||||
},
|
|
||||||
fs_util,
|
|
||||||
};
|
|
||||||
|
|
||||||
use super::traits::Command;
|
pub struct Build;
|
||||||
|
|
||||||
pub struct Build {
|
|
||||||
slides_builder: Box<dyn Builder>,
|
|
||||||
docs_builder: Box<dyn Builder>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Command for Build {
|
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 mut build_execution = BuildExecution::new();
|
||||||
|
return build_execution.execute();
|
||||||
if !Self::directory_exists(&path) {
|
|
||||||
return Self::docs_directory_missing();
|
|
||||||
}
|
|
||||||
|
|
||||||
let result = fs_util::fetch_paths_recursive(&path);
|
|
||||||
|
|
||||||
let Ok(paths) = result else {
|
|
||||||
return Err(result.unwrap_err())
|
|
||||||
};
|
|
||||||
|
|
||||||
for (index, path) in paths.iter().enumerate() {
|
|
||||||
let progress = index + 1;
|
|
||||||
let goal = paths.len();
|
|
||||||
|
|
||||||
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 {
|
|
||||||
let out_path = path.clone().replace("/docs/", "/dist/");
|
|
||||||
self.copy(&path, &out_path, goal, progress)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return Ok(());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn new() -> Self
|
fn new() -> Self
|
||||||
where
|
where
|
||||||
Self: Sized,
|
Self: Sized,
|
||||||
{
|
{
|
||||||
return Build {
|
return Build {}
|
||||||
slides_builder: Box::new(AsciiDoctorSlideBuilder {}),
|
|
||||||
docs_builder: Box::new(AsciiDoctorDocsBuilder {}),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Build {
|
|
||||||
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,
|
|
||||||
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!(
|
|
||||||
"({} / {}) [{}] {} -> {}",
|
|
||||||
progress,
|
|
||||||
goal,
|
|
||||||
conversion_type,
|
|
||||||
in_path,
|
|
||||||
out_path
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn build_doc(&self, in_path: &str, out_path: &str, goal: usize, progress: usize) {
|
|
||||||
self.build_file_and_status(&self.docs_builder, in_path, out_path, goal, progress, "doc");
|
|
||||||
}
|
|
||||||
|
|
||||||
fn build_slide(&self, path: &str, out_path: &str, goal: usize, progress: usize) {
|
|
||||||
return self.build_file_and_status(&self.slides_builder, path, out_path, goal, progress, "slide");
|
|
||||||
}
|
|
||||||
|
|
||||||
fn 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(),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
144
src/app/commands/executions/build_execution.rs
Normal file
144
src/app/commands/executions/build_execution.rs
Normal file
@@ -0,0 +1,144 @@
|
|||||||
|
use std::{fs, path::Path};
|
||||||
|
|
||||||
|
use crate::app::{
|
||||||
|
builder::{
|
||||||
|
asciidoctor::{AsciiDoctorDocsBuilder, AsciiDoctorSlideBuilder},
|
||||||
|
Builder,
|
||||||
|
},
|
||||||
|
fs_util,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub struct BuildExecution {
|
||||||
|
progress: usize,
|
||||||
|
goal: usize,
|
||||||
|
doc_builder: Box<dyn Builder>,
|
||||||
|
slide_builder: Box<dyn Builder>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl BuildExecution {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
return BuildExecution {
|
||||||
|
progress: 0,
|
||||||
|
goal: 0,
|
||||||
|
slide_builder: Box::new(AsciiDoctorSlideBuilder {}),
|
||||||
|
doc_builder: Box::new(AsciiDoctorDocsBuilder {}),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn execute(&mut self) -> Result<(), String> {
|
||||||
|
let path = "./docs/".to_string();
|
||||||
|
if !Self::directory_exists(&path) {
|
||||||
|
return Self::docs_directory_missing();
|
||||||
|
}
|
||||||
|
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
|
||||||
|
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()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn docs_directory_missing() -> Result<(), String> {
|
||||||
|
return Err(
|
||||||
|
"direcotry {path} was not found. The filesystem was maybe updated while build"
|
||||||
|
.to_string(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn build_dir(&mut self, path: &str) -> Result<(), String> {
|
||||||
|
let result = fs_util::fetch_paths_recursive(&path);
|
||||||
|
|
||||||
|
let Ok(paths) = result else {
|
||||||
|
return Err(result.unwrap_err())
|
||||||
|
};
|
||||||
|
|
||||||
|
for (index, path) in paths.iter().enumerate() {
|
||||||
|
self.progress = index + 1;
|
||||||
|
self.goal = paths.len();
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
}
|
||||||
1
src/app/commands/executions/mod.rs
Normal file
1
src/app/commands/executions/mod.rs
Normal file
@@ -0,0 +1 @@
|
|||||||
|
pub mod build_execution;
|
||||||
@@ -5,6 +5,7 @@ use traits::Command;
|
|||||||
use self::build::Build;
|
use self::build::Build;
|
||||||
|
|
||||||
pub mod traits;
|
pub mod traits;
|
||||||
|
pub mod executions;
|
||||||
mod build;
|
mod build;
|
||||||
|
|
||||||
pub struct CommandRegistry {
|
pub struct CommandRegistry {
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ impl App {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn start(&self, args: Vec<String>) {
|
pub fn start(self, args: Vec<String>) {
|
||||||
let command_args = &args[1..];
|
let command_args = &args[1..];
|
||||||
let mut path = String::from("");
|
let mut path = String::from("");
|
||||||
let mut argument_map = HashMap::new();
|
let mut argument_map = HashMap::new();
|
||||||
@@ -45,7 +45,7 @@ impl App {
|
|||||||
self.execute_path(&path, &argument_map);
|
self.execute_path(&path, &argument_map);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn execute_path(&self, path: &String, args: &HashMap<String, String>) {
|
fn execute_path(self, path: &String, args: &HashMap<String, String>) {
|
||||||
let command = self.command_regisrty.command_by(path);
|
let command = self.command_regisrty.command_by(path);
|
||||||
|
|
||||||
if let Some(c) = command {
|
if let Some(c) = command {
|
||||||
@@ -60,7 +60,7 @@ impl App {
|
|||||||
|
|
||||||
match result {
|
match result {
|
||||||
Ok(_) => println!("successfully executed"),
|
Ok(_) => println!("successfully executed"),
|
||||||
Err(message) => println!("{message}"),
|
Err(message) => println!("{message}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user