/
uninstall.ps1
115 lines (103 loc) · 3.5 KB
/
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
# This script will remove the Bun installation at the location of this
# script, removing it from %PATH%, deleting caches, and removing it from
# the list of installed programs.
param(
[switch]$PauseOnError = $false
)
$ErrorActionPreference = "Stop"
# These two environment functions are roughly copied from https://github.com/prefix-dev/pixi/pull/692
# They are used instead of `SetEnvironmentVariable` because of unwanted variable expansions.
function Write-Env {
param([String]$Key, [String]$Value)
$RegisterKey = Get-Item -Path 'HKCU:'
$EnvRegisterKey = $RegisterKey.OpenSubKey('Environment', $true)
if ($null -eq $Value) {
$EnvRegisterKey.DeleteValue($Key)
} else {
$RegistryValueKind = if ($Value.Contains('%')) {
[Microsoft.Win32.RegistryValueKind]::ExpandString
} elseif ($EnvRegisterKey.GetValue($Key)) {
$EnvRegisterKey.GetValueKind($Key)
} else {
[Microsoft.Win32.RegistryValueKind]::String
}
$EnvRegisterKey.SetValue($Key, $Value, $RegistryValueKind)
}
}
function Get-Env {
param([String] $Key)
$RegisterKey = Get-Item -Path 'HKCU:'
$EnvRegisterKey = $RegisterKey.OpenSubKey('Environment')
$EnvRegisterKey.GetValue($Key, $null, [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames)
}
if (-not (Test-Path "${PSScriptRoot}\bin\bun.exe")) {
Write-Host "bun.exe not found in ${PSScriptRoot}\bin`n`nRefusing to delete this directory as it may.`n`nIf this uninstallation is still intentional, please just manually delete this folder."
if ($PauseOnError) { pause }
exit 1
}
function Stop-Bun {
try {
Get-Process -Name bun | Where-Object { $_.Path -eq "${PSScriptRoot}\bin\bun.exe" } | Stop-Process -Force
} catch [Microsoft.PowerShell.Commands.ProcessCommandException] {
# ignore
} catch {
Write-Host "There are open instances of bun.exe that could not be automatically closed."
if ($PauseOnError) { pause }
exit 1
}
}
# Remove ~\.bun\bin\bun.exe
try {
Stop-Bun
Remove-Item "${PSScriptRoot}\bin\bun.exe" -Force
} catch {
# Try a second time
Stop-Bun
Start-Sleep -Seconds 1
try {
Remove-Item "${PSScriptRoot}\bin\bun.exe" -Force
} catch {
Write-Host $_
Write-Host "`n`nCould not delete ${PSScriptRoot}\bin\bun.exe."
Write-Host "Please close all instances of bun.exe and try again."
if ($PauseOnError) { pause }
exit 1
}
}
# Remove ~\.bun
try {
Remove-Item "${PSScriptRoot}" -Recurse -Force
} catch {
Write-Host "Could not delete ${PSScriptRoot}."
if ($PauseOnError) { pause }
exit 1
}
# Delete some tempdir files. Do not fail if an error happens here
try {
Remove-Item "${Temp}\bun-*" -Recurse -Force
} catch {}
try {
Remove-Item "${Temp}\bunx-*" -Recurse -Force
} catch {}
# Remove Entry from path
try {
$Path = Get-Env -Key 'Path'
$Path = $Path -split ';'
$Path = $Path | Where-Object { $_ -ne "${PSScriptRoot}\bin" }
Write-Env -Key 'Path' -Value ($Path -join ';')
} catch {
Write-Host "Could not remove ${PSScriptRoot}\bin from PATH."
Write-Error $_
if ($PauseOnError) { pause }
exit 1
}
# Remove Entry from Windows Installer, if it is owned by this installation.
try {
$item = Get-Item "HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\Bun";
$location = $item.GetValue("InstallLocation");
if ($location -eq "${PSScriptRoot}") {
Remove-Item "HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\Bun" -Recurse
}
} catch {
# unlucky tbh
}