Skip to content

Commit

Permalink
[wip][bndbuild] Add command
Browse files Browse the repository at this point in the history
  • Loading branch information
Krusty/Benediction committed Aug 1, 2024
1 parent 9cdbbf8 commit bfbcbcd
Show file tree
Hide file tree
Showing 14 changed files with 262 additions and 122 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cpclib-bndbuild/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ build-deps = "0.1.4"
[dev-dependencies]
test-generator = "0.3.1"
serial_test = "3.1.1"
tempfile = {workspace = true}

[[bin]]
name = "bndbuild"
Expand Down
25 changes: 13 additions & 12 deletions cpclib-bndbuild/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use cpclib_common::lazy_static::lazy_static;

use crate::runners::basm::BasmRunner;
use crate::runners::bndbuild::BndBuildRunner;
use crate::runners::cp::CpRunner;
use crate::runners::disc::DiscManagerRunner;
use crate::runners::echo::EchoRunner;
use crate::runners::imgconverter::ImgConverterRunner;
Expand All @@ -16,6 +17,7 @@ use crate::task::Task;
lazy_static! {
pub static ref BASM_RUNNER: BasmRunner = BasmRunner::default();
pub static ref BNDBUILD_RUNNER: BndBuildRunner = BndBuildRunner::default();
pub static ref CP_RUNNER: CpRunner = CpRunner::default();
pub static ref DISC_RUNNER: DiscManagerRunner = DiscManagerRunner::default();
pub static ref ECHO_RUNNER: EchoRunner = EchoRunner::default();
pub static ref EXTERN_RUNNER: ExternRunner = ExternRunner::default();
Expand All @@ -25,18 +27,17 @@ lazy_static! {
}

pub fn execute(task: &Task) -> Result<(), String> {
let (runner, args) = match task {
Task::Basm(_) => (BASM_RUNNER.deref() as &dyn Runner, task.args()),
Task::BndBuild(_) => (BNDBUILD_RUNNER.deref() as &dyn Runner, task.args()),
Task::Disc(_) => (DISC_RUNNER.deref() as &dyn Runner, task.args()),
Task::Echo(_) => (ECHO_RUNNER.deref() as &dyn Runner, task.args()),
Task::Extern(_) => (EXTERN_RUNNER.deref() as &dyn Runner, task.args()),
Task::ImgConverter(_) => (IMGCONV_RUNNER.deref() as &dyn Runner, task.args()),
Task::Rm(_) => (RM_RUNNER.deref() as &dyn Runner, task.args()),
Task::Xfer(_) => (XFER_RUNNER.deref() as &dyn Runner, task.args())
};

runner.run(args).or_else(|e| {
match task {
Task::Basm(_) => BASM_RUNNER.run(task.args()),
Task::BndBuild(_) => BNDBUILD_RUNNER.run(task.args()),
Task::Cp(_) => CP_RUNNER.run(task.args()),
Task::Disc(_) => DISC_RUNNER.run(task.args()),
Task::Echo(_) => ECHO_RUNNER.run(task.args()),
Task::Extern(_) => EXTERN_RUNNER.run(task.args()),
Task::ImgConverter(_) => IMGCONV_RUNNER.run(task.args()),
Task::Rm(_) => RM_RUNNER.run(task.args()),
Task::Xfer(_) => XFER_RUNNER.run(task.args()),
}.or_else(|e| {
if task.ignore_errors() {
println!("\t\tError ignored. {}", e);
Ok(())
Expand Down
6 changes: 3 additions & 3 deletions cpclib-bndbuild/src/runners/basm.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use cpclib_common::clap::{self, Arg, ArgAction, Command};

use super::{Runner, RunnerWithClap};
use crate::built_info;
use crate::{built_info, task::BASM_CMDS};

pub struct BasmRunner {
command: clap::Command
Expand Down Expand Up @@ -53,7 +53,7 @@ impl RunnerWithClap for BasmRunner {
}

impl Runner for BasmRunner {
fn inner_run(&self, itr: &[String]) -> Result<(), String> {
fn inner_run<S: AsRef<str>>(&self, itr: &[S]) -> Result<(), String> {
let matches = self.get_matches(itr)?;

if matches.get_flag("version") {
Expand All @@ -79,6 +79,6 @@ impl Runner for BasmRunner {
}

fn get_command(&self) -> &str {
"basm"
&BASM_CMDS[0]
}
}
6 changes: 3 additions & 3 deletions cpclib-bndbuild/src/runners/bndbuild.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use cpclib_common::clap::{self, Command};

use super::{Runner, RunnerWithClap};
use crate::built_info;
use crate::{built_info, task::BNDBUILD_CMDS};

pub struct BndBuildRunner {
command: clap::Command
Expand All @@ -28,7 +28,7 @@ impl RunnerWithClap for BndBuildRunner {
}

impl Runner for BndBuildRunner {
fn inner_run(&self, itr: &[String]) -> Result<(), String> {
fn inner_run<S: AsRef<str>>(&self, itr: &[S]) -> Result<(), String> {
// backup of cwd
let cwd = std::env::current_dir().unwrap();

Expand All @@ -43,6 +43,6 @@ impl Runner for BndBuildRunner {
}

fn get_command(&self) -> &str {
"bndbuild"
&BNDBUILD_CMDS[0]
}
}
128 changes: 128 additions & 0 deletions cpclib-bndbuild/src/runners/cp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
use std::path::Path;

use cpclib_common::{clap::{self, Arg, ArgAction}, itertools::Itertools};

use super::Runner;
use crate::{built_info, expand_glob, task::CP_CMDS};

#[derive(Default)]
pub struct CpRunner {}

impl CpRunner {
pub fn print_help(&self) {
clap::Command::new("cp")
.before_help("Copy files.")
.disable_help_flag(true)
.after_help(format!(
"Inner command of {} {}",
built_info::PKG_NAME,
built_info::PKG_VERSION
))
.arg(
Arg::new("arguments")
.action(ArgAction::Append)
.help("Files to copy. Last one being the destination")
)
.print_long_help()
.unwrap();
}
}

impl Runner for CpRunner {
fn inner_run<S: AsRef<str>>(&self, itr: &[S]) -> Result<(), String> {
let mut errors = String::new();

let fnames = itr
.iter()
.map(|s| s.as_ref())
.map(expand_glob)
.flatten()
.collect_vec();
let files = fnames.iter()
.map( |fname| Path::new(fname))
.collect_vec();
let dest = files.last();

let copy = |from: &Path, to: &Path, error: &mut String| {
std::fs::copy(from, to)
.map_err(|e| error.push_str(&format!("Error when copying {} to {}. {}.\n", from.display(), to.display(), e.to_string())));
};

match files.len() {
0 => {
errors.push_str("No source and destination provided\n");
},

1 => {
errors.push_str("No source or destination provided\n");
},

2 => {
let dest = dest.unwrap();
let src = files.first().unwrap();
let dest = if dest.is_dir() {
dest.join(src.file_name().unwrap())
} else {
dest.to_path_buf()
};
copy(src, &dest, &mut errors);
},

_ => {
let dest = dest.unwrap();
if ! dest.is_dir() {
errors.push_str(&format!("{} must be a directory.", dest.display()))
} else {
let files = &files[..files.len()-1];
for src in files.iter() {
let dst = dest.join(src.file_name().unwrap());
copy(src, &dst, &mut errors);
}
}
}
};


if errors.is_empty() {
Ok(())
}
else {
Err(errors)
}
}

fn get_command(&self) -> &'static str {
&CP_CMDS[0]
}
}


#[cfg(test)]
mod test {
use std::io::Write;

use crate::runners::{cp::CpRunner, Runner};

#[test]

fn test_copy_successful() {
// prepare the files for the test
let mut src = tempfile::NamedTempFile::new().unwrap();
let mut dst = tempfile::NamedTempFile::new().unwrap();

src.as_file_mut().write("test".as_bytes()).unwrap();

let src = src.into_temp_path();
let dst = dst.into_temp_path();
std::fs::remove_file(&dst).unwrap();

assert!(src.exists());
assert!(!dst.exists());

// Run the test
let cp = CpRunner::default();
cp.inner_run(&[src.display().to_string(), dst.display().to_string()]).unwrap();
assert!(dst.exists());

}
}
6 changes: 3 additions & 3 deletions cpclib-bndbuild/src/runners/disc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use cpclib_common::clap::{self, Command};
use cpclib_disc::dsk_manager_build_arg_parser;

use super::{Runner, RunnerWithClap};
use crate::built_info;
use crate::{built_info, task::DISC_CMDS};

pub struct DiscManagerRunner {
command: clap::Command
Expand All @@ -29,12 +29,12 @@ impl RunnerWithClap for DiscManagerRunner {
}

impl Runner for DiscManagerRunner {
fn inner_run(&self, itr: &[String]) -> Result<(), String> {
fn inner_run<S: AsRef<str>>(&self, itr: &[S]) -> Result<(), String> {
let matches = self.get_matches(itr)?;
cpclib_disc::dsk_manager_handle(&matches).map_err(|e| e.to_string())
}

fn get_command(&self) -> &str {
"disc"
&DISC_CMDS[0]
}
}
8 changes: 5 additions & 3 deletions cpclib-bndbuild/src/runners/echo.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
use cpclib_common::itertools::Itertools;

use crate::task::ECHO_CMDS;

use super::Runner;

#[derive(Default)]
pub struct EchoRunner {}

impl Runner for EchoRunner {
fn inner_run(&self, itr: &[String]) -> Result<(), String> {
let txt = itr.iter().join(" ");
fn inner_run<S: AsRef<str>>(&self, itr: &[S]) -> Result<(), String> {
let txt = itr.iter().map(|s| s.as_ref()).join(" ");
println!("{txt}");
Ok(())
}

fn get_command(&self) -> &str {
"echo"
&ECHO_CMDS[0]
}
}
12 changes: 10 additions & 2 deletions cpclib-bndbuild/src/runners/extern.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
use std::fmt::Debug;

use cpclib_common::itertools::Itertools;

use crate::task::EXTERN_CMDS;

use super::Runner;

#[derive(Default)]
pub struct ExternRunner {}
impl ExternRunner {}
impl Runner for ExternRunner {
fn inner_run(&self, itr: &[String]) -> Result<(), String> {
fn inner_run<S: AsRef<str>>(&self, itr: &[S]) -> Result<(), String> {
let itr = itr.iter().map(|s| s.as_ref()).collect_vec();

// WARNING
// Deactivated because if makes fail normal progam on Linux
// however, it was maybe mandatory for Windows
Expand Down Expand Up @@ -45,6 +53,6 @@ impl Runner for ExternRunner {
}

fn get_command(&self) -> &str {
"extern"
&EXTERN_CMDS[0]
}
}
6 changes: 3 additions & 3 deletions cpclib-bndbuild/src/runners/imgconverter.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use cpclib_common::clap::{Arg, ArgAction, Command};

use super::{Runner, RunnerWithClap};
use crate::built_info;
use crate::{built_info, task::IMG2CPC_CMDS};

pub struct ImgConverterRunner {
command: Command
Expand Down Expand Up @@ -39,7 +39,7 @@ impl RunnerWithClap for ImgConverterRunner {
}

impl Runner for ImgConverterRunner {
fn inner_run(&self, itr: &[String]) -> Result<(), String> {
fn inner_run<S: AsRef<str>>(&self, itr: &[S]) -> Result<(), String> {
let args = self.get_clap_command().clone();

let matches = self.get_matches(itr)?;
Expand All @@ -52,6 +52,6 @@ impl Runner for ImgConverterRunner {
}

fn get_command(&self) -> &str {
"img2cpc"
&IMG2CPC_CMDS[0]
}
}
9 changes: 6 additions & 3 deletions cpclib-bndbuild/src/runners/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use std::fmt::Debug;

use cpclib_common::clap::{ArgMatches, Command};
use glob::glob;
use shlex::split;

pub mod basm;
pub mod bndbuild;
pub mod cp;
pub mod disc;
pub mod echo;
pub mod r#extern;
Expand Down Expand Up @@ -45,18 +48,18 @@ pub trait Runner {
}

/// Implement the command specific action
fn inner_run(&self, itr: &[String]) -> Result<(), String>;
fn inner_run<S: AsRef<str>>(&self, itr: &[S]) -> Result<(), String>;

fn get_command(&self) -> &str;
}

pub trait RunnerWithClap: Runner {
fn get_clap_command(&self) -> &Command;

fn get_matches(&self, itr: &[String]) -> Result<ArgMatches, String> {
fn get_matches<S: AsRef<str>>(&self, itr: &[S]) -> Result<ArgMatches, String> {
self.get_clap_command()
.clone()
.try_get_matches_from(itr)
.try_get_matches_from(itr.iter().map(|s| s.as_ref()))
.map_err(|e| e.to_string())
}

Expand Down
4 changes: 2 additions & 2 deletions cpclib-bndbuild/src/runners/rm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ impl RmRunner {
}
}
impl Runner for RmRunner {
fn inner_run(&self, itr: &[String]) -> Result<(), String> {
fn inner_run<S: AsRef<str>>(&self, itr: &[S]) -> Result<(), String> {
let mut errors = String::new();

for fname in itr
.into_iter()
.map(|s| s.as_str())
.map(|s| s.as_ref())
// .map(|s| glob(s).unwrap())
.map(expand_glob)
.flatten()
Expand Down
Loading

0 comments on commit bfbcbcd

Please sign in to comment.