From 90011992c1913d71c8cffdf28a6e7c4d92f405ee Mon Sep 17 00:00:00 2001 From: jan Michal Date: Tue, 8 Feb 2022 14:31:25 +0000 Subject: [PATCH] starting to compartmentalise --- .gitignore | 1 - src/main.rs | 197 ++------------------------------------- src/operations/build.rs | 69 ++++++++++++++ src/operations/config.rs | 12 +++ src/operations/init.rs | 82 ++++++++++++++++ src/operations/mod.rs | 26 ++++++ src/operations/pull.rs | 49 ++++++++++ 7 files changed, 245 insertions(+), 191 deletions(-) create mode 100644 src/operations/build.rs create mode 100644 src/operations/config.rs create mode 100644 src/operations/init.rs create mode 100644 src/operations/mod.rs create mode 100644 src/operations/pull.rs diff --git a/.gitignore b/.gitignore index 2b3d2b0..8c0d7bb 100755 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,4 @@ .idea Cargo.lock /pkg -/src *tar* \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index abf79eb..9a60750 100755 --- a/src/main.rs +++ b/src/main.rs @@ -14,6 +14,7 @@ static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc; mod internal; mod repository; mod workspace; +mod operations; fn main() { extern "C" { @@ -132,194 +133,19 @@ fn main() { } if let true = matches.is_present("init") { - let config = workspace::read_cfg(); - if config.mode == "workspace" { - for r in config.repo { - info(format!("Cloning (workspace mode): {}", r)); - Command::new("git") - .args(&["clone", &r]) - .spawn() - .unwrap() - .wait() - .unwrap(); - } - } else if config.mode == "repository" { - for r in config.repo { - info(format!("Cloning (repository mode): {}", r)); - Command::new("git") - .args(&["clone", &r]) - .spawn() - .unwrap() - .wait() - .unwrap(); - } - } else { - crash("Invalid mode in mlc.toml".to_string(), 1); - } + operations::init(); } if let true = matches.is_present("reinit") { - let config = workspace::read_cfg(); - let out = Command::new("bash") - .args(&["-c", "ls -A"]) - .output() - .unwrap() - .stdout; - let dirs_to_s = String::from_utf8_lossy(&*out); - let mut dirs = dirs_to_s.lines().collect::>(); - - let name = config.name.unwrap(); - - dirs.retain(|x| *x != "mlc.toml"); - dirs.retain(|x| *x != ".git"); - if config.mode == "repository" { - dirs.retain(|x| *x != "out"); - dirs.retain(|x| *x != name); - } - - info("Removing all repo directories to reinitialise".to_string()); - - Command::new("rm") - .args(&["-rf", &dirs.join(" ")]) - .spawn() - .unwrap() - .wait() - .unwrap(); - - if config.mode == "workspace" { - for r in config.repo { - info(format!("Cloning (workspace mode): {}", r)); - Command::new("git") - .args(&["clone", &r]) - .spawn() - .unwrap() - .wait() - .unwrap(); - } - } else if config.mode == "repository" { - for r in config.repo { - info(format!("Cloning (repository mode): {}", r)); - Command::new("git") - .args(&["clone", &r]) - .spawn() - .unwrap() - .wait() - .unwrap(); - } - } else { - crash("Invalid mode in mlc.toml".to_string(), 1); - } + operations::reinit(); } if let true = matches.is_present("build") { - let config = workspace::read_cfg(); - let mut packages: Vec = matches - .subcommand_matches("build") - .unwrap() - .values_of_lossy("package(s)") - .unwrap_or_default(); - - let exclude: Vec = matches - .subcommand_matches("build") - .unwrap() - .values_of_lossy("exclude") - .unwrap_or_default(); - - for pkg in &exclude { - packages.retain(|x| &*x != pkg); - } - - if config.mode != "repository" { - crash("Cannot build packages in workspace mode".to_string(), 2); - } - - let mut repos: Vec = vec![]; - for r in config.repo { - let split = r.split('/').collect::>(); - let a = split.last().unwrap(); - repos.push(a.parse().unwrap()); - } - - if matches - .subcommand_matches("build") - .unwrap() - .is_present("exclude") - { - for ex in exclude { - repos.retain(|x| *x != ex); - } - } - - for pkg in packages { - if !repos.contains(&pkg) { - crash(format!("Package {} not found in repos in mlc.toml", pkg), 3); - } else { - repository::build(pkg); - } - } - - if matches - .subcommand_matches("build") - .unwrap() - .is_present("all") - { - for pkg in repos { - repository::build(pkg); - } - } - - if matches - .subcommand_matches("build") - .unwrap() - .is_present("regen") - { - repository::generate(); - } + operations::build(&matches); } if let true = matches.is_present("pull") { - let packages: Vec = matches - .subcommand_matches("pull") - .unwrap() - .values_of_lossy("package(s)") - .unwrap_or_default(); - let config = workspace::read_cfg(); - let cdir = env::current_dir().unwrap(); - if packages.is_empty() { - for r in config.repo { - info(format!("Entering working directory: {}", r)); - let dir = format!( - "{}/{}", - env::current_dir().unwrap().display(), - r.split('/').collect::>().last().unwrap() - ); - env::set_current_dir(dir).unwrap(); - Command::new("git") - .args(&["pull", &r]) - .spawn() - .unwrap() - .wait() - .unwrap(); - env::set_current_dir(&cdir).unwrap(); - } - } else { - for r in packages { - info(format!("Entering working directory: {}", r)); - let dir = format!( - "{}/{}", - env::current_dir().unwrap().display(), - r.split('/').collect::>().last().unwrap() - ); - env::set_current_dir(dir).unwrap(); - Command::new("git") - .args(&["pull", &r]) - .spawn() - .unwrap() - .wait() - .unwrap(); - env::set_current_dir(&cdir).unwrap(); - } - } + operations::pull(&matches); } if let true = matches.is_present("repo-gen") { @@ -383,15 +209,6 @@ fn main() { } if let true = matches.is_present("config") { - if !Path::exists("mlc.toml".as_ref()) { - create_config(); - } - let editor = env::var("EDITOR").unwrap_or_else(|_| "nano".to_string()); - Command::new(editor) - .arg("mlc.toml") - .spawn() - .unwrap() - .wait() - .unwrap(); + operations::config(); } -} +} \ No newline at end of file diff --git a/src/operations/build.rs b/src/operations/build.rs new file mode 100644 index 0000000..50e9552 --- /dev/null +++ b/src/operations/build.rs @@ -0,0 +1,69 @@ +use clap::ArgMatches; +use crate::{crash, repository, workspace}; + +pub fn build(matches: &ArgMatches) { + let config = workspace::read_cfg(); + let mut packages: Vec = matches + .subcommand_matches("build") + .unwrap() + .values_of_lossy("package(s)") + .unwrap_or_default(); + + let exclude: Vec = matches + .subcommand_matches("build") + .unwrap() + .values_of_lossy("exclude") + .unwrap_or_default(); + + + for pkg in &exclude { + packages.retain(|x| &*x != pkg); + } + + if config.mode != "repository" { + crash("Cannot build packages in workspace mode".to_string(), 2); + } + + let mut repos: Vec = vec![]; + for r in config.repo { + let split = r.split('/').collect::>(); + let a = split.last().unwrap(); + repos.push(a.parse().unwrap()); + } + + if matches + .subcommand_matches("build") + .unwrap() + .is_present("exclude") + { + for ex in exclude { + repos.retain(|x| *x != ex); + } + } + + for pkg in packages { + if !repos.contains(&pkg) { + crash(format!("Package {} not found in repos in mlc.toml", pkg), 3); + } else { + repository::build(pkg); + } + } + + if matches + .subcommand_matches("build") + .unwrap() + .is_present("all") + { + for pkg in repos { + repository::build(pkg); + } + } + + if matches + .subcommand_matches("build") + .unwrap() + .is_present("regen") + { + repository::generate(); + } +} \ No newline at end of file diff --git a/src/operations/config.rs b/src/operations/config.rs new file mode 100644 index 0000000..b5ad1d2 --- /dev/null +++ b/src/operations/config.rs @@ -0,0 +1,12 @@ +pub fn config() { + if !Path::exists("mlc.toml".as_ref()) { + create_config(); + } + let editor = env::var("EDITOR").unwrap_or_else(|_| "nano".to_string()); + Command::new(editor) + .arg("mlc.toml") + .spawn() + .unwrap() + .wait() + .unwrap(); +} \ No newline at end of file diff --git a/src/operations/init.rs b/src/operations/init.rs new file mode 100644 index 0000000..29d7933 --- /dev/null +++ b/src/operations/init.rs @@ -0,0 +1,82 @@ +use std::process::Command; +use crate::{crash, info, workspace}; + +pub fn reinit() { + let config = workspace::read_cfg(); + let out = Command::new("bash") + .args(&["-c", "ls -A"]) + .output() + .unwrap() + .stdout; + let dirs_to_s = String::from_utf8_lossy(&*out); + let mut dirs = dirs_to_s.lines().collect::>(); + + let name = config.name.unwrap(); + + dirs.retain(|x| *x != "mlc.toml"); + dirs.retain(|x| *x != ".git"); + if config.mode == "repository" { + dirs.retain(|x| *x != "out"); + dirs.retain(|x| *x != name); + } + + info("Removing all repo directories to reinitialise".to_string()); + + Command::new("rm") + .args(&["-rf", &dirs.join(" ")]) + .spawn() + .unwrap() + .wait() + .unwrap(); + + if config.mode == "workspace" { + for r in config.repo { + info(format!("Cloning (workspace mode): {}", r)); + Command::new("git") + .args(&["clone", &r]) + .spawn() + .unwrap() + .wait() + .unwrap(); + } + } else if config.mode == "repository" { + for r in config.repo { + info(format!("Cloning (repository mode): {}", r)); + Command::new("git") + .args(&["clone", &r]) + .spawn() + .unwrap() + .wait() + .unwrap(); + } + } else { + crash("Invalid mode in mlc.toml".to_string(), 1); + } +} + +pub fn init() { + let config = workspace::read_cfg(); + if config.mode == "workspace" { + for r in config.repo { + info(format!("Cloning (workspace mode): {}", r)); + Command::new("git") + .args(&["clone", &r]) + .spawn() + .unwrap() + .wait() + .unwrap(); + } + } else if config.mode == "repository" { + for r in config.repo { + info(format!("Cloning (repository mode): {}", r)); + Command::new("git") + .args(&["clone", &r]) + .spawn() + .unwrap() + .wait() + .unwrap(); + } + } else { + crash("Invalid mode in mlc.toml".to_string(), 1); + } +} \ No newline at end of file diff --git a/src/operations/mod.rs b/src/operations/mod.rs new file mode 100644 index 0000000..442b40c --- /dev/null +++ b/src/operations/mod.rs @@ -0,0 +1,26 @@ +use clap::ArgMatches; + +mod init; +mod build; +mod pull; +mod config; + +pub fn reinit() { + init::reinit(); +} + +pub fn init() { + init::init(); +} + +pub fn build(matches: &ArgMatches) { + build::build(matches); +} + +pub fn pull(matches: &ArgMatches) { + pull::pull(matches); +} + +pub fn config() { + config::config(); +} \ No newline at end of file diff --git a/src/operations/pull.rs b/src/operations/pull.rs new file mode 100644 index 0000000..78769e7 --- /dev/null +++ b/src/operations/pull.rs @@ -0,0 +1,49 @@ +use std::env; +use std::process::Command; +use clap::ArgMatches; +use crate::{info, workspace}; + +pub fn pull(matches: &ArgMatches) { + let packages: Vec = matches + .subcommand_matches("pull") + .unwrap() + .values_of_lossy("package(s)") + .unwrap_or_default(); + let config = workspace::read_cfg(); + let cdir = env::current_dir().unwrap(); + if packages.is_empty() { + for r in config.repo { + info(format!("Entering working directory: {}", r)); + let dir = format!( + "{}/{}", + env::current_dir().unwrap().display(), + r.split('/').collect::>().last().unwrap() + ); + env::set_current_dir(dir).unwrap(); + Command::new("git") + .args(&["pull", &r]) + .spawn() + .unwrap() + .wait() + .unwrap(); + env::set_current_dir(&cdir).unwrap(); + } + } else { + for r in packages { + info(format!("Entering working directory: {}", r)); + let dir = format!( + "{}/{}", + env::current_dir().unwrap().display(), + r.split('/').collect::>().last().unwrap() + ); + env::set_current_dir(dir).unwrap(); + Command::new("git") + .args(&["pull", &r]) + .spawn() + .unwrap() + .wait() + .unwrap(); + env::set_current_dir(&cdir).unwrap(); + } + } +} \ No newline at end of file