2023-01-20 16:43:17 +01:00
|
|
|
use crate::config::{ModToml, Module};
|
|
|
|
use crate::prompt::multi_select;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct NamedModule(String, Module);
|
|
|
|
impl ToString for NamedModule {
|
|
|
|
fn to_string(&self) -> String {
|
|
|
|
self.0.clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn install_mod(m: NamedModule) {
|
2023-01-25 16:06:29 +01:00
|
|
|
println!("Installing module {}...", m.0);
|
2023-01-20 16:43:17 +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-01-25 16:06:29 +01:00
|
|
|
if let Err(err) = crate::utils::remove_dir(dst, &m.1.ignore, &pb) {
|
|
|
|
println!("Failed to delete directory:\n{}", err);
|
|
|
|
continue 'content_iter;
|
2023-01-20 16:43:17 +01:00
|
|
|
}
|
|
|
|
|
2023-01-25 16:06:29 +01:00
|
|
|
if let Err(err) = crate::utils::copy_dir(&src, dst, &[], &pb) {
|
2023-01-20 16:43:17 +01:00
|
|
|
println!("Failed to copy directory:\n{}", err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn collect_mod(m: NamedModule) {
|
2023-01-25 16:06:29 +01:00
|
|
|
println!("Collecting module {}...", m.0);
|
2023-01-20 16:43:17 +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("#>-"));
|
|
|
|
|
|
|
|
if let Err(err) = crate::utils::remove_dir(&dst, &[], &pb) {
|
|
|
|
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-01-25 16:06:29 +01:00
|
|
|
if let Err(err) = crate::utils::copy_dir(src, &dst, &m.1.ignore, &pb) {
|
|
|
|
println!("Failed to copy source content:\n{}", err);
|
|
|
|
continue 'content_iter;
|
2023-01-20 16:43:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn install(config: &ModToml) {
|
|
|
|
let to_install = multi_select(
|
|
|
|
Some("Which modules do you want to install?"),
|
|
|
|
config.modules.iter().map(|v| NamedModule(v.0.clone(), v.1.clone())).collect()
|
|
|
|
);
|
|
|
|
|
|
|
|
for m in to_install {
|
|
|
|
install_mod(m);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn collect(config: &ModToml) {
|
|
|
|
let to_collect = multi_select(
|
|
|
|
Some("Which modules do you want to collect?"),
|
|
|
|
config.modules.iter().map(|v| NamedModule(v.0.clone(), v.1.clone())).collect()
|
|
|
|
);
|
|
|
|
|
|
|
|
for m in to_collect {
|
|
|
|
collect_mod(m);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-25 12:39:31 +01:00
|
|
|
pub fn upload() {
|
|
|
|
if !crate::repository::is_clean() {
|
2023-01-20 16:43:17 +01:00
|
|
|
let msg = crate::prompt::input("Commit message");
|
2023-01-25 12:39:31 +01:00
|
|
|
if !crate::repository::commit_changes(&msg) {
|
|
|
|
println!("Failed to commit");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !crate::repository::push_repo() {
|
|
|
|
println!("Failed to push to origin");
|
2023-01-20 16:43:17 +01:00
|
|
|
}
|
|
|
|
}
|