initial commit (proof of concept build function, added cli structure

This commit is contained in:
2023-01-19 17:12:48 +01:00
commit b38bb186b1
12 changed files with 661 additions and 0 deletions

67
src/app/commands/build.rs Normal file
View File

@@ -0,0 +1,67 @@
use std::{collections::HashMap, env, fs, process};
use super::traits::Command;
pub struct Build;
impl Build {
fn build_dir(&self, path: String) -> Result<(), String> {
let Ok(dirs) = fs::read_dir(path) else {
return 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());
};
if entry.path().is_dir() {
self.build_dir(entry.path().to_str().unwrap().to_string());
} else {
self.build_file(entry.path().to_str().unwrap().to_string());
}
}
return Err("No files to build".to_string());
}
fn build_file(&self, path: String) -> 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(());
}
}
impl Command for Build {
fn execute(&self, _args: &HashMap<String, String>) -> Result<(), String> {
let Ok(project_cwd_object) = env::current_dir() else {
return Err("current dirctory does not seem to exist".to_string())
};
let Some(project_cwd) = project_cwd_object.to_str() else {
return Err("invalid path".to_string());
};
let path = format!("{project_cwd}/docs/");
return self.build_dir(path);
}
fn new() -> Self
where
Self: Sized,
{
return Build {};
}
}

40
src/app/commands/mod.rs Normal file
View File

@@ -0,0 +1,40 @@
use std::collections::HashMap;
use traits::Command;
use self::build::Build;
pub mod traits;
mod build;
pub struct CommandRegistry {
commands: HashMap<String, Box<dyn Command>>
}
impl CommandRegistry {
pub fn register_all(&mut self) {
let registry = self;
registry.register("/build".to_string(), Box::new(Build::new()), true)
}
pub fn register(&mut self, path: String, command: Box<dyn Command>, enabled: bool) {
if enabled {
self.commands.insert(path, command);
}
}
pub fn new() -> CommandRegistry {
let mut registry = CommandRegistry { commands: HashMap::new() };
registry.register_all();
registry
}
pub fn command_by(&self, path: &String) -> Option<&Box<dyn Command>> {
let command = self.commands.get(path);
return command;
}
}

View File

@@ -0,0 +1,9 @@
use std::collections::HashMap;
pub trait Command {
fn execute(&self, args: &HashMap<String, String>) -> Result<(), String>;
fn new() -> Self where Self: Sized;
}