-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathscoop-uninstall.ps1
149 lines (124 loc) · 4.57 KB
/
scoop-uninstall.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# Usage: scoop uninstall <app> [options]
# Summary: Uninstall an app
# Help: e.g. scoop uninstall git
#
# Options:
# -g, --global Uninstall a globally installed app
# -p, --purge Remove all persistent data
. "$PSScriptRoot\..\lib\getopt.ps1"
. "$PSScriptRoot\..\lib\manifest.ps1" # 'Get-Manifest' 'Select-CurrentVersion' (indirectly)
. "$PSScriptRoot\..\lib\install.ps1"
. "$PSScriptRoot\..\lib\shortcuts.ps1"
. "$PSScriptRoot\..\lib\psmodules.ps1"
. "$PSScriptRoot\..\lib\versions.ps1" # 'Select-CurrentVersion'
# options
$opt, $apps, $err = getopt $args 'gp' 'global', 'purge'
if ($err) {
error "scoop uninstall: $err"
exit 1
}
$global = $opt.g -or $opt.global
$purge = $opt.p -or $opt.purge
if (!$apps) {
error '<app> missing'
my_usage
exit 1
}
if ($global -and !(is_admin)) {
error 'You need admin rights to uninstall global apps.'
exit 1
}
if ($apps -eq 'scoop') {
& "$PSScriptRoot\..\bin\uninstall.ps1" $global $purge
exit
}
$apps = Confirm-InstallationStatus $apps -Global:$global
if (!$apps) { exit 0 }
:app_loop foreach ($_ in $apps) {
($app, $global) = $_
$version = Select-CurrentVersion -AppName $app -Global:$global
$appDir = appdir $app $global
if ($version) {
Write-Host "Uninstalling '$app' ($version)."
$dir = versiondir $app $version $global
$persist_dir = persistdir $app $global
$manifest = installed_manifest $app $version $global
$install = install_info $app $version $global
$architecture = $install.architecture
Invoke-HookScript -HookType 'pre_uninstall' -Manifest $manifest -Arch $architecture
#region Workaround for #2952
if (test_running_process $app $global) {
continue
}
#endregion Workaround for #2952
try {
Test-Path $dir -ErrorAction Stop | Out-Null
} catch [UnauthorizedAccessException] {
error "Access denied: $dir. You might need to restart."
continue
}
run_uninstaller $manifest $architecture $dir
rm_shims $app $manifest $global $architecture
rm_startmenu_shortcuts $manifest $global $architecture
# If a junction was used during install, that will have been used
# as the reference directory. Otherwise it will just be the version
# directory.
$refdir = unlink_current $dir
uninstall_psmodule $manifest $refdir $global
env_rm_path $manifest $refdir $global $architecture
env_rm $manifest $global $architecture
try {
# unlink all potential old link before doing recursive Remove-Item
unlink_persist_data $manifest $dir
Remove-Item $dir -Recurse -Force -ErrorAction Stop
} catch {
if (Test-Path $dir) {
error "Couldn't remove '$(friendly_path $dir)'; it may be in use."
continue
}
}
Invoke-HookScript -HookType 'post_uninstall' -Manifest $manifest -Arch $architecture
}
# remove older versions
$oldVersions = @(Get-ChildItem $appDir -Name -Exclude 'current')
foreach ($version in $oldVersions) {
Write-Host "Removing older version ($version)."
$dir = versiondir $app $version $global
try {
# unlink all potential old link before doing recursive Remove-Item
unlink_persist_data $manifest $dir
Remove-Item $dir -Recurse -Force -ErrorAction Stop
} catch {
error "Couldn't remove '$(friendly_path $dir)'; it may be in use."
continue app_loop
}
}
if (Test-Path ($currentDir = Join-Path $appDir 'current')) {
attrib $currentDir -R /L
Remove-Item $currentDir -ErrorAction Stop -Force
}
if (!(Get-ChildItem $appDir)) {
try {
# if last install failed, the directory seems to be locked and this
# will throw an error about the directory not existing
Remove-Item $appdir -Recurse -Force -ErrorAction Stop
} catch {
if ((Test-Path $appdir)) { throw } # only throw if the dir still exists
}
}
# purge persistant data
if ($purge) {
Write-Host 'Removing persisted data.'
$persist_dir = persistdir $app $global
if (Test-Path $persist_dir) {
try {
Remove-Item $persist_dir -Recurse -Force -ErrorAction Stop
} catch {
error "Couldn't remove '$(friendly_path $persist_dir)'; it may be in use."
continue
}
}
}
success "'$app' was uninstalled."
}
exit 0