summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHenrique Alves <henriquelalves@gmail.com>2022-07-25 21:14:13 -0300
committerHenrique Alves <henriquelalves@gmail.com>2022-07-25 21:14:13 -0300
commita1b227642bc812169bf0ce14cecd63e80fcc5460 (patch)
tree174227f66514819d8ab3ea50237b1861dbf1ff1b
parentb32d5035628b2d575806eefa4ae3717fb67402eb (diff)
downloadglam-a1b227642bc812169bf0ce14cecd63e80fcc5460.tar.gz
glam-a1b227642bc812169bf0ce14cecd63e80fcc5460.tar.bz2
glam-a1b227642bc812169bf0ce14cecd63e80fcc5460.zip
Separate commands in another file
-rw-r--r--src/commands.rs223
-rw-r--r--src/main.rs20
2 files changed, 144 insertions, 99 deletions
diff --git a/src/commands.rs b/src/commands.rs
index 08db773..4a97bb6 100644
--- a/src/commands.rs
+++ b/src/commands.rs
@@ -55,89 +55,42 @@ pub fn check_ignores(root : &str) {
}
}
-pub fn install(root : &str, verbose : bool) {
+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 glam_packages = glam_object.packages;
+ let mut glam_packages = glam_object.packages;
- for package in glam_packages.iter() {
- install_package(root, &package.git_repo, "", verbose);
+ for package in glam_packages.iter_mut() {
+ clone_or_fetch_package(root, &package, verbose);
+ let commit = package.commit.to_string();
+ install_glam_package(root, &commit, package, false, true, verbose);
}
}
-pub fn install_package(root : &str, git_repo : &str, commit : &str, verbose : bool) {
+pub fn install_package(root : &str, git_repo : &str, commit : &str, copy_files : bool, verbose : 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;
- let mut package_index = 0;
- let mut found_package = false;
let name = utils::get_repo_name(git_repo);
-
- for (i, package) in glam_packages.iter().enumerate() {
- if package.name == name {
- package_index = i;
- found_package = true;
- }
- }
-
- if !found_package {
- let package = GlamPackage{
+ let (_, target_package) = find_package(
+ &mut glam_packages,
+ &name,
+ Some(GlamPackage{
git_repo : git_repo.to_string(),
name : name.to_string(),
commit : "".to_string(),
- };
-
- glam_packages.push(package);
- package_index = glam_packages.len() - 1;
- }
-
- let target_package = &mut glam_packages[package_index];
+ }));
clone_or_fetch_package(root, &target_package, verbose);
// Update package folder to commit hash
- if commit != "" {
- target_package.commit = commit.to_string();
- }
-
- if target_package.commit == "" {
- let res = utils::run_shell_command(
- &format!("cd .glam.d/{} && git rev-parse HEAD",
- target_package.name),
- &root,
- verbose).unwrap();
- target_package.commit = res.trim().to_string();
- } else {
- utils::log_info("Git checkout to package commit");
- utils::run_shell_command(
- &format!("cd .glam.d/{} && git checkout {}",
- target_package.name,
- target_package.commit),
- &root,
- verbose);
- }
-
- // If project addon folder doesn't exist, create it
- utils::run_shell_command(
- &format!("mkdir -p {}/addons/{}", root, name),
- &root,
- verbose
- );
-
- // TODO: use source_folder to copy files from (default: /addons/)
- // TODO: use target_folder to copy files to (defautl: (root)/)
- // Copy addon repository content to target folder
- utils::run_shell_command(
- &format!("cp -f -r .glam.d/{}/addons/* -t {}/addons/", name, root),
- &root,
- verbose
- );
+ install_glam_package(root, commit, target_package, false, copy_files, verbose);
// Write .glam file
glam_object.packages = glam_packages;
@@ -148,12 +101,16 @@ pub fn update_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 glam_packages = glam_object.packages;
+ let mut glam_object = read_glam_file(&glam_file_path);
+ let mut glam_packages = glam_object.packages;
- for package in glam_packages.iter() {
- install_package(root, &package.git_repo, "", verbose);
+ for package in glam_packages.iter_mut() {
+ clone_or_fetch_package(root, &package, verbose);
+ install_glam_package(root, "", package, true, true, verbose);
}
+
+ glam_object.packages = glam_packages;
+ write_glam_file(&glam_file_path, &glam_object);
}
pub fn update_package(root : &str, package_name : &str, verbose : bool) {
@@ -163,58 +120,146 @@ 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);
+
+ clone_or_fetch_package(root, &target_package, verbose);
+ install_glam_package(root, "", &mut target_package, true, true, verbose);
+
+ // 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 (index, target_package) = find_package(&mut glam_packages, package_name, None);
+
+ remove_glam_package_files(root, &target_package, verbose);
+
+ glam_packages.remove(index);
+
+ // Write .glam file
+ glam_object.packages = glam_packages;
+ write_glam_file(&glam_file_path, &glam_object);
+}
+
+pub fn apply_changes(root : &str, package_name : &str, verbose : bool) {
+ let glam_file_path = format!("{}/.glam", root);
+
+ // Find package to update
+
+ 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);
+}
+
+fn find_package<'a>(packages : &'a mut Vec<GlamPackage>, name : &str, new_package : Option<GlamPackage>) -> (usize, &'a mut GlamPackage) {
let mut package_index = 0;
let mut found_package = false;
- for (i, package) in glam_packages.iter().enumerate() {
- if package.name == package_name {
+ for (i, package) in packages.iter().enumerate() {
+ if package.name == name {
package_index = i;
found_package = true;
}
}
if !found_package {
- utils::log_error("Package not found!");
- exit(1);
+ match new_package {
+ None => {
+ utils::log_error("Package not found!");
+ exit(1);
+ },
+ Some(package) => {
+ packages.push(package);
+ package_index = packages.len() - 1;
+ }
+ }
}
- let target_package = &mut glam_packages[package_index];
+ return (package_index, &mut packages[package_index]);
+}
- clone_or_fetch_package(root, &target_package, verbose);
+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();
+ }
- let res = utils::run_shell_command(
- &format!("cd .glam.d/{} && git rev-parse HEAD",
- target_package.name),
- &root,
- verbose).unwrap();
- target_package.commit = res.trim().to_string();
+ if commit != "" {
+ package.commit = commit.to_string();
+ }
- // If project addon folder doesn't exist, create it
+ 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");
+ utils::run_shell_command(
+ &format!("cd .glam.d/{} && git checkout {}",
+ package.name,
+ package.commit),
+ &root,
+ verbose);
+ }
+
+ if copy_files {
+ // If project addon folder doesn't exist, create it
+ utils::run_shell_command(
+ &format!("mkdir -p {}/addons/{}", root, package.name),
+ &root,
+ verbose
+ );
+
+ // TODO: use source_folder to copy files from (default: /addons/)
+ // TODO: use target_folder to copy files to (defautl: (root)/)
+ // Copy addon repository content to target folder
+ utils::run_shell_command(
+ &format!("cp -rf .glam.d/{}/addons/* -t {}/addons/", package.name, root),
+ &root,
+ verbose
+ );
+ }
+}
+
+fn apply_glam_package_files(root : &str, package : &GlamPackage, verbose : bool) {
+ // TODO: use source_folder to copy files from (default: /addons/)
+ // TODO: use target_folder to copy files to (defautl: (root)/)
+ // Copy addon repository content to target folder
utils::run_shell_command(
- &format!("mkdir -p {}/addons/{}", root, target_package.name),
+ &format!("for f in $(ls ./.glam.d/{}/addons/); do cp -rf ./addons/$f ./.glam.d/{}/addons/; done", package.name, package.name),
&root,
verbose
);
+}
+fn remove_glam_package_files(root : &str, package : &GlamPackage, verbose : bool) {
// TODO: use source_folder to copy files from (default: /addons/)
// TODO: use target_folder to copy files to (defautl: (root)/)
// Copy addon repository content to target folder
utils::run_shell_command(
- &format!("cp -f -r .glam.d/{}/addons/* -t {}/addons/", target_package.name, root),
+ &format!("for f in $(ls ./.glam.d/{}/addons/); do rm -rf ./addons/$f ; done", package.name),
&root,
verbose
);
- // Write .glam file
- glam_object.packages = glam_packages;
- write_glam_file(&glam_file_path, &glam_object);
-}
-
-pub fn remove_package(root : &str, package : &str, verbose : bool) {
-}
-
-pub fn apply_changes(root : &str, package : &str, verbose : bool) {
-
+ utils::run_shell_command(
+ &format!("rm -rf ./.glam.d/{}", package.name),
+ &root,
+ verbose
+ );
}
pub fn initialize_glam_files(root : &str) {
diff --git a/src/main.rs b/src/main.rs
index 6b7691b..5d05698 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -22,20 +22,20 @@ enum Commands {
/// Package project git
git_repo: String,
/// Commit to checkout (default is latest)
- #[clap(short, long, required=false)]
+ #[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 packages on .glam file
+ /// Install all packages on .glam file
Install {
- /// Verbose (output subshell commands)
- #[clap(short, long, takes_value = false)]
- verbose: bool
},
+ /// Update a single GLAM package
UpdatePackage {
/// Name of the package to update (default is all packages)
- #[clap(short, long)]
package_name: String,
},
@@ -66,18 +66,18 @@ fn main() {
commands::initialize(&root);
},
- Commands::InstallPackage {git_repo, commit} => {
+ Commands::InstallPackage {git_repo, commit, no_copy} => {
let root = commands::search_project_root();
commands::check_ignores(&root);
commands::initialize_glam_files(&root);
- commands::install_package(&root, git_repo, commit, cli.verbose);
+ commands::install_package(&root, git_repo, commit, !*no_copy, cli.verbose);
},
- Commands::Install { verbose } => {
+ Commands::Install { } => {
let root = commands::search_project_root();
commands::check_ignores(&root);
commands::initialize_glam_files(&root);
- commands::install(&root, *verbose);
+ commands::install_all_packages(&root, cli.verbose);
},
Commands::UpdatePackage { package_name } => {