From 978d276c184843b19de7843de8aadb97c6812e3c Mon Sep 17 00:00:00 2001 From: Michal S Date: Mon, 22 Aug 2022 15:54:50 +0100 Subject: [PATCH] Further split of aur_install --- src/internal/initialise.rs | 15 ++++++ src/main.rs | 5 +- src/operations/aur_install.rs | 86 ++++++++++++++++++++++++++++------- src/operations/uninstall.rs | 26 +---------- 4 files changed, 88 insertions(+), 44 deletions(-) diff --git a/src/internal/initialise.rs b/src/internal/initialise.rs index 00ae125..5c58549 100644 --- a/src/internal/initialise.rs +++ b/src/internal/initialise.rs @@ -23,6 +23,21 @@ pub fn init(options: Options) { }); } + // If cache dir doesn't exist, create it + if !Path::new(&format!("{}/.cache/ame", homedir)).exists() { + if verbosity >= 1 { + log!("Initialising cache directory"); + } + std::fs::create_dir_all(format!("{}/.cache/ame", homedir)).unwrap_or_else(|e| { + crash!( + AppExitCode::FailedCreatingPaths, + "Couldn't create path: {}/.cache/ame: {}", + homedir, + e, + ); + }); + } + // If config dir doesn't exist, create it if !Path::new(&format!("{}/.config/ame/", homedir)).exists() { if verbosity >= 1 { diff --git a/src/main.rs b/src/main.rs index 1247dec..97bd132 100644 --- a/src/main.rs +++ b/src/main.rs @@ -129,7 +129,8 @@ fn cmd_install(args: InstallArgs, options: Options, cachedir: &str) { let out = std::process::Command::new("expac") .args(&["-Q", "-l", "\n ", " %O", &p]) .output() - .unwrap().stdout; + .unwrap() + .stdout; let out = String::from_utf8(out).unwrap().trim().to_string(); if !out.is_empty() { info!("{}:", p); @@ -146,7 +147,7 @@ fn cmd_remove(args: RemoveArgs, options: Options) { info!("Uninstalling packages: {}", &packages.join(", ")); // Remove packages - operations::uninstall(packages, options); + operations::uninstall(&packages, options); } fn cmd_search(args: &SearchArgs, options: Options) { diff --git a/src/operations/aur_install.rs b/src/operations/aur_install.rs index 1b1936a..cdc8588 100644 --- a/src/operations/aur_install.rs +++ b/src/operations/aur_install.rs @@ -10,6 +10,8 @@ use crate::internal::exit_code::AppExitCode; use crate::internal::rpc::rpcinfo; use crate::{crash, info, log, prompt, warn, Options}; +const AUR_CACHE: &str = ".cache/ame"; + fn list(dir: &str) -> Vec { let dirs = fs::read_dir(Path::new(&dir)).unwrap(); let dirs: Vec = dirs @@ -120,7 +122,13 @@ fn finish(cachedir: &str, pkg: &str, options: &Options) { .args(&[ "-cO", "extglob", - format!("sudo pacman -U --asdeps {}/!({})/*.zst {}", cachedir, pkg, if options.noconfirm { "--noconfirm" } else { "" }).as_str(), + format!( + "sudo pacman -U --asdeps {}/!({})/*.zst {}", + cachedir, + pkg, + if options.noconfirm { "--noconfirm" } else { "" } + ) + .as_str(), ]) .spawn() .unwrap() @@ -129,7 +137,10 @@ fn finish(cachedir: &str, pkg: &str, options: &Options) { if cmd.success() { info!("All AUR dependencies installed"); } else { - crash!(AppExitCode::PacmanError, "AUR dependencies failed to install"); + crash!( + AppExitCode::PacmanError, + "AUR dependencies failed to install" + ); } } @@ -138,7 +149,13 @@ fn finish(cachedir: &str, pkg: &str, options: &Options) { let cmd = std::process::Command::new("bash") .args(&[ "-c", - format!("sudo pacman -U {}/{}/*.zst {}", cachedir, pkg, if options.noconfirm { "--noconfirm" } else { "" }).as_str(), + format!( + "sudo pacman -U {}/{}/*.zst {}", + cachedir, + pkg, + if options.noconfirm { "--noconfirm" } else { "" } + ) + .as_str(), ]) .spawn() .unwrap() @@ -151,9 +168,40 @@ fn finish(cachedir: &str, pkg: &str, options: &Options) { } } +fn clone(pkg: &String, pkgcache: &str) { + let url = crate::internal::rpc::URL; + + // See if package is already cloned to AUR_CACHE + let dirs = list(pkgcache); + println!("{:?}", dirs); + if dirs.contains(pkg) { + // Enter directory and git pull + info!("Updating cached package source"); + set_current_dir(Path::new(&format!( + "{}/{}/{}", + env::var("HOME").unwrap(), + AUR_CACHE, + pkg + ))) + .unwrap(); + ShellCommand::git() + .arg("pull") + .wait() + .silent_unwrap(AppExitCode::GitError); + } else { + // Clone package into cachedir + info!("Cloning package source"); + set_current_dir(Path::new(&pkgcache)).unwrap(); + ShellCommand::git() + .arg("clone") + .arg(format!("{}/{}", url, pkg)) + .wait() + .silent_unwrap(AppExitCode::GitError); + } +} + pub fn aur_install(a: Vec, options: Options, orig_cachedir: &str) { // Initialise variables - let url = crate::internal::rpc::URL; let cachedir = if options.asdeps || !orig_cachedir.is_empty() { orig_cachedir.to_string() } else { @@ -188,25 +236,29 @@ pub fn aur_install(a: Vec, options: Options, orig_cachedir: &str) { // Get package name let pkg = &rpcres.package.as_ref().unwrap().name; + let pkgcache = format!("{}/{}", env::var("HOME").unwrap(), AUR_CACHE); + if verbosity >= 1 { log!("Cloning {} into cachedir", pkg); } - info!("Cloning package source"); // Clone package into cachedir - set_current_dir(Path::new(&cachedir)).unwrap(); - ShellCommand::git() - .arg("clone") - .arg(format!("{}/{}", url, pkg)) + clone(pkg, &pkgcache); + + // Copy package from AUR_CACHE to cachedir + Command::new("cp") + .arg("-r") + .arg(format!( + "{}/{}/{}", + env::var("HOME").unwrap(), + AUR_CACHE, + pkg + )) + .arg(format!("{}/{}", cachedir, pkg)) + .spawn() + .unwrap() .wait() - .silent_unwrap(AppExitCode::GitError); - if verbosity >= 1 { - log!( - "Cloned {} into cachedir, moving on to resolving dependencies: {:?}", - pkg, - rpcres.package - ); - } + .unwrap(); // Sort dependencies and makedepends if verbosity >= 1 { diff --git a/src/operations/uninstall.rs b/src/operations/uninstall.rs index 544acd1..d0e5e07 100644 --- a/src/operations/uninstall.rs +++ b/src/operations/uninstall.rs @@ -1,12 +1,9 @@ -use std::path::Path; -use std::{env, fs}; - use crate::internal::commands::ShellCommand; use crate::internal::error::SilentUnwrap; use crate::internal::exit_code::AppExitCode; use crate::{log, Options}; -pub fn uninstall(packages: Vec, options: Options) { +pub fn uninstall(packages: &[String], options: Options) { // Build pacman args let mut pacman_args = vec!["-Rs"]; pacman_args.append(&mut packages.iter().map(String::as_str).collect()); @@ -28,25 +25,4 @@ pub fn uninstall(packages: Vec, options: Options) { if verbosity >= 1 { log!("Uninstalling packages: {:?} exited with code 0", &packages); } - - for package in packages { - // Remove old cache directory - if Path::new(&format!( - "{}/.cache/ame/{}", - env::var("HOME").unwrap(), - package - )) - .exists() - { - if verbosity >= 1 { - log!("Old cache directory found, deleting"); - } - fs::remove_dir_all(Path::new(&format!( - "{}/.cache/ame/{}", - env::var("HOME").unwrap(), - package - ))) - .unwrap(); - } - } }