initial commit (proof of concept build function, added cli structure
This commit is contained in:
67
src/app/commands/build.rs
Normal file
67
src/app/commands/build.rs
Normal 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
40
src/app/commands/mod.rs
Normal 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;
|
||||
}
|
||||
}
|
||||
|
||||
9
src/app/commands/traits.rs
Normal file
9
src/app/commands/traits.rs
Normal 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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
66
src/app/mod.rs
Normal file
66
src/app/mod.rs
Normal file
@@ -0,0 +1,66 @@
|
||||
mod commands;
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use commands::traits::Command;
|
||||
use commands::CommandRegistry;
|
||||
|
||||
pub struct App {
|
||||
command_regisrty: CommandRegistry,
|
||||
}
|
||||
|
||||
impl App {
|
||||
pub fn new() -> App {
|
||||
return App {
|
||||
command_regisrty: CommandRegistry::new()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn start(&self, args: Vec<String>) {
|
||||
let command_args = &args[1..];
|
||||
let mut path = String::from("");
|
||||
let mut argument_map = HashMap::new();
|
||||
let mut only_options_left = false;
|
||||
|
||||
for (index, argument) in command_args.iter().enumerate() {
|
||||
if argument.starts_with("--") {
|
||||
only_options_left = true;
|
||||
let value = command_args.get(index + 1);
|
||||
if let Some(v) = value {
|
||||
if v.starts_with("--") {
|
||||
argument_map.insert(argument.replace("--", ""), String::from(""));
|
||||
} else {
|
||||
argument_map.insert(argument.replace("--", ""), String::from(v));
|
||||
}
|
||||
} else {
|
||||
argument_map.insert(argument.replace("--", ""), String::from(""));
|
||||
}
|
||||
} else if !only_options_left {
|
||||
path.push_str(&format!("/{argument}"))
|
||||
}
|
||||
}
|
||||
|
||||
self.execute_path(&path, &argument_map);
|
||||
}
|
||||
|
||||
fn execute_path(&self, path: &String, args: &HashMap<String, String>) {
|
||||
let command = self.command_regisrty.command_by(path);
|
||||
|
||||
if let Some(c) = command {
|
||||
self.execute_command(c, args)
|
||||
} else {
|
||||
println!("command not found")
|
||||
}
|
||||
}
|
||||
|
||||
fn execute_command(&self, c: &Box<dyn Command>, args: &HashMap<String, String>) {
|
||||
let result = c.execute(args);
|
||||
|
||||
match result {
|
||||
Ok(_) => println!("successfully executed"),
|
||||
Err(message) => println!("{message}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user