Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup: added option -n/--keep to keep more than one old version (#5382) #5441

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions libexec/scoop-cleanup.ps1
Original file line number Diff line number Diff line change
@@ -1,25 +1,34 @@
# Usage: scoop cleanup <app> [options]
# Summary: Cleanup apps by removing old versions
# Help: 'scoop cleanup' cleans Scoop apps by removing old versions.
# 'scoop cleanup <app>' cleans up the old versions of that app if said versions exist.
# 'scoop cleanup <app> -n:1' cleans up the old versions of that app if said versions exist,
# but keeps n versions, default 1
#
# You can use '*' in place of <app> or `-a`/`--all` switch to cleanup all apps.
#
# Options:
# -a, --all Cleanup all apps (alternative to '*')
# -g, --global Cleanup a globally installed app
# -k, --cache Remove outdated download cache
# -n, --keep Option to keep more than one version

. "$PSScriptRoot\..\lib\getopt.ps1"
. "$PSScriptRoot\..\lib\manifest.ps1" # 'Select-CurrentVersion' (indirectly)
. "$PSScriptRoot\..\lib\versions.ps1" # 'Select-CurrentVersion'
. "$PSScriptRoot\..\lib\install.ps1" # persist related

$opt, $apps, $err = getopt $args 'agk' 'all', 'global', 'cache'
$opt, $apps, $err = getopt $args 'agkn:' 'all', 'global', 'cache', 'keep='
if ($err) { "scoop cleanup: $err"; exit 1 }
$global = $opt.g -or $opt.global
$cache = $opt.k -or $opt.cache
$all = $opt.a -or $opt.all
$keep = 1
if ($opt.n -gt 1) {
$keep = $opt.n
}
if ($opt.keep -gt 1) {
$keep = $opt.keep
}

if (!$apps -and !$all) { 'ERROR: <app> missing'; my_usage; exit 1 }

Expand All @@ -35,6 +44,7 @@ function cleanup($app, $global, $verbose, $cache) {
$appDir = appdir $app $global
$versions = Get-ChildItem $appDir -Name
$versions = $versions | Where-Object { $current_version -ne $_ -and $_ -ne 'current' }
$versions = $versions | Select-Object -first ([math]::max($versions.count - $keep + 1, 0))
if (!$versions) {
if ($verbose) { success "$app is already clean" }
return
Expand Down