diff --git a/src/mods/clone.rs b/src/mods/clone.rs index c6a1d7f..fee6a8b 100644 --- a/src/mods/clone.rs +++ b/src/mods/clone.rs @@ -130,7 +130,9 @@ pub fn clone(noconfirm: bool, as_dep: bool, pkg: &str) { match install_result { Ok(_) => { uninstall_make_depend(pkg); - add_pkg(false, pkg); + let mut vec = Vec::new(); + vec.push(pkg); + add_pkg(false, &vec); } Err(_) => { err_unrec(format!("Couldn't install {}", pkg)); @@ -145,7 +147,9 @@ pub fn clone(noconfirm: bool, as_dep: bool, pkg: &str) { match install_result.code() { Some(0) => { uninstall_make_depend(pkg); - add_pkg(false, pkg); + let mut vec = Vec::new(); + vec.push(pkg); + add_pkg(false, &vec); } Some(_) => { err_unrec(format!("Couldn't install {}", pkg)); @@ -166,7 +170,9 @@ pub fn clone(noconfirm: bool, as_dep: bool, pkg: &str) { match install_result { Ok(_) => { uninstall_make_depend(pkg); - add_pkg(false, pkg); + let mut vec = Vec::new(); + vec.push(pkg); + add_pkg(false, &vec); } Err(_) => { err_unrec(format!("Couldn't install {}", pkg)); diff --git a/src/mods/database.rs b/src/mods/database.rs index 798b605..afa2fdb 100644 --- a/src/mods/database.rs +++ b/src/mods/database.rs @@ -74,7 +74,7 @@ pub fn rem_pkg(pkgs: &[String]) { for i in pkgs { let result = connection.execute(format!( " - DELETE FROM pkgs WHERE name = {}; + DELETE FROM pkgs WHERE name = \"{}\"; ", i )); @@ -85,24 +85,39 @@ pub fn rem_pkg(pkgs: &[String]) { } } -pub fn add_pkg(_from_repo: bool, pkg: &str) { - let file = format!("{}/.local/share/ame/aur_pkgs.db", env::var("HOME").unwrap()); - let connection = sqlite::open(file).unwrap(); - let results = raur::search(&pkg); - let mut package_name = String::new(); - let mut package_version = String::new(); - for res in &results { - package_name = res[0].name.to_string(); - package_version = res[0].version.to_string(); - } - let result = connection.execute(format!( - " - INSERT INTO pkgs (name, version) VALUES (\"{}\", \"{}\"); - ", - package_name, package_version - )); - match result { - Ok(_) => inf(format!("Added {} to database", package_name)), - Err(_) => err_unrec(format!("Couldn't add {} to database", package_name)), +pub fn add_pkg(from_repo: bool, pkgs: &Vec<&str>) { + for pkg in pkgs { + let file = format!("{}/.local/share/ame/aur_pkgs.db", env::var("HOME").unwrap()); + let connection = sqlite::open(file).unwrap(); + let results = raur::search(&pkg); + let mut package_name = String::new(); + let mut package_version = String::new(); + for res in &results { + package_name = res[0].name.to_string(); + package_version = res[0].version.to_string(); + } + if from_repo == false { + let result = connection.execute(format!( + " + INSERT INTO pkgs (name, version) VALUES (\"{}\", \"{}\"); + ", + package_name, package_version + )); + match result { + Ok(_) => inf(format!("Added {} to database", package_name)), + Err(_) => err_unrec(format!("Couldn't add {} to database", package_name)), + } + } else { + let result = connection.execute(format!( + " + INSERT INTO pkgs (name, version) VALUES (\"{}\", \"{}\"); + ", + pkg, "from_repo".to_string() + )); + match result { + Ok(_) => inf(format!("Added {} to database", package_name)), + Err(_) => err_unrec(format!("Couldn't add {} to database", package_name)), + } + } } } diff --git a/src/mods/install.rs b/src/mods/install.rs index 7205f21..f8a415d 100644 --- a/src/mods/install.rs +++ b/src/mods/install.rs @@ -1,4 +1,5 @@ use crate::mods::strs::{err_unrec, succ}; +use crate::mods::database::add_pkg; use runas::Command; pub fn install(noconfirm: bool, as_dep: bool, pkg: &str) { @@ -14,7 +15,10 @@ pub fn install(noconfirm: bool, as_dep: bool, pkg: &str) { .status() .expect("Couldn't call pacman"); match result.code() { - Some(0) => succ(format!("Succesfully installed packages: {}", pkg)), + Some(0) => { + succ(format!("Succesfully installed packages: {}", pkg)); + add_pkg(true, &pkgs); + }, Some(_) => err_unrec(format!("Couldn't install packages: {}", pkg)), None => err_unrec(format!("Couldn't install packages: {}", pkg)), }; @@ -26,7 +30,10 @@ pub fn install(noconfirm: bool, as_dep: bool, pkg: &str) { .status() .expect("Couldn't call pacman"); match result.code() { - Some(0) => succ(format!("Succesfully installed packages: {}", pkg)), + Some(0) => { + succ(format!("Succesfully installed packages: {}", pkg)); + add_pkg(true, &pkgs); + }, Some(_) => err_unrec(format!("Couldn't install packages: {}", pkg)), None => err_unrec(format!("Couldn't install packages: {}", pkg)), }; @@ -41,7 +48,10 @@ pub fn install(noconfirm: bool, as_dep: bool, pkg: &str) { .status() .expect("Couldn't call pacman"); match result.code() { - Some(0) => succ(format!("Succesfully installed packages: {}", pkg)), + Some(0) => { + succ(format!("Succesfully installed packages: {}", pkg)); + add_pkg(true, &pkgs); + }, Some(_) => err_unrec(format!("Couldn't install packages: {}", pkg)), None => err_unrec(format!("Couldn't install packages: {}", pkg)), }; diff --git a/src/mods/stat_database.rs b/src/mods/stat_database.rs index 5272dd5..e23091d 100644 --- a/src/mods/stat_database.rs +++ b/src/mods/stat_database.rs @@ -8,10 +8,10 @@ pub fn stat_create_database() { let _cdar = fs::create_dir_all(format!("/{}/.local/ame/",homepath)); match _cdar { Ok(_) => { - inf(format!("Created path for database (previously missing)")) + inf("Created path for database (previously missing)".to_string()); } Err(_) => { - err_unrec(format!("Couldn't create path for database (~/.local/rhare/ame)")) + err_unrec("Couldn't create path for database (~/.local/share/ame)".to_string()) } } } @@ -24,6 +24,29 @@ pub fn stat_create_database() { .unwrap(); } +pub fn stat_dump_dat() -> Vec { + let homepath = env::var("HOME").unwrap(); + let file = format!("{}/.local/share/ame/aur_pkgs.db", env::var("HOME").unwrap()); + let connection = sqlite::open(file).unwrap(); + let mut dat_pkgs = Vec::new(); + let result = connection + .iterate("SELECT name FROM static_pkgs", |pairs| { + for &(column, value) in pairs.iter() { + dat_pkgs.push(value.unwrap().to_string()); + } + true + }); + match result { + Ok(_) => { + //nf("Dumped static packages".to_string()); + } + Err(_) => { + err_unrec("Couldn't dump packages from database".to_string()) + } + } + return dat_pkgs; +} + pub fn stat_get_value(pkg: &str, sear_value: &str) -> bool { let file = format!("{}/.local/share/ame/aur_pkgs.db", env::var("HOME").unwrap()); let connection = sqlite::open(file).unwrap(); @@ -42,11 +65,7 @@ pub fn stat_get_value(pkg: &str, sear_value: &str) -> bool { Ok(_) => {}, Err(_) => err_unrec("Couldn't get value from database".to_string()), } - if return_val == true { - return true; - } else { - return false; - } + return return_val == true; }, "update" => { let result = connection.iterate(format!("SELECT pin FROM static_pkgs WHERE name = \"{}\";",&pkg), |pairs| { @@ -60,11 +79,7 @@ pub fn stat_get_value(pkg: &str, sear_value: &str) -> bool { Ok(_) => {}, Err(_) => err_unrec("Couldn't get value from database".to_string()), } - if return_val == true { - return true; - } else { - return false; - } + return return_val == true }, _ => { return_val = false @@ -76,7 +91,7 @@ pub fn stat_get_value(pkg: &str, sear_value: &str) -> bool { pub fn stat_rem_pkg(static_pkgs: &Vec) { let file = format!("{}/.local/share/ame/aur_pkgs.db", env::var("HOME").unwrap()); let connection = sqlite::open(file).unwrap(); - + print!("{:?}",static_pkgs); for i in static_pkgs { let result = connection.execute( format!(" @@ -88,7 +103,7 @@ pub fn stat_rem_pkg(static_pkgs: &Vec) { inf(format!("Removed {} from database", i)) } Err(_) => { - err_unrec(format!("Couldn't remove {} from database", i)) + err_unrec(format!("Couldn't remove {} from database (static packages table)", i)) } } } diff --git a/src/mods/statpkgs.rs b/src/mods/statpkgs.rs index ae5b9dd..989763d 100644 --- a/src/mods/statpkgs.rs +++ b/src/mods/statpkgs.rs @@ -2,10 +2,14 @@ use std::{fs, env}; use crate::inf; use toml::{Value, toml}; use crate::{ + err_rec, stat_add_pkg, stat_create_database, stat_get_value, stat_rem_pkg, + inssort, + stat_dump_dat, + uninstall, }; pub fn rebuild(noconfirm: bool) { @@ -13,7 +17,7 @@ pub fn rebuild(noconfirm: bool) { let file = format!("{}/.config/ame/pkgs.toml", env::var("HOME").unwrap()); let mut database = String::new(); database = fs::read_to_string(&file).expect("Can't Open Database"); - inf(format!("installing crystal config")); + inf("installing crystal config".to_string()); let db_parsed = database.parse::().expect("Invalid Database"); let mut pkgs = Vec::new(); @@ -27,11 +31,51 @@ pub fn rebuild(noconfirm: bool) { pkgs.push(tempvec); } } - let mut pkgs_to_add: Vec = Vec::new(); + let mut pkgs_to_add: Vec> = Vec::new(); + let mut pkgs_to_install: Vec = Vec::new(); for i in pkgs { if !stat_get_value(&i[0], "name") { - pkgs_to_add.push(i[0].to_string()); + let mut tempvec = Vec::new(); + tempvec.push(i[0].to_string()); + tempvec.push(i[1].to_string()); + pkgs_to_add.push(tempvec); + pkgs_to_install.push(i[0].to_string()); + } + } + let mut config_no_change = 0; + if pkgs_to_install.len() > 0 { + inf(format!("Installing {}", pkgs_to_install.join(", "))); + inssort(noconfirm, false, pkgs_to_install); + for i in pkgs_to_add { + stat_add_pkg(&i[1], &i[0]); } + config_no_change += 1; + } + let dat_pkgs = stat_dump_dat(); + + let mut pkgs = Vec::new(); + for entry in db_parsed.as_table() { + for (key, value) in &*entry { + pkgs.push(key); + } + } + + let mut pkgs_to_remove: Vec = Vec::new(); + for i in dat_pkgs { + if !pkgs.contains(&&i) { + pkgs_to_remove.push(i.to_string()); + } + config_no_change += 1; + } + if pkgs_to_remove.len() > 0 { + inf(format!("Removing {}", pkgs_to_remove.join(", "))); + stat_rem_pkg(&pkgs_to_remove); + uninstall(noconfirm, pkgs_to_remove); + } + + if config_no_change != 0 { + inf("Rebuild Complete".to_string()); + } else { + err_rec("Configuration not changed!".to_string()); } - inf(format!("Installing {}", pkgs_to_add.join(", "))); } \ No newline at end of file