2023-02-14 17:23:22 +01:00
|
|
|
use crate::common::{repository, utils, config::{ModToml, Module}};
|
2023-01-20 16:43:17 +01:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
2023-02-14 17:23:22 +01:00
|
|
|
pub struct NamedModule(pub String, pub Module);
|
|
|
|
|
|
|
|
impl From<&ModToml> for Vec<NamedModule> {
|
|
|
|
fn from(config: &ModToml) -> Self {
|
|
|
|
config.modules.iter()
|
|
|
|
.map(|v| NamedModule(v.0.clone(), v.1.clone()))
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-20 16:43:17 +01:00
|
|
|
impl ToString for NamedModule {
|
|
|
|
fn to_string(&self) -> String {
|
|
|
|
self.0.clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-30 17:31:31 +01:00
|
|
|
fn install_mod(m: &NamedModule) {
|
2023-01-25 16:06:29 +01:00
|
|
|
println!("Installing module {}...", m.0);
|
2023-01-30 17:31:31 +01:00
|
|
|
'content_iter: for entry in &m.1.content {
|
2023-01-20 17:21:47 +01:00
|
|
|
let src = std::path::Path::new("repo").join(&entry.0);
|
2023-01-20 16:43:17 +01:00
|
|
|
if src.is_file() {
|
|
|
|
if let Err(err) = std::fs::copy(src, entry.1) {
|
|
|
|
println!("Failed to copy {}:\n{}", entry.0, err);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let dst = std::path::Path::new(entry.1.as_str());
|
|
|
|
|
|
|
|
if let Err(err) = std::fs::create_dir_all(dst) {
|
|
|
|
println!("Failed to create directory {}:\n{}", entry.1, err);
|
|
|
|
continue 'content_iter;
|
|
|
|
}
|
|
|
|
|
2023-01-25 16:06:29 +01:00
|
|
|
let pb = indicatif::ProgressBar::new(0);
|
|
|
|
pb.set_style(indicatif::ProgressStyle::with_template("{spinner} [{wide_bar}] {pos:>6}/{len:6}").unwrap().progress_chars("#>-"));
|
2023-01-20 16:43:17 +01:00
|
|
|
|
2023-02-14 17:23:22 +01:00
|
|
|
if let Err(err) = utils::remove_dir(dst, &m.1.ignore, &pb) {
|
2023-01-25 16:06:29 +01:00
|
|
|
println!("Failed to delete directory:\n{}", err);
|
|
|
|
continue 'content_iter;
|
2023-01-20 16:43:17 +01:00
|
|
|
}
|
|
|
|
|
2023-02-14 17:23:22 +01:00
|
|
|
if let Err(err) = utils::copy_dir(&src, dst, &[], &pb) {
|
2023-01-20 16:43:17 +01:00
|
|
|
println!("Failed to copy directory:\n{}", err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-30 17:31:31 +01:00
|
|
|
fn collect_mod(m: &NamedModule) {
|
2023-01-25 16:06:29 +01:00
|
|
|
println!("Collecting module {}...", m.0);
|
2023-01-30 17:31:31 +01:00
|
|
|
'content_iter: for entry in &m.1.content {
|
2023-01-20 17:21:47 +01:00
|
|
|
let dst = std::path::Path::new("repo").join(entry.0.as_str());
|
2023-01-20 16:43:17 +01:00
|
|
|
let src = std::path::Path::new(entry.1.as_str());
|
|
|
|
if src.is_file() {
|
|
|
|
if let Err(err) = std::fs::copy(src, dst) {
|
|
|
|
println!("Failed to copy {}:\n{}", entry.1, err);
|
|
|
|
}
|
|
|
|
} else {
|
2023-01-25 16:06:29 +01:00
|
|
|
let pb = indicatif::ProgressBar::new(0);
|
|
|
|
pb.set_style(indicatif::ProgressStyle::with_template("{spinner} [{wide_bar}] {pos:>6}/{len:6}").unwrap().progress_chars("#>-"));
|
|
|
|
|
2023-02-14 17:23:22 +01:00
|
|
|
if let Err(err) = utils::remove_dir(&dst, &[], &pb) {
|
2023-01-25 16:06:29 +01:00
|
|
|
println!("Failed to delete directory:\n{}", err);
|
2023-01-20 16:43:17 +01:00
|
|
|
continue 'content_iter;
|
|
|
|
}
|
|
|
|
|
2023-01-25 16:06:29 +01:00
|
|
|
if let Err(err) = std::fs::create_dir_all(&dst) {
|
|
|
|
println!("Failed to create directory {}:\n{}", entry.0, err);
|
|
|
|
continue 'content_iter;
|
2023-01-20 16:43:17 +01:00
|
|
|
}
|
|
|
|
|
2023-02-14 17:23:22 +01:00
|
|
|
if let Err(err) = utils::copy_dir(src, &dst, &m.1.ignore, &pb) {
|
2023-01-25 16:06:29 +01:00
|
|
|
println!("Failed to copy source content:\n{}", err);
|
|
|
|
continue 'content_iter;
|
2023-01-20 16:43:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-14 17:23:22 +01:00
|
|
|
pub fn install(mods: &[NamedModule]) {
|
|
|
|
for m in mods {
|
|
|
|
install_mod(m);
|
2023-01-30 17:31:31 +01:00
|
|
|
|
|
|
|
if let Some(on_install) = &m.1.on_install {
|
|
|
|
match std::process::Command::new("sh").arg("-c").arg(on_install).status() {
|
|
|
|
Ok(status) => println!("Install command exited with {status}"),
|
|
|
|
Err(err) => println!("Failed to execute on install command '{on_install}' with error:\n{err}")
|
|
|
|
}
|
|
|
|
}
|
2023-01-20 16:43:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-14 17:23:22 +01:00
|
|
|
pub fn collect(mods: &[NamedModule]) {
|
|
|
|
for m in mods {
|
|
|
|
collect_mod(m);
|
2023-01-20 16:43:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-14 17:23:22 +01:00
|
|
|
pub fn upload<F: FnOnce() -> String>(get_commit_msg: F) {
|
|
|
|
if !repository::is_clean() {
|
|
|
|
let msg = get_commit_msg();
|
|
|
|
if !repository::commit_changes(&msg) {
|
2023-01-25 12:39:31 +01:00
|
|
|
println!("Failed to commit");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2023-02-14 17:23:22 +01:00
|
|
|
if !repository::push_repo() {
|
2023-01-25 12:39:31 +01:00
|
|
|
println!("Failed to push to origin");
|
2023-01-20 16:43:17 +01:00
|
|
|
}
|
|
|
|
}
|