From 60e424bdb07a94de5ecddac9dc2c92c9412a4fac Mon Sep 17 00:00:00 2001 From: noah the goodra Date: Wed, 30 Aug 2017 07:45:45 -0500 Subject: [PATCH] add cleanup command (#1648) --- libexec/scoop-cleanup.ps1 | 80 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 libexec/scoop-cleanup.ps1 diff --git a/libexec/scoop-cleanup.ps1 b/libexec/scoop-cleanup.ps1 new file mode 100644 index 000000000..0abec656b --- /dev/null +++ b/libexec/scoop-cleanup.ps1 @@ -0,0 +1,80 @@ +# Usage: scoop cleanup [options] +# Summary: Update apps, or Scoop itself +# Help: 'scoop cleanup' cleans Scoop apps by removing old versions. +# 'scoop cleanup ' cleans up the old versions of that app if said versions exist. +# +# You can use '*' in place of to cleanup all apps. +# +# Options: +# --global, -g Cleanup a globally installed app +. "$psscriptroot\..\lib\core.ps1" +. "$psscriptroot\..\lib\manifest.ps1" +. "$psscriptroot\..\lib\buckets.ps1" +. "$psscriptroot\..\lib\versions.ps1" +. "$psscriptroot\..\lib\getopt.ps1" + +reset_aliases + +$opt, $apps, $err = getopt $args 'g' 'global' +if ($err) { "scoop cleanup: $err"; exit 1 } +$global = $opt.g -or $opt.global + +function cleanup($app, $global) { + $current_version = current_version $app $global + $installedappdir = appdir $app $global + foreach($versionDir in (Get-ChildItem $installedappdir)) + { + if(($versionDir.Name -ne $current_version) -and ($versionDir.Name -ne "current")) + { + foreach($file in (Get-ChildItem $installedappdir/$versionDir)) + { + if($file.LinkType -ne $null) + { + fsutil.exe reparsepoint delete $file.FullName + } + } + Remove-Item $versionDir.FullName -Recurse -Force + } + } +} + +function ensure_all_installed($apps, $global) { + $app = $apps | Where-Object { !(installed $_ $global) } | Select-Object -first 1 # just get the first one that's not installed + if ($app) { + if (installed $app (!$global)) { + function wh($g) { if ($g) { "globally" } else { "for your account" } } + write-host "'$app' isn't installed $(wh $global), but it is installed $(wh (!$global))." -f darkred + "Try cleaning $(if($global) { 'without' } else { 'with' }) the --global (or -g) flag instead." + exit 1 + } + else { + abort "'$app' isn't installed." + } + } +} + +# convert list of apps to list of ($app, $global) tuples +function applist($apps, $global) { + return , @($apps | % { , @($_, $global) }) +} +if($apps) { + if ($global -and !(is_admin)) { + 'ERROR: You need admin rights to update global apps.'; exit 1 + } + + if ($apps -eq '*') { + $apps = applist (installed_apps $false) $false + if ($global) { + $apps += applist (installed_apps $true) $true + } + } + else { + ensure_all_installed $apps $global + $apps = applist $apps $global + } + + # $apps is now a list of ($app, $global) tuples + $apps | ForEach-Object { cleanup @_ } +} + +exit 0