diff options
author | Henrique Alves <henriquelalves@gmail.com> | 2023-04-19 09:56:27 -0300 |
---|---|---|
committer | Henrique Alves <henriquelalves@gmail.com> | 2023-04-19 09:56:27 -0300 |
commit | efc547719531d90b32867dda47da506441178d6e (patch) | |
tree | 3beceec59691e06eda1101626ec4481fa6211c72 | |
parent | 5a94bd3f86b28a3d135256d83baa12f7c83afdab (diff) | |
download | glam-efc547719531d90b32867dda47da506441178d6e.tar.gz glam-efc547719531d90b32867dda47da506441178d6e.tar.bz2 glam-efc547719531d90b32867dda47da506441178d6e.zip |
Add inquire to add command
-rw-r--r-- | src/commands.rs | 913 | ||||
-rw-r--r-- | src/content.rs | 15 | ||||
-rw-r--r-- | src/main.rs | 190 | ||||
-rw-r--r-- | src/utils.rs | 114 |
4 files changed, 614 insertions, 618 deletions
diff --git a/src/commands.rs b/src/commands.rs index a863705..a796762 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -1,529 +1,546 @@ -use std::path::{Path, PathBuf}; +use inquire::Text; +use serde::{Deserialize, Serialize}; use std::fs; use std::fs::write; +use std::path::{Path, PathBuf}; use std::process::exit; -use serde::{Serialize, Deserialize}; -#[path = "utils.rs"] mod utils; -#[path = "content.rs"] mod content; +#[path = "content.rs"] +mod content; +#[path = "utils.rs"] +mod utils; #[derive(Serialize, Deserialize)] struct GlamObject { - packages: Vec<GlamPackage> + packages: Vec<GlamPackage>, } #[derive(Clone, Debug, Serialize, Deserialize)] 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, + 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(); + return "".to_string(); } -pub fn initialize(root : &str) { - let git_ignore = &format!("{}/.gitignore", &root); - if !Path::new(git_ignore).exists() { - match write(git_ignore, content::create_gitignore_file()) { - Ok(_v) => (), - Err(_e) => { - utils::log_error("There was a problem creating the .gitignore file!"); - exit(1); - } - } - } +pub fn initialize(root: &str) { + let git_ignore = &format!("{}/.gitignore", &root); + if !Path::new(git_ignore).exists() { + match write(git_ignore, content::create_gitignore_file()) { + Ok(_v) => (), + Err(_e) => { + utils::log_error("There was a problem creating the .gitignore file!"); + exit(1); + } + } + } } -pub fn initialize_glam_files(root : &str) { - // Create glam.d/ folder if it doesn't exist - if !Path::new(&format!("{}/.glam.d/", root)).exists() { - let res = utils::run_shell_command( - "mkdir -p .glam.d", - &root, - false - ); - - utils::assert_res(&res, "Couldn't create .glam.d/ folder!"); - - let gd_ignore = &format!("{}/.glam.d/.gdignore", &root); - if !Path::new(gd_ignore).exists() { - match write(gd_ignore, content::create_gdignore_file()) { - Ok(_v) => (), - Err(_e) => { - utils::log_error("There was a problem creating the .gdignore file!"); - exit(1); - } - } - } - - utils::log_info("Created .glam.d/ folder"); - } - - // Create .glam file if it doesn't exist - if !Path::new(&format!("{}/.glam", root)).exists() { - fs::write(&format!("{}/.glam",root), - content::create_glam_file()) - .expect("Couldn't create .glam file!"); - utils::log_info("Created .glam file"); - } +pub fn initialize_glam_files(root: &str) { + // Create glam.d/ folder if it doesn't exist + if !Path::new(&format!("{}/.glam.d/", root)).exists() { + let res = utils::run_shell_command("mkdir -p .glam.d", &root, false); + + utils::assert_result(&res, "Couldn't create .glam.d/ folder!"); + + let gd_ignore = &format!("{}/.glam.d/.gdignore", &root); + if !Path::new(gd_ignore).exists() { + match write(gd_ignore, content::create_gdignore_file()) { + Ok(_v) => (), + Err(_e) => { + utils::log_error("There was a problem creating the .gdignore file!"); + exit(1); + } + } + } + + utils::log_info("Created .glam.d/ folder"); + } + + // Create .glam file if it doesn't exist + if !Path::new(&format!("{}/.glam", root)).exists() { + fs::write(&format!("{}/.glam", root), content::create_glam_file()) + .expect("Couldn't create .glam file!"); + utils::log_info("Created .glam file"); + } } -pub fn check_initialization(root : &str) -> bool { - let git_ignore = &format!("{}/.gitignore", &root); - if !Path::new(git_ignore).exists() { - utils::log_warning(".gitignore file does not exist!"); - } - - let mut ret = true; - let glam_file = &format!("{}/.glam", &root); - if !Path::new(glam_file).exists() { - utils::log_error(".glam file does not exist!"); - ret = false; - } - - let glam_folder = &format!("{}/.glam.d/", &root); - if !Path::new(glam_folder).exists() { - utils::log_error(".glam.d/ folder does not exist!"); - ret = false; - } - return ret; +pub fn check_initialization(root: &str) -> bool { + let git_ignore = &format!("{}/.gitignore", &root); + if !Path::new(git_ignore).exists() { + utils::log_warning(".gitignore file does not exist!"); + } + + let mut ret = true; + let glam_file = &format!("{}/.glam", &root); + if !Path::new(glam_file).exists() { + utils::log_error(".glam file does not exist!"); + ret = false; + } + + let glam_folder = &format!("{}/.glam.d/", &root); + if !Path::new(glam_folder).exists() { + utils::log_error(".glam.d/ folder does not exist!"); + ret = false; + } + return ret; } -pub fn install_all_packages(root : &str, verbose : bool, copy_files : bool) { - let glam_file_path = format!("{}/.glam", root); +pub fn install_repositories(root: &str, verbose: bool, copy_files: bool) { + let glam_file_path = format!("{}/.glam", root); - // Find glam object or create one with default configuration - let mut glam_object = read_glam_file(&glam_file_path); - let mut glam_packages = glam_object.packages; + // Find glam object or create one with default configuration + 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, copy_files, verbose); - } + 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, copy_files, verbose); + } - // Write .glam file - glam_object.packages = glam_packages; - write_glam_file(&glam_file_path, &glam_object); + // 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) { - let glam_file_path = format!("{}/.glam", root); +pub fn add_repository(root: &str, git_repo: &str, verbose: bool) { + // Create glam object with default configuration + let glam_file_path = format!("{}/.glam", root); + let mut glam_object = read_glam_file(&glam_file_path); + let mut glam_packages = glam_object.packages; - // Find glam object or create one with default configuration - let mut glam_object = read_glam_file(&glam_file_path); - let mut glam_packages = glam_object.packages; - - let name = utils::get_repo_name(git_repo); + if find_package_by_repository(&glam_packages, &git_repo).is_some() { + utils::log_error("Repository already exists!"); + exit(1); + } - 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: "".to_string(), - source_folder: "".to_string() - }); + let default_name: String = utils::get_repo_name(git_repo); + let inquire_name = "Name of the repository:"; + let name = Text::new(inquire_name).with_default(&default_name).with_placeholder(&default_name).prompt().unwrap(); - package_index = Some(glam_packages.len() - 1); - } + if find_package_by_name(&glam_packages, &name).is_some() { + utils::log_error("Repository name exists!"); + exit(1); + } - _ => {} - } + let default_commit = "latest"; + let inquire_commit = "Commit hash of the repository:"; + let commit = Text::new(inquire_commit).with_default(&default_commit).with_placeholder(&default_commit).prompt().unwrap(); - let package_index = package_index.unwrap(); - let target_package = &mut glam_packages[package_index]; + glam_packages.push(GlamPackage { + name: name.to_string(), + git_repo: git_repo.to_string(), + commit: commit.to_string(), + target_folder: "".to_string(), + source_folder: "".to_string(), + }); - clone_or_fetch_package(root, target_package, verbose); + let target_package = glam_packages.last_mut().unwrap(); - // Update package folder to commit hash - install_glam_package(root, commit, target_package, false, copy_files, verbose); + clone_or_fetch_package(root, target_package, verbose); + install_glam_package(root, &commit, target_package, false, true, verbose); - // Write .glam file - glam_object.packages = glam_packages; - write_glam_file(&glam_file_path, &glam_object); + // Write .glam file + glam_object.packages = glam_packages; + write_glam_file(&glam_file_path, &glam_object); } -pub fn update_all_packages(root : &str, verbose : bool, copy_files : bool) { - let glam_file_path = format!("{}/.glam", root); +pub fn update_repositories(root: &str, verbose: bool, copy_files: bool) { + let glam_file_path = format!("{}/.glam", root); - // Find glam object or create one with default configuration - let mut glam_object = read_glam_file(&glam_file_path); - let mut glam_packages = glam_object.packages; + // Find glam object or create one with default configuration + 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!("Updating {}...", package.name)); - clone_or_fetch_package(root, package, verbose); - install_glam_package(root, "", package, true, copy_files, verbose); - } + 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, copy_files, verbose); + } - glam_object.packages = glam_packages; - write_glam_file(&glam_file_path, &glam_object); + glam_object.packages = glam_packages; + write_glam_file(&glam_file_path, &glam_object); } -pub fn update_package(root : &str, package_name : &str, verbose : bool, copy_files : bool) { - let glam_file_path = format!("{}/.glam", root); +pub fn update_package(root: &str, package_name: &str, verbose: bool, copy_files: bool) { + let glam_file_path = format!("{}/.glam", root); - // Find package to update + // Find package to update - let mut glam_object = read_glam_file(&glam_file_path); - let mut glam_packages = glam_object.packages; + let mut glam_object = read_glam_file(&glam_file_path); + let mut glam_packages = glam_object.packages; - let package_index = find_package(&glam_packages, package_name); + let package_index = find_package_by_name(&glam_packages, package_name); - if package_index.is_none() { - utils::log_error("Package not found!"); - exit(1); - } + 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]; + 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, "", target_package, true, copy_files, verbose); + clone_or_fetch_package(root, target_package, verbose); + install_glam_package(root, "", target_package, true, copy_files, verbose); - // Write .glam file - glam_object.packages = glam_packages; - write_glam_file(&glam_file_path, &glam_object); + // Write .glam file + glam_object.packages = glam_packages; + write_glam_file(&glam_file_path, &glam_object); } -pub fn remove_package(root : &str, package_name : &str, verbose : bool) { - let glam_file_path = format!("{}/.glam", root); - - // Find package to update - - let mut glam_object = read_glam_file(&glam_file_path); - let mut glam_packages = glam_object.packages; - - 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]; +// TODO: just "apply" don't apply changes to all repositories +pub fn apply_changes(root: &str, package_name: &str, create_from_addon: &str, verbose: bool) { + let glam_file_path = format!("{}/.glam", root); + + // Find package to update + + let mut glam_object = read_glam_file(&glam_file_path); + let mut glam_packages = glam_object.packages; + + let mut package_index = find_package_by_name(&glam_packages, package_name); + + if package_index.is_none() && create_from_addon == "" { + utils::log_error("Package not found!"); + exit(1); + } else if package_index.is_some() && create_from_addon != "" { + utils::log_error("Package already exists!"); + exit(1); + } else if create_from_addon != "" { + create_new_package_from_addon( + root, + &mut glam_packages, + package_name, + create_from_addon, + verbose, + ); + package_index = Some(glam_packages.len() - 1); + } + + let package_index = package_index.unwrap(); + let target_package = &mut glam_packages[package_index]; + + apply_glam_package_files(root, target_package, verbose); + + // Save changes + if create_from_addon != "" { + glam_object.packages = glam_packages; + write_glam_file(&glam_file_path, &glam_object); + } +} - remove_glam_package_files(root, target_package, verbose); +fn find_package_by_name(packages: &Vec<GlamPackage>, name: &str) -> Option<usize> { + let mut package_index = 0; + let mut found_package = false; - glam_packages.remove(package_index); + for (i, package) in packages.iter().enumerate() { + if package.name == name { + package_index = i; + found_package = true; + } + } - // Write .glam file - glam_object.packages = glam_packages; - write_glam_file(&glam_file_path, &glam_object); -} + if found_package { + return Some(package_index); + } -// TODO: just "apply" don't apply changes to all repositories -pub fn apply_changes(root : &str, package_name : &str, create_from_addon : &str, verbose : bool) { - let glam_file_path = format!("{}/.glam", root); - - // Find package to update - - let mut glam_object = read_glam_file(&glam_file_path); - let mut glam_packages = glam_object.packages; - - let mut package_index = find_package(&glam_packages, package_name); - - if package_index.is_none() && create_from_addon == "" { - utils::log_error("Package not found!"); - exit(1); - } - else if package_index.is_some() && create_from_addon != "" { - utils::log_error("Package already exists!"); - exit(1); - } - else if create_from_addon != "" { - create_new_package_from_addon(root, &mut glam_packages, package_name, create_from_addon, verbose); - package_index = Some(glam_packages.len()-1); - } - - let package_index = package_index.unwrap(); - let target_package = &mut glam_packages[package_index]; - - apply_glam_package_files(root, target_package, verbose); - - // Save changes - if create_from_addon != "" { - glam_object.packages = glam_packages; - write_glam_file(&glam_file_path, &glam_object); - } + return None; } -fn find_package(packages : &Vec<GlamPackage>, name : &str) -> Option<usize> { - let mut package_index = 0; - let mut found_package = false; +fn find_package_by_repository(packages: &Vec<GlamPackage>, repo: &str) -> Option<usize> { + let mut package_index = 0; + let mut found_package = false; - for (i, package) in packages.iter().enumerate() { - if package.name == name { - package_index = i; - found_package = true; - } - } + for (i, package) in packages.iter().enumerate() { + if package.git_repo == repo { + package_index = i; + found_package = true; + } + } - if found_package { - return Some(package_index); - } + if found_package { + return Some(package_index); + } - return None; + return None; } -fn install_glam_package(root : &str, commit : &str, package : &mut GlamPackage, update_package : bool, copy_files : bool, verbose : bool) { - // Update package folder to commit hash - if update_package { - package.commit = "".to_string(); - } - - if commit != "" { - package.commit = commit.to_string(); - } - - // TODO Get all folders on addon - let res = utils::run_shell_command( - &format!("ls .glam.d/{}/addons", package.name), - &root, - verbose); - - if res.is_err() { - utils::log_error("Couldn't get addon name."); - exit(1); - } - - let addon_folder = res.unwrap().trim().to_string(); - - if package.source_folder == "" { - package.source_folder = format!("addons/{}", addon_folder); - } - - if package.target_folder == "" { - package.target_folder = format!("addons/{}", addon_folder); - } - - if package.commit == "" { - let res = utils::run_shell_command( - &format!("cd .glam.d/{} && git rev-parse HEAD", - package.name), - &root, - verbose).unwrap(); - package.commit = res.trim().to_string(); - } else { - utils::log_info("Git checkout to package commit"); - let res = utils::run_shell_command( - &format!("cd .glam.d/{} && git reset --hard {}", - package.name, - package.commit), - &root, - verbose); - - utils::assert_res(&res, "Couldn't checkout repository!"); - } - - if copy_files { - // If project addon folder doesn't exist, create it - let res = utils::run_shell_command( - &format!("mkdir -p {}", package.target_folder), - &root, - verbose - ); - - utils::assert_res(&res, "Couldn't create addons folder!"); - // TODO: Why this is throwing Err("cp: -t: No such file or directory\n") on mac? - println!("cp -rf .glam.d/{}/{}/* -t {}", package.name, package.source_folder, package.target_folder); - - // Copy addon repository content to target folder - let res = utils::run_shell_command( - &format!("cp -rf .glam.d/{}/{}/* -t {}", package.name, package.source_folder, package.target_folder), - &root, - verbose - ); - - utils::assert_res(&res, "Couldn't copy files to addons!"); - } +fn install_glam_package( + root: &str, + commit: &str, + package: &mut GlamPackage, + update_package: bool, + copy_files: bool, + verbose: bool, +) { + // Update package folder to commit hash + if update_package { + package.commit = "latest".to_string(); + } + + if commit != "latest" { + package.commit = commit.to_string(); + } + + // TODO Get all folders on addon + let res = utils::run_shell_command( + &format!("ls .glam.d/{}/addons", package.name), + &root, + verbose, + ); + + if res.is_err() { + utils::log_error("Couldn't get addon name."); + exit(1); + } + + let addon_folder = res.unwrap().trim().to_string(); + + if package.source_folder == "" { + package.source_folder = format!("addons/{}", addon_folder); + } + + if package.target_folder == "" { + package.target_folder = format!("addons/{}", addon_folder); + } + + if package.commit == "latest" { + let res = utils::run_shell_command( + &format!("cd .glam.d/{} && git rev-parse HEAD", package.name), + &root, + verbose, + ) + .unwrap(); + package.commit = res.trim().to_string(); + } else { + utils::log_info("Git checkout to package commit"); + let res = utils::run_shell_command( + &format!( + "cd .glam.d/{} && git reset --hard {}", + package.name, package.commit + ), + &root, + verbose, + ); + + utils::assert_result(&res, "Couldn't checkout repository!"); + } + + if copy_files { + // If project addon folder doesn't exist, create it + let res = utils::run_shell_command( + &format!("mkdir -p {}", package.target_folder), + &root, + verbose, + ); + + utils::assert_result(&res, "Couldn't create addons folder!"); + // TODO: Why this is throwing Err("cp: -t: No such file or directory\n") on mac? + // println!( + // "cp -rf .glam.d/{}/{}/* -t {}", + // package.name, package.source_folder, package.target_folder + // ); + + // Copy addon repository content to target folder + let res = utils::run_shell_command( + &format!( + "cp -rf .glam.d/{}/{}/* -t {}", + package.name, package.source_folder, package.target_folder + ), + &root, + verbose, + ); + + utils::assert_result(&res, "Couldn't copy files to addons!"); + } } -fn create_new_package_from_addon(root : &str, packages : &mut Vec<GlamPackage>, package_name : &str, create_from_addon : &str, verbose : bool) { - if !Path::new(&format!("addons/{}", create_from_addon)).exists() { - utils::log_error("Addon folder doesn't exist!"); - exit(1); - } - - packages.push(GlamPackage { - name: package_name.to_string(), - git_repo : "".to_string(), - commit: "".to_string(), - target_folder: format!("addons/{}",create_from_addon), - source_folder: format!("addons/{}",create_from_addon), - }); - - let package = &packages[packages.len()-1]; - - let res = utils::run_shell_command( - &format!("mkdir -p .glam.d/{}/{}", package.name, package.source_folder), - &root, - false - ); - - utils::assert_res(&res, "Couldn't create source folder!"); - - let res = utils::run_shell_command( - &format!("git init .glam.d/{}", package.name), - &root, - false - ); - - utils::assert_res(&res, "Couldn't create new repository!"); - - let git_ignore_path = Path::new(root).join(&format!(".glam.d/{}/.gitignore", package.name)); - let git_ignore = git_ignore_path.to_str().unwrap(); - - match write(git_ignore, content::create_gitignore_file()) { - Ok(_v) => (), - Err(_e) => { - utils::log_error("There was a problem creating the .gitignore file!"); - exit(1); - } - } - - apply_glam_package_files(root, &packages[packages.len()-1], verbose); +fn create_new_package_from_addon( + root: &str, + packages: &mut Vec<GlamPackage>, + package_name: &str, + create_from_addon: &str, + verbose: bool, +) { + if !Path::new(&format!("addons/{}", create_from_addon)).exists() { + utils::log_error("Addon folder doesn't exist!"); + exit(1); + } + + packages.push(GlamPackage { + name: package_name.to_string(), + git_repo: "".to_string(), + commit: "".to_string(), + target_folder: format!("addons/{}", create_from_addon), + source_folder: format!("addons/{}", create_from_addon), + }); + + let package = &packages[packages.len() - 1]; + + let res = utils::run_shell_command( + &format!( + "mkdir -p .glam.d/{}/{}", + package.name, package.source_folder + ), + &root, + false, + ); + + utils::assert_result(&res, "Couldn't create source folder!"); + + let res = utils::run_shell_command(&format!("git init .glam.d/{}", package.name), &root, false); + + utils::assert_result(&res, "Couldn't create new repository!"); + + let git_ignore_path = Path::new(root).join(&format!(".glam.d/{}/.gitignore", package.name)); + let git_ignore = git_ignore_path.to_str().unwrap(); + + match write(git_ignore, content::create_gitignore_file()) { + Ok(_v) => (), + Err(_e) => { + utils::log_error("There was a problem creating the .gitignore file!"); + exit(1); + } + } + + apply_glam_package_files(root, &packages[packages.len() - 1], verbose); } -fn apply_glam_package_files(root : &str, package : &GlamPackage, verbose : bool) { - - // Overwrite source folder with target folder - let res = utils::run_shell_command( - &format!("for f in $(ls .glam.d/{}/{}); do rm -rf .glam.d/{}/{}/$f; done", - package.name, - package.source_folder, - package.name, - package.source_folder), - &root, - verbose - ); - - utils::assert_res(&res, "Couldn't remove source folder files!"); - - // Copy addon repository content to target folder - let res = utils::run_shell_command( - &format!("for f in $(ls ./{}); do cp -rf ./{}/$f ./.glam.d/{}/{}/$f; done", - package.target_folder, - package.target_folder, - package.name, - package.source_folder), - &root, - verbose - ); - - utils::assert_res(&res, "Couldn't copy files to repository!"); +fn apply_glam_package_files(root: &str, package: &GlamPackage, verbose: bool) { + // Overwrite source folder with target folder + let res = utils::run_shell_command( + &format!( + "for f in $(ls .glam.d/{}/{}); do rm -rf .glam.d/{}/{}/$f; done", + package.name, package.source_folder, package.name, package.source_folder + ), + &root, + verbose, + ); + + utils::assert_result(&res, "Couldn't remove source folder files!"); + + // Copy addon repository content to target folder + let res = utils::run_shell_command( + &format!( + "for f in $(ls ./{}); do cp -rf ./{}/$f ./.glam.d/{}/{}/$f; done", + package.target_folder, package.target_folder, package.name, package.source_folder + ), + &root, + verbose, + ); + + utils::assert_result(&res, "Couldn't copy files to repository!"); } -fn remove_glam_package_files(root : &str, package : &GlamPackage, verbose : bool) { - // Copy addon repository content to target folder - let res = utils::run_shell_command( - &format!("rm -rf {}", package.target_folder), - &root, - verbose - ); +fn remove_glam_package_files(root: &str, package: &GlamPackage, verbose: bool) { + // Copy addon repository content to target folder + let res = + utils::run_shell_command(&format!("rm -rf {}", package.target_folder), &root, verbose); - utils::assert_res(&res, "Couldn't remove files from root addons!"); + utils::assert_result(&res, "Couldn't remove files from root addons!"); - let res = utils::run_shell_command( - &format!("rm -rf ./.glam.d/{}", package.name), - &root, - verbose - ); + let res = utils::run_shell_command( + &format!("rm -rf ./.glam.d/{}", package.name), + &root, + verbose, + ); - utils::assert_res(&res, "Couldn't copy files to addons!"); + utils::assert_result(&res, "Couldn't copy files to addons!"); } -fn clone_or_fetch_package(root : &str, package : &mut GlamPackage, verbose : bool) { - // If glam package folder doesn't exist, clone project - if !Path::new(&format!("{}/.glam.d/{}", root, package.name)).exists() { - let res = utils::run_shell_command( - &format!("cd .glam.d/ && git clone {} {} --progress", package.git_repo, package.name), - &root, - verbose - ); - - utils::assert_res(&res, "Couldn't clone repository!"); - - utils::log_check("Created package folder on .glam.d"); - } else { - if package.git_repo == "" { - let res = utils::run_shell_command( - &format!("cd .glam.d/{} && git remote get-url origin", - package.name), - &root, - verbose); - - if res.is_err() { - utils::log_error("GLAM Package has no origin yet!"); - exit(1); - } - - package.git_repo = res.unwrap().trim().to_string(); - } - - let res = utils::run_shell_command( - &format!("cd .glam.d/{} && git fetch origin && git pull", package.name), - &root, - verbose - ); - utils::assert_res(&res, "Couldn't fetch package repository updates!"); - - utils::log_info("Glam package folder already exists, fetched and pulled latest changes"); - } +fn clone_or_fetch_package(root: &str, package: &mut GlamPackage, verbose: bool) { + // If glam package folder doesn't exist, clone project + if !Path::new(&format!("{}/.glam.d/{}", root, package.name)).exists() { + let res = utils::run_shell_command( + &format!( + "cd .glam.d/ && git clone {} {} --progress", + package.git_repo, package.name + ), + &root, + verbose, + ); + + utils::assert_result(&res, "Couldn't clone repository!"); + + utils::log_check("Created package folder on .glam.d"); + } else { + if package.git_repo == "" { + let res = utils::run_shell_command( + &format!("cd .glam.d/{} && git remote get-url origin", package.name), + &root, + verbose, + ); + + if res.is_err() { + utils::log_error("GLAM Package has no origin yet!"); + exit(1); + } + + package.git_repo = res.unwrap().trim().to_string(); + } + + let res = utils::run_shell_command( + &format!( + "cd .glam.d/{} && git fetch origin && git pull", + package.name + ), + &root, + verbose, + ); + utils::assert_result(&res, "Couldn't fetch package repository updates!"); + + utils::log_info("Glam package folder already exists, fetched and pulled latest changes"); + } } -fn read_glam_file(file_path : &str) -> GlamObject { - if !Path::new(file_path).exists() { - fs::write(file_path, content::create_glam_file()).expect("Couldn't create .glam file!"); - } +fn read_glam_file(file_path: &str) -> GlamObject { + if !Path::new(file_path).exists() { + fs::write(file_path, content::create_glam_file()).expect("Couldn't create .glam file!"); + } - let glam_content = fs::read_to_string(file_path).expect("Couldn't read .glam file!"); - let glam_obj : GlamObject = serde_json::from_str(&glam_content).unwrap(); + let glam_content = fs::read_to_string(file_path).expect("Couldn't read .glam file!"); + let glam_obj: GlamObject = serde_json::from_str(&glam_content).unwrap(); - return glam_obj; + return glam_obj; } -fn write_glam_file(file_path : &str, glam_object : &GlamObject) { - let json_string = serde_json::to_string_pretty(glam_object).unwrap(); - fs::write(file_path, json_string).expect("Couldn't create .glam file!"); +fn write_glam_file(file_path: &str, glam_object: &GlamObject) { + let json_string = serde_json::to_string_pretty(glam_object).unwrap(); + fs::write(file_path, json_string).expect("Couldn't create .glam file!"); } -pub fn search_project_root() -> String{ - let path = PathBuf::from("./"); - let mut dir = path.canonicalize().unwrap(); - - loop { - let dir_path = dir.to_str().unwrap(); - let proj_path = format!("{}/project.godot", dir_path); - - let godot_project = Path::new(&proj_path); - - if godot_project.exists() { - break; - } - else { - let parent = dir.parent(); - if parent.is_none() { - utils::log_error("Godot project not found!"); - exit(1); - } - dir = dir.parent().unwrap().to_path_buf(); - } - } - - let root = dir.to_str().unwrap().to_string(); - utils::log_check(&format!("Found root project in: {}", root)); - return root; -} +pub fn search_project_root() -> String { + let path = PathBuf::from("./"); + let mut dir = path.canonicalize().unwrap(); + + loop { + let dir_path = dir.to_str().unwrap(); + let proj_path = format!("{}/project.godot", dir_path); + + let godot_project = Path::new(&proj_path); + + if godot_project.exists() { + break; + } else { + let parent = dir.parent(); + if parent.is_none() { + utils::log_error("Godot project not found!"); + exit(1); + } + dir = dir.parent().unwrap().to_path_buf(); + } + } + + let root = dir.to_str().unwrap().to_string(); + utils::log_check(&format!("Found root project in: {}", root)); + return root; +}
\ No newline at end of file diff --git a/src/content.rs b/src/content.rs index 94ea18d..b31b033 100644 --- a/src/content.rs +++ b/src/content.rs @@ -1,6 +1,5 @@ pub fn create_gitignore_file() -> String { - return -r#"# Godot 4+ ignores + return r#"# Godot 4+ ignores .godot/ # Godot 3 ignores @@ -14,19 +13,19 @@ r#"# Godot 4+ ignores data_*/ # Glam-specific ignores -.glam.d/"#.to_string(); +.glam.d/"# + .to_string(); } pub fn create_gdignore_file() -> String { - return -r#"# Hide this folder from Godot editor"#.to_string(); + return r#"# Hide this folder from Godot editor"#.to_string(); } pub fn create_glam_file() -> String { - return -r#"{ + return r#"{ "packages" : [ ] } -"#.to_string(); +"# + .to_string(); } diff --git a/src/main.rs b/src/main.rs index f3b1e72..cad56d5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,11 +4,11 @@ mod commands; #[derive(Parser)] struct Cli { - #[clap(subcommand)] - command: Commands, - /// Verbose (output subshell commands) - #[clap(short, long, takes_value = false)] - verbose: bool + #[clap(subcommand)] + command: Commands, + /// Verbose (output subshell commands) + #[clap(short, long, takes_value = false)] + verbose: bool, } // TODO: COMMANDS BROKE @@ -17,107 +17,87 @@ struct Cli { #[derive(Subcommand)] enum Commands { - /// Initialize Godot project for GLAM - Init { - }, - - /// Add new repository - Add { - /// Package project git - git_repo: String, - /// Commit to checkout (default is latest) - #[clap(short, long, required = false, default_value = "")] - commit: String, - /// Don't copy to target folder - #[clap(short, long, required = false, takes_value = false)] - no_copy: bool, - }, - - /// Install all packages on .glam file - Install { - /// Don't copy to target folder - #[clap(short, long, required = false, takes_value = false)] - no_copy: bool, - }, - - /// Update a single repository. If no repository name is provided, update all repositories - Update { - /// Name of the package to update (default is all packages) - package_name: String, - /// Don't copy to target folder - #[clap(short, long, required = false, takes_value = false)] - no_copy: bool, - }, - - /// Remove a repository - Remove { - /// Name of the package to remove - package_name: String, - }, - - /// Apply changes to a repository - Apply { - /// Names of the package to apply changes to - package_names: Vec<String>, - /// Create new package from the specified addon folder (will create a git repo) - #[clap(short, long, required = false, default_value = "")] - create_from_addon: String, - } + /// Initialize Godot project for GLAM + Init {}, + + /// Add new repository + Add { + /// Package project git + git_repo: String, + }, + + /// Install all packages on .glam file + Install { + /// Don't copy to target folder + #[clap(short, long, required = false, takes_value = false)] + no_copy: bool, + }, + + /// Update a single repository. If no repository name is provided, update all repositories + Update { + /// Name of the package to update (default is all packages) + package_name: String, + /// Don't copy to target folder + #[clap(short, long, required = false, takes_value = false)] + no_copy: bool, + }, + + /// Apply changes to a repository + Apply { + /// Names of the package to apply changes to + package_names: Vec<String>, + /// Create new package from the specified addon folder (will create a git repo) + #[clap(short, long, required = false, default_value = "")] + create_from_addon: String, + }, } fn main() { - let cli = Cli::parse(); - - match &cli.command { - Commands::Init {} => { - let root = commands::search_project_root(); - commands::initialize_glam_files(&root); - commands::initialize(&root); - }, - - Commands::InstallPackage {git_repo, commit, no_copy} => { - let root = commands::search_project_root(); - if commands::check_initialization(&root) { - commands::install_package(&root, git_repo, commit, !*no_copy, cli.verbose); - } - }, - - Commands::Install { no_copy } => { - let root = commands::search_project_root(); - if commands::check_initialization(&root) { - commands::install_all_packages(&root, cli.verbose, !*no_copy); - } - }, - - Commands::UpdatePackage { package_name, no_copy } => { - let root = commands::search_project_root(); - if commands::check_initialization(&root) { - commands::update_package(&root, &package_name, cli.verbose, !*no_copy); - } - - }, - - Commands::Update { no_copy } => { - let root = commands::search_project_root(); - if commands::check_initialization(&root) { - commands::update_all_packages(&root, cli.verbose, !*no_copy); - } - }, - - Commands::RemovePackage {package_name} => { - let root = commands::search_project_root(); - if commands::check_initialization(&root) { - commands::remove_package(&root, &package_name, cli.verbose); - } - }, - - Commands::Apply {package_names, create_from_addon} => { - let root = commands::search_project_root(); - if commands::check_initialization(&root) { - for package_name in package_names { - commands::apply_changes(&root, &package_name, &create_from_addon, cli.verbose); - } - } - }, - } + let cli = Cli::parse(); + + match &cli.command { + Commands::Init {} => { + let root = commands::search_project_root(); + commands::initialize_glam_files(&root); + commands::initialize(&root); + } + + Commands::Add { + git_repo, + } => { + let root = commands::search_project_root(); + if commands::check_initialization(&root) { + commands::add_repository(&root, git_repo, cli.verbose); + } + } + + Commands::Install { no_copy } => { + let root = commands::search_project_root(); + if commands::check_initialization(&root) { + commands::install_repositories(&root, cli.verbose, !*no_copy); + } + } + + Commands::Update { + no_copy, + package_name, + } => { + let root = commands::search_project_root(); + if commands::check_initialization(&root) { + commands::update_repositories(&root, cli.verbose, !*no_copy); + } + } + + Commands::Apply { + package_names, + create_from_addon, + } => { + let root = commands::search_project_root(); + if commands::check_initialization(&root) { + for package_name in package_names { + commands::apply_changes(&root, &package_name, &create_from_addon, cli.verbose); + } + } + } + } } diff --git a/src/utils.rs b/src/utils.rs index 81bd63f..71181ab 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,90 +1,90 @@ use colored::Colorize; +use std::fmt::Debug; use std::io::Write; use std::process::exit; pub fn log_warning(msg: &str) { - let msg = format!("⚠️ {}", msg).yellow(); - println!("{}", msg); + let msg = format!("⚠️ {}", msg).yellow(); + println!("{}", msg); } pub fn log_error(msg: &str) { - let msg = format!("❌ {}", msg).red(); - println!("{}", msg); + let msg = format!("❌ {}", msg).red(); + println!("{}", msg); } pub fn log_info(msg: &str) { - let msg = format!("ℹ️ {}", msg).blue(); - println!("{}", msg); + let msg = format!("ℹ️ {}", msg).blue(); + println!("{}", msg); } pub fn log_check(msg: &str) { - let msg = format!("✅ {}", msg).green(); - println!("{}", msg); + let msg = format!("✅ {}", msg).green(); + println!("{}", msg); } -pub fn assert_res(res : &Result<String, String>, error_msg : &str) { - if res.is_err() { - log_error(error_msg); - log_error(&format!("{:?}", res)); - exit(1); - } +pub fn assert_result<E>(res: &Result<String, E>, error_msg: &str) where E: Debug { + if res.is_err() { + log_error(error_msg); + log_error(&format!("{:?}", res)); + exit(1); + } } pub fn get_repo_name(repo: &str) -> String { - let mut chars = repo.chars().rev(); - let length = repo.chars().count(); - let mut last_i = 0; - let mut first_i = 0; + let mut chars = repo.chars().rev(); + let length = repo.chars().count(); + let mut last_i = 0; + let mut first_i = 0; - let mut i = length; + let mut i = length; - while i > 0 { - match chars.next() { - Some('.') => { - last_i = i-1; - } - Some('/') => { - first_i = i; - break; - } - _ => {} - } - i -= 1; - } + while i > 0 { + match chars.next() { + Some('.') => { + last_i = i - 1; + } + Some('/') => { + first_i = i; + break; + } + _ => {} + } + i -= 1; + } - if last_i == 0 { - last_i = length; - } + if last_i == 0 { + last_i = length; + } - let name = &repo[first_i..last_i]; - // TODO: Return a Result (may be error) - return name.to_string(); + let name = &repo[first_i..last_i]; + // TODO: Return a Result (may be error) + return name.to_string(); } -pub fn run_shell_command(command : &str, folder : &str, verbose : bool) -> Result<String, String> { - let output = std::process::Command:: - new("sh") - .current_dir(folder) - .stdin(std::process::Stdio::inherit()) +pub fn run_shell_command(command: &str, folder: &str, verbose: bool) -> Result<String, String> { + let output = std::process::Command::new("sh") + .current_dir(folder) + .stdin(std::process::Stdio::inherit()) .arg("-c") .arg(command) .output() .expect("Error running command."); - if verbose { - std::io::stdout().write_all(&output.stdout).unwrap(); - std::io::stderr().write_all(&output.stderr).unwrap(); - } + if verbose { + std::io::stdout().write_all(&output.stdout).unwrap(); + std::io::stderr().write_all(&output.stderr).unwrap(); + } - match output.status.success() { - true => { - let stdout_str = String::from_utf8(output.stdout).unwrap(); - return Ok(stdout_str); - } + match output.status.success() { + true => { + let stdout_str = String::from_utf8(output.stdout).unwrap(); + return Ok(stdout_str); + } - false => { - let stderr_str = String::from_utf8(output.stderr).unwrap(); - return Err(stderr_str); - } - } + false => { + let stderr_str = String::from_utf8(output.stderr).unwrap(); + return Err(stderr_str); + } + } } |