forked from ScoopInstaller/Scoop
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scoop-cleanup.ps1
91 lines (79 loc) · 2.82 KB
/
scoop-cleanup.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# 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.
#
# You can use '*' in place of <app> to cleanup all apps.
#
# Options:
# -g, --global Cleanup a globally installed app
# -k, --cache Remove outdated download cache
. "$psscriptroot\..\lib\core.ps1"
. "$psscriptroot\..\lib\manifest.ps1"
. "$psscriptroot\..\lib\buckets.ps1"
. "$psscriptroot\..\lib\versions.ps1"
. "$psscriptroot\..\lib\getopt.ps1"
. "$psscriptroot\..\lib\help.ps1"
. "$psscriptroot\..\lib\install.ps1"
reset_aliases
$opt, $apps, $err = getopt $args 'gk' 'global', 'cache'
if ($err) { "scoop cleanup: $err"; exit 1 }
$global = $opt.g -or $opt.global
$cache = $opt.k -or $opt.cache
if (!$apps) { 'ERROR: <app> missing'; my_usage; exit 1 }
if ($global -and !(is_admin)) {
'ERROR: you need admin rights to cleanup global apps'; exit 1
}
function cleanup($app, $global, $verbose, $cache) {
$current_version = Select-CurrentVersion -AppName $app -Global:$global
if ($cache) {
Remove-Item "$cachedir\$app#*" -Exclude "$app#$current_version#*"
}
$appDir = appdir $app $global
$versions = Get-ChildItem $appDir -Name
$versions = $versions | Where-Object { $current_version -ne $_ -and $_ -ne 'current' }
if (!$versions) {
if ($verbose) { success "$app is already clean" }
return
}
Write-Host -f yellow "Removing $app`:" -NoNewline
$versions | ForEach-Object {
$version = $_
Write-Host " $version" -NoNewline
$dir = versiondir $app $version $global
# unlink all potential old link before doing recursive Remove-Item
unlink_persist_data $dir
Remove-Item $dir -ErrorAction Stop -Recurse -Force
}
$leftVersions = Get-ChildItem $appDir
if ($leftVersions.Length -eq 1 -and $leftVersions.Name -eq 'current' -and $leftVersions.LinkType) {
attrib $leftVersions.FullName -R /L
Remove-Item $leftVersions.FullName -ErrorAction Stop -Force
$leftVersions = $null
}
if (!$leftVersions) {
Remove-Item $appDir -ErrorAction Stop -Force
}
Write-Host ''
}
if ($apps) {
if ($apps -eq '*') {
$verbose = $false
$apps = applist (installed_apps $false) $false
if ($global) {
$apps += applist (installed_apps $true) $true
}
} else {
$verbose = $true
$apps = Confirm-InstallationStatus $apps -Global:$global
}
# $apps is now a list of ($app, $global) tuples
$apps | ForEach-Object { cleanup @_ $verbose $cache }
if ($cache) {
Remove-Item "$cachedir\*.download" -ErrorAction Ignore
}
if (!$verbose) {
success 'Everything is shiny now!'
}
}
exit 0