-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall-spm-template.ps1
209 lines (174 loc) · 7.12 KB
/
install-spm-template.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# # Run a separate PowerShell process because the script calls exit, so it will end the current PowerShell session.
# &powershell -NoProfile -ExecutionPolicy unrestricted -Command "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; &([scriptblock]::Create((Invoke-WebRequest -UseBasicParsing 'https://dot.net/v1/dotnet-install.ps1'))) -Runtime dotnet"
$ErrorActionPreference = 'Stop'
$ProgressPreference = 'SilentlyContinue'
Function Get-InstalledDotnetCoreVersion
{
[CmdletBinding()]
[OutputType([PSObject])]
param()
[Hashtable] $SDKVersions = [ordered] @{}
[Hashtable] $RuntimeVersions = [ordered] @{}
[Hashtable] $FrameworkHostingVersions = [ordered] @{}
[Hashtable] $ServerHostingBundleVersions = [ordered] @{}
[Hashtable] $AspNetRuntimePackageStoreVersions = [ordered] @{}
try
{
[string] $RootDotnetPath = $PSVersionTable.Platform -eq "Unix" ? "/usr/share/dotnet" : (Join-Path -Path $env:programfiles -ChildPath 'dotnet')
[string] $SdkPath = Join-Path -Path $RootDotnetPath -ChildPath 'sdk'
[string] $RuntimePath = Join-Path -Path $RootDotnetPath -ChildPath 'shared\Microsoft.NETCore.App'
[string] $SFHPath = Join-Path -Path $RootDotnetPath -ChildPath 'host\fxr'
if (Test-Path -Path $RootDotnetPath -PathType Container)
{
if (Test-Path -Path $SdkPath -PathType Container)
{
Write-Verbose "$($SdkPath) was found. Enumerating versions now."
Get-ChildItem -Path $SdkPath -Directory | Where-Object { $_.Name -match '^\d.\d.\d' } `
| Sort-Object -Property Name | Foreach-Object { $SDKVersions.Add( $_.Name, $_.FullName ) }
}
if (Test-Path -Path $RuntimePath -PathType Container)
{
Write-Verbose "$($RuntimePath) was found. Enumerating versions now."
Get-ChildItem -Path $RuntimePath -Directory | Where-Object { $_.Name -match '^\d.\d.\d' } `
| Sort-Object -Property Name | Foreach-Object { $RuntimeVersions.Add( $_.Name, $_.FullName ) }
}
if (Test-Path -Path $SFHPath -PathType Container)
{
Write-Verbose "$($SFHPath) was found. Enumerating versions now."
Get-ChildItem -Path $SFHPath -Directory | Where-Object { $_ -match '^\d.\d.\d' } `
| Sort-Object -Property Name | Foreach-Object { $FrameworkHostingVersions.Add( $_.Name, $_.FullName ) }
}
}
$ServerHostingBundleVersions = Get-InstalledDotnetCoreBundles -BundleName 'WindowsServerHosting'
$AspNetRuntimePackageStoreVersions = Get-InstalledDotnetCoreBundles -BundleName 'ASPNetRuntimePackageStore'
[Hashtable] $Properties = @{ 'SDKVersions' = $SDKVersions;
'RuntimeVersions' = $RuntimeVersions;
'SharedFrameworkHostingVersions' = $FrameworkHostingVersions;
'WindowsServerHostingVersions' = $ServerHostingBundleVersions;
'AspNetRuntimePackageStoreVersions' = $AspNetRuntimePackageStoreVersions
}
$Result = New-Object -TypeName PSObject -Prop $Properties
return $Result
}
catch
{
throw $_
}
}
Function Get-InstalledDotnetCoreBundles
{
[CmdletBinding()]
[OutputType([HashTable])]
param(
[Parameter(Mandatory = $true, HelpMessage = 'DotnetCore bundle name to search for')]
[ValidateSet('WindowsServerHosting', 'ASPNetRuntimePackageStore')]
[string]
$BundleName
)
[HashTable] $InstalledVersion = [ordered] @{}
[string] $RootPath = 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\Updates\.NET Core'
[string] $PathFilter = ''
try
{
switch ($BundleName)
{
'WindowsServerHosting' { $PathFilter = 'Microsoft .Net Core*Windows Server Hosting' }
'ASPNetRuntimePackageStore' { $PathFilter = 'Microsoft ASP.NET Core*Runtime Package Store*' }
}
if (Test-Path -Path $RootPath -PathType Container)
{
[string[]] $InstalledSHBundles = Get-ChildItem -Path $RootPath `
| Where-Object { $_.PSChildName -like $PathFilter } `
| Sort-Object -Property PSChildName | Select-Object -ExpandProperty PSChildName
for ($i = 0; $i -lt $InstalledSHBundles.Count; $i++)
{
if ($InstalledSHBundles[$i] -match '\d.\d.\d')
{
$InstalledVersion.Add($Matches[0], $InstalledSHBundles[$i])
}
}
}
}
catch
{
throw $_
}
finally
{
Write-Output -InputObject $InstalledVersion -NoEnumerate
}
}
Function Assert-InstalledDotnetCoreRuntimeVersion
{
[CmdletBinding()]
[OutputType([void])]
param(
[Parameter(Mandatory = $true, HelpMessage = 'DotnetCore version to search for')]
[string]
$Version
)
Add-Type -AssemblyName System.Runtime
$versions = Get-InstalledDotnetCoreVersion
$expectedVersion = $null
if (-not [System.Version]::TryParse($Version,([ref]$expectedVersion)))
{
throw "could not parse expected dotnet version '$Version'"
}
$matchingVersions = $versions.RuntimeVersions.Keys |
ForEach-Object {
$out = $null
if (-not [System.Version]::TryParse($_,([ref]$out)))
{
throw "could not parse installed dotnet version '$_'"
}
$out
} |
Where-Object {
$_ -ge $expectedVersion
}
if ($matchingVersions.Count -eq 0)
{
throw "No matching dotnet runtime found on the machine. Goto https://dotnet.microsoft.com/en-us/download and 'Download .NET Runtime'"
}
}
Function Install-SourcePawnManager
{
[CmdletBinding()]
[OutputType([void])]
param(
[Parameter(Mandatory = $true, HelpMessage = 'SourcePawnManager version to install')]
[string]
$Version,
[Parameter(Mandatory = $false, HelpMessage = 'path SourcePawnManager gets installed in')]
[string]
$DestinationPath = $pwd.Path
)
$filename = $PSVersionTable.Platform -eq "Unix" ? "linux-x64.zip" : "win-x64.zip"
$url = "https://github.com/eisbaer66/spm/releases/download/$Version/$filename"
$url
Invoke-WebRequest -Uri $url -OutFile $filename
Expand-Archive $filename -Force -DestinationPath $DestinationPath
Remove-Item -Path $filename
}
Function Add-Path
{
[CmdletBinding()]
[OutputType([void])]
param(
[Parameter(Mandatory = $false, HelpMessage = 'path to add to the PATH')]
[string]
$Path = $pwd.Path
)
$envPath = [Environment]::GetEnvironmentVariable("Path", "User")
if ($envPath -like "*$Path*")
{
return
}
[Environment]::SetEnvironmentVariable("Path", $envPath + ";$Path", "User")
}
$requiredDotnetRuntimeVersion = "6.0"
$spmVersion = "v0.1"
Assert-InstalledDotnetCoreRuntimeVersion -Version $requiredDotnetRuntimeVersion
Install-SourcePawnManager -Version $spmVersion
Add-Path
Write-Output "spm $spmVersion installed and added '$($pwd.Path)' to your PATH"