@ -1,10 +1,11 @@
use crate ::args ::UpgradeArgs ;
use crate ::internal ::commands ::ShellCommand ;
use crate ::internal ::commands ::ShellCommand ;
use crate ::internal ::detect ;
use crate ::internal ::detect ;
use crate ::internal ::error ::SilentUnwrap ;
use crate ::internal ::error ::SilentUnwrap ;
use crate ::internal ::exit_code ::AppExitCode ;
use crate ::internal ::exit_code ::AppExitCode ;
use crate ::internal ::rpc ::rpcinfo ;
use crate ::internal ::rpc ::rpcinfo ;
use crate ::operations ::aur_install ::aur_install ;
use crate ::operations ::aur_install ::aur_install ;
use crate ::{ info , log , prompt , Options} ;
use crate ::{ info , log , prompt , spinner, warn , Options} ;
#[ derive(Debug) ]
#[ derive(Debug) ]
struct QueriedPackage {
struct QueriedPackage {
@ -12,36 +13,49 @@ struct QueriedPackage {
pub version : String ,
pub version : String ,
}
}
pub fn upgrade ( options : Options ) {
pub fn upgrade ( options : Options , args : UpgradeArgs ) {
// Initialise variables
// Initialise variables
let verbosity = options . verbosity ;
let verbosity = options . verbosity ;
let noconfirm = options . noconfirm ;
let noconfirm = options . noconfirm ;
// Build pacman args
let args = if ! args . aur & & ! args . repo {
let mut pacman_args = vec! [ "-Syu" ] ;
UpgradeArgs {
if noconfirm {
aur : true ,
pacman_args . push ( "--noconfirm" ) ;
repo : true ,
}
}
} else {
args
} ;
if args . repo {
// Build pacman args
let mut pacman_args = vec! [ "-Syu" ] ;
if noconfirm {
pacman_args . push ( "--noconfirm" ) ;
}
if verbosity > = 1 {
if verbosity > = 1 {
log ! ( "Upgrading repo packages" ) ;
log ! ( "Upgrading repo packages" ) ;
}
}
// Upgrade repo packages
// Upgrade repo packages
let pacman_result = ShellCommand ::pacman ( )
let pacman_result = ShellCommand ::pacman ( )
. elevated ( )
. elevated ( )
. args ( pacman_args )
. args ( pacman_args )
. wait ( )
. wait ( )
. silent_unwrap ( AppExitCode ::PacmanError ) ;
. silent_unwrap ( AppExitCode ::PacmanError ) ;
if pacman_result . success ( ) {
// If pacman was successful, notify user
info ! ( "Successfully upgraded repo packages" ) ;
} else {
// Otherwise warn user
warn ! ( "Failed to upgrade repo packages." , ) ;
}
}
if pacman_result . success ( ) {
if args . repo & & args . aur {
// If pacman was successful, notify user
let cont = prompt ! ( default false , "Continue to upgrade AUR packages?" ) ;
info ! ( "Successfully upgraded repo packages" ) ;
} else {
// Otherwise, prompt user whether to continue
let cont = prompt ! ( default false ,
"Failed to upgrade repo packages, continue to upgrading AUR packages?" ,
) ;
if ! cont {
if ! cont {
// If user doesn't want to continue, break
// If user doesn't want to continue, break
info ! ( "Exiting" ) ;
info ! ( "Exiting" ) ;
@ -49,69 +63,76 @@ pub fn upgrade(options: Options) {
}
}
}
}
if verbosity > = 1 {
if args . aur {
log ! ( "Checking AUR upgrades..." ) ;
}
// List non-native packages using `pacman -Qm` and collect to a Vec<String>
let non_native = ShellCommand ::pacman ( )
. arg ( "-Qm" )
. args ( & [ "--color" , "never" ] )
. wait_with_output ( )
. silent_unwrap ( AppExitCode ::PacmanError ) ;
// Collect by lines to a Vec<String>
let mut non_native = non_native . stdout . split ( '\n' ) . collect ::< Vec < & str > > ( ) ;
// Remove last element, which is an empty line
non_native . pop ( ) ;
// Parse non-native packages into a Vec<QueriedPackage>
let mut parsed_non_native : Vec < QueriedPackage > = vec! [ ] ;
for pkg in non_native {
// Split by space
let split = pkg . split ( ' ' ) . collect ::< Vec < & str > > ( ) ;
if verbosity > = 1 {
if verbosity > = 1 {
log ! ( " {:?}", split ) ;
log ! ( "Checking AUR upgrades..." ) ;
}
}
// Create QueriedPackage and push it to parsed_non_native
let name = split [ 0 ] . to_string ( ) ;
let version = split [ 1 ] . to_string ( ) ;
parsed_non_native . push ( QueriedPackage { name , version } ) ;
}
if verbosity > = 1 {
log ! ( "{:?}" , & parsed_non_native ) ;
}
// Check if AUR package versions are the same as installed
// Start spinner
let mut aur_upgrades = vec! [ ] ;
let sp = spinner ! ( "Checking AUR upgrades..." ) ;
for pkg in parsed_non_native {
// Query AUR
// List non-native packages using `pacman -Qm` and collect to a Vec<String>
let rpc_result = rpcinfo ( ( & * pkg . name ) . to_string ( ) ) ;
let non_native = ShellCommand ::pacman ( )
. arg ( "-Qm" )
. args ( & [ "--color" , "never" ] )
. wait_with_output ( )
. silent_unwrap ( AppExitCode ::PacmanError ) ;
// Collect by lines to a Vec<String>
let mut non_native = non_native . stdout . split ( '\n' ) . collect ::< Vec < & str > > ( ) ;
// Remove last element, which is an empty line
non_native . pop ( ) ;
// Parse non-native packages into a Vec<QueriedPackage>
let mut parsed_non_native : Vec < QueriedPackage > = vec! [ ] ;
for pkg in non_native {
// Split by space
let split = pkg . split ( ' ' ) . collect ::< Vec < & str > > ( ) ;
if verbosity > = 1 {
log ! ( "{:?}" , split ) ;
}
// Create QueriedPackage and push it to parsed_non_native
let name = split [ 0 ] . to_string ( ) ;
let version = split [ 1 ] . to_string ( ) ;
parsed_non_native . push ( QueriedPackage { name , version } ) ;
}
if ! rpc_result . found {
if verbosity > = 1 {
// If package not found, skip
log ! ( "{:?}" , & parsed_non_native ) ;
continue ;
}
}
// If versions differ, push to a vector
// Check if AUR package versions are the same as installed
if rpc_result . package . unwrap ( ) . version ! = pkg . version {
let mut aur_upgrades = vec! [ ] ;
aur_upgrades . push ( pkg . name ) ;
for pkg in parsed_non_native {
// Query AUR
let rpc_result = rpcinfo ( ( & * pkg . name ) . to_string ( ) ) ;
if ! rpc_result . found {
// If package not found, skip
continue ;
}
// If versions differ, push to a vector
if rpc_result . package . unwrap ( ) . version ! = pkg . version {
aur_upgrades . push ( pkg . name ) ;
}
}
}
}
// If vector isn't empty, prompt to install AUR packages from vector, effectively upgrading
sp . stop_bold ( "Finished!" ) ;
if ! aur_upgrades . is_empty ( ) {
let cont = prompt ! ( default false ,
// If vector isn't empty, prompt to install AUR packages from vector, effectively upgrading
"Found AUR packages {} have new versions available, upgrade?" ,
if ! aur_upgrades . is_empty ( ) {
aur_upgrades . join ( ", " ) ,
let cont = prompt ! ( default true ,
) ;
"Found AUR packages {} have new versions available, upgrade?" ,
if cont {
aur_upgrades . join ( ", " ) ,
aur_install ( aur_upgrades , options ) ;
) ;
} ;
if cont {
} else {
aur_install ( aur_upgrades , options ) ;
info ! ( "No upgrades available for installed AUR packages" ) ;
} ;
} else {
info ! ( "No upgrades available for installed AUR packages" ) ;
}
}
}
// Check for .pacnew files
// Check for .pacnew files