From 099cc8e218c1cb7a6032a934562e90e74985fc1e Mon Sep 17 00:00:00 2001 From: Henrique Alves Date: Tue, 26 Jul 2022 14:19:29 -0300 Subject: Add default value for target and source folders --- Cargo.toml | 6 ++- src/commands.rs | 115 +++++++++++++++++++++++++++++++++++++++++--------------- 2 files changed, 89 insertions(+), 32 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d6a48e0..31a08c8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "godot-glam" -version = "0.1.0" +version = "0.1.1" edition = "2021" authors = ["Henrique Alves "] license = "MIT" @@ -10,7 +10,9 @@ repository = "https://github.com/henriquelalves/glam" keywords = ["cli", "godot", "addon", "manager"] categories = ["command-line-utilities"] -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[[bin]] +name = "glam" +path = "src/main.rs" [dependencies] clap = { version = "3.0", features = ["derive"] } diff --git a/src/commands.rs b/src/commands.rs index 7a09a8d..bb883d4 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -17,7 +17,16 @@ struct GlamObject { struct GlamPackage { name : String, git_repo : String, + #[serde(default = "default_string")] commit : String, + #[serde(default = "default_string")] + target_folder : String, + #[serde(default = "default_string")] + source_folder : String, +} + +fn default_string() -> String { + return "".to_string(); } pub fn initialize(root : &str) { @@ -60,14 +69,19 @@ pub fn install_all_packages(root : &str, verbose : bool) { let glam_file_path = format!("{}/.glam", root); // Find glam object or create one with default configuration - let glam_object = read_glam_file(&glam_file_path); + let mut glam_object = read_glam_file(&glam_file_path); let mut glam_packages = glam_object.packages; for package in glam_packages.iter_mut() { + utils::log_info(&format!("Installing {}...", package.name)); clone_or_fetch_package(root, &package, verbose); let commit = package.commit.to_string(); install_glam_package(root, &commit, package, false, true, verbose); } + + // Write .glam file + glam_object.packages = glam_packages; + write_glam_file(&glam_file_path, &glam_object); } pub fn install_package(root : &str, git_repo : &str, commit : &str, copy_files : bool, verbose : bool) { @@ -79,16 +93,31 @@ pub fn install_package(root : &str, git_repo : &str, commit : &str, copy_files : let mut glam_packages = glam_object.packages; let name = utils::get_repo_name(git_repo); - let (_, target_package) = find_package( - &mut glam_packages, - &name, - Some(GlamPackage{ - git_repo : git_repo.to_string(), - name : name.to_string(), - commit : "".to_string(), - })); + let source_folder = format!("addons/{}", name); + let target_folder = format!("addons/{}", name); + + let mut package_index = find_package(&glam_packages, &name); + match package_index { + None => { + glam_packages.push(GlamPackage { + name, + git_repo : git_repo.to_string(), + commit: "".to_string(), + target_folder, + source_folder + }); + + package_index = Some(glam_packages.len() - 1); + } - clone_or_fetch_package(root, &target_package, verbose); + _ => {} + } + + let package_index = package_index.unwrap(); + let target_package = &mut glam_packages[package_index]; + + + clone_or_fetch_package(root, target_package, verbose); // Update package folder to commit hash install_glam_package(root, commit, target_package, false, copy_files, verbose); @@ -106,6 +135,7 @@ pub fn update_all_packages(root : &str, verbose : bool) { let mut glam_packages = glam_object.packages; for package in glam_packages.iter_mut() { + utils::log_info(&format!("Updating {}...", package.name)); clone_or_fetch_package(root, &package, verbose); install_glam_package(root, "", package, true, true, verbose); } @@ -122,10 +152,18 @@ pub fn update_package(root : &str, package_name : &str, verbose : bool) { let mut glam_object = read_glam_file(&glam_file_path); let mut glam_packages = glam_object.packages; - let (_, mut target_package) = find_package(&mut glam_packages, package_name, None); + let package_index = find_package(&glam_packages, package_name); + + if package_index.is_none() { + utils::log_error("Package not found!"); + exit(1); + } + + let package_index = package_index.unwrap(); + let target_package = &mut glam_packages[package_index]; clone_or_fetch_package(root, &target_package, verbose); - install_glam_package(root, "", &mut target_package, true, true, verbose); + install_glam_package(root, "", target_package, true, true, verbose); // Write .glam file glam_object.packages = glam_packages; @@ -139,11 +177,20 @@ pub fn remove_package(root : &str, package_name : &str, verbose : bool) { let mut glam_object = read_glam_file(&glam_file_path); let mut glam_packages = glam_object.packages; - let (index, target_package) = find_package(&mut glam_packages, package_name, None); - remove_glam_package_files(root, &target_package, verbose); + let package_index = find_package(&glam_packages, package_name); + + if package_index.is_none() { + utils::log_error("Package not found!"); + exit(1); + } + + let package_index = package_index.unwrap(); + let target_package = &mut glam_packages[package_index]; - glam_packages.remove(index); + remove_glam_package_files(root, target_package, verbose); + + glam_packages.remove(package_index); // Write .glam file glam_object.packages = glam_packages; @@ -157,12 +204,21 @@ pub fn apply_changes(root : &str, package_name : &str, verbose : bool) { let glam_object = read_glam_file(&glam_file_path); let mut glam_packages = glam_object.packages; - let (_, target_package) = find_package(&mut glam_packages, package_name, None); - apply_glam_package_files(root, &target_package, verbose); + let package_index = find_package(&glam_packages, package_name); + + if package_index.is_none() { + utils::log_error("Package not found!"); + exit(1); + } + + let package_index = package_index.unwrap(); + let target_package = &mut glam_packages[package_index]; + + apply_glam_package_files(root, target_package, verbose); } -fn find_package<'a>(packages : &'a mut Vec, name : &str, new_package : Option) -> (usize, &'a mut GlamPackage) { +fn find_package(packages : &Vec, name : &str) -> Option { let mut package_index = 0; let mut found_package = false; @@ -173,20 +229,11 @@ fn find_package<'a>(packages : &'a mut Vec, name : &str, new_packag } } - if !found_package { - match new_package { - None => { - utils::log_error("Package not found!"); - exit(1); - }, - Some(package) => { - packages.push(package); - package_index = packages.len() - 1; - } - } + if found_package { + return Some(package_index); } - return (package_index, &mut packages[package_index]); + return None; } fn install_glam_package(root : &str, commit : &str, package : &mut GlamPackage, update_package : bool, copy_files : bool, verbose : bool) { @@ -199,6 +246,14 @@ fn install_glam_package(root : &str, commit : &str, package : &mut GlamPackage, package.commit = commit.to_string(); } + if package.source_folder == "" { + package.source_folder = format!("addons/{}", package.name); + } + + if package.target_folder == "" { + package.target_folder = format!("addons/{}", package.name); + } + if package.commit == "" { let res = utils::run_shell_command( &format!("cd .glam.d/{} && git rev-parse HEAD", -- cgit v1.2.3