-
Notifications
You must be signed in to change notification settings - Fork 18
/
Clean-Old-DotNet6-Previews.ps1
104 lines (90 loc) · 2.42 KB
/
Clean-Old-DotNet6-Previews.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
param (
[Parameter(HelpMessage="DotNet SDK Root install directory")]
[string]$DotnetRoot = $env:DOTNET_ROOT,
[Parameter(HelpMessage="Deletes SDK/Runtime files as well - only use this if you know what you are doing!")]
[switch]$FullDelete = $false
)
$psMajorVersion = $host.Version.Major
if ($psMajorVersion -lt 7)
{
throw "This script requires PowerShell v7 or newer."
}
$requireAdmin = $false
# Otherwise look for the default paths
if (-not ($DotnetRoot))
{
# Admin / sudo required for global installs
$requireAdmin = $true
if ($IsMacOS)
{
$DotnetRoot = '/usr/local/share/dotnet/'
} elseif ($IsWindows)
{
$DotnetRoot = Join-Path -Path $env:ProgramFiles -ChildPath 'dotnet'
}
}
# Make sure the SDK path exists
if (-not (Test-Path $DotnetRoot))
{
throw "dotnet SDK root not found"
}
# If modifying a global install we need admin/sudo, so check the context if we are
if ($requireAdmin)
{
if ($IsWindows)
{
if (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{
throw "Adminstrator privilege required to modify global dotnet install. Re-run this script under an elevated terminal."
}
}
else
{
if (-NOT ($(whoami) -eq 'root'))
{
throw "Superuser privilege required to modify global dotnet install. Re-run this script with sudo."
}
}
}
$rmPaths = @(
'packs/Microsoft.Android.*',
'packs/Microsoft.iOS.*',
'packs/Microsoft.MacCatalyst.*',
'packs/Microsoft.macOS.*',
'packs/Microsoft.Maui.*',
'packs/Microsoft.NET.Runtime.*',
'packs/Microsoft.NETCore.App.Runtime.AOT.*',
'packs/Microsoft.NETCore.App.Runtime.Mono.*',
'packs/Microsoft.tvOS.*',
'templates/6.0*',
'metadata/*'
)
# Delete ALL dotnet6 preview files including host runtime and sdk
if ($FullDelete)
{
# macOS stores some shared bits in a .app file and windows does not use this extension
$osAppExt = ''
if ($IsMacOS)
{
$osAppExt = '.app'
}
$rmPaths += @(
'sdk-manifests/6.0.*',
'host/fxr/6.0*',
'sdk/6.0*',
"shared/Microsoft.AspNetCore.App$osAppExt/6.0*",
"shared/Microsoft.NETCore.App$osAppExt/6.0*"
)
}
foreach ($rmPath in $rmPaths)
{
Remove-Item -Recurse -Force -Path (Join-Path -Path $DotnetRoot -ChildPath $rmPath)
}
if (Test-Path ~/.templateengine)
{
Remove-Item -Recurse -Force -Path ~/.templateengine
}
if (Test-Path ~/.dotnet/sdk-advertising)
{
Remove-Item -Recurse -Force -Path "~/.dotnet/sdk-advertising/6.0*"
}