forked from MessagePack-CSharp/MessagePack-CSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInstall-DotNetSdk.ps1
executable file
·418 lines (361 loc) · 17.4 KB
/
Install-DotNetSdk.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#!/usr/bin/env pwsh
<#
.SYNOPSIS
Installs the .NET SDK specified in the global.json file at the root of this repository,
along with supporting .NET runtimes used for testing.
.DESCRIPTION
This MAY not require elevation, as the SDK and runtimes are installed locally to this repo location,
unless `-InstallLocality machine` is specified.
.PARAMETER InstallLocality
A value indicating whether dependencies should be installed locally to the repo or at a per-user location.
Per-user allows sharing the installed dependencies across repositories and allows use of a shared expanded package cache.
Visual Studio will only notice and use these SDKs/runtimes if VS is launched from the environment that runs this script.
Per-repo allows for high isolation, allowing for a more precise recreation of the environment within an Azure Pipelines build.
When using 'repo', environment variables are set to cause the locally installed dotnet SDK to be used.
Per-repo can lead to file locking issues when dotnet.exe is left running as a build server and can be mitigated by running `dotnet build-server shutdown`.
Per-machine requires elevation and will download and install all SDKs and runtimes to machine-wide locations so all applications can find it.
.PARAMETER SdkOnly
Skips installing the runtime.
.PARAMETER IncludeX86
Installs a x86 SDK and runtimes in addition to the x64 ones. Only supported on Windows. Ignored on others.
.PARAMETER IncludeAspNetCore
Installs the ASP.NET Core runtime along with the .NET runtime.
#>
[CmdletBinding(SupportsShouldProcess=$true,ConfirmImpact='Medium')]
Param (
[ValidateSet('repo','user','machine')]
[string]$InstallLocality='user',
[switch]$SdkOnly,
[switch]$IncludeX86,
[switch]$IncludeAspNetCore=$true
)
$DotNetInstallScriptRoot = "$PSScriptRoot/../obj/tools"
if (!(Test-Path $DotNetInstallScriptRoot)) { New-Item -ItemType Directory -Path $DotNetInstallScriptRoot -WhatIf:$false | Out-Null }
$DotNetInstallScriptRoot = Resolve-Path $DotNetInstallScriptRoot
# Look up actual required .NET SDK version from global.json
$sdkVersion = & "$PSScriptRoot/../azure-pipelines/variables/DotNetSdkVersion.ps1"
If ($IncludeX86 -and ($IsMacOS -or $IsLinux)) {
Write-Verbose "Ignoring -IncludeX86 switch because 32-bit runtimes are only supported on Windows."
$IncludeX86 = $false
}
$IncludeAspNetCore = $true
$arch = [System.Runtime.InteropServices.RuntimeInformation]::ProcessArchitecture
if (!$arch) { # Windows Powershell leaves this blank
$arch = 'x64'
if ($env:PROCESSOR_ARCHITECTURE -eq 'ARM64') { $arch = 'ARM64' }
}
# Search for all .NET runtime versions referenced from MSBuild projects and arrange to install them.
$runtimeVersions = @()
$windowsDesktopRuntimeVersions = @()
$aspnetRuntimeVersions = @()
if (!$SdkOnly) {
Get-ChildItem "$PSScriptRoot\..\src\*.*proj","$PSScriptRoot\..\tests\*.*proj","$PSScriptRoot\..\Directory.Build.props" -Recurse |% {
$projXml = [xml](Get-Content -Path $_)
$pg = $projXml.Project.PropertyGroup
if ($pg) {
$targetFrameworks = @()
$tf = $pg.TargetFramework
$targetFrameworks += $tf
$tfs = $pg.TargetFrameworks
if ($tfs) {
$targetFrameworks = $tfs -Split ';'
}
}
$targetFrameworks |? { $_ -match 'net(?:coreapp)?(\d+\.\d+)' } |% {
$v = $Matches[1]
$runtimeVersions += $v
$aspnetRuntimeVersions += $v
if ($v -ge '3.0' -and -not ($IsMacOS -or $IsLinux)) {
$windowsDesktopRuntimeVersions += $v
}
}
# Add target frameworks of the form: netXX
$targetFrameworks |? { $_ -match 'net(\d+\.\d+)' } |% {
$v = $Matches[1]
$runtimeVersions += $v
$aspnetRuntimeVersions += $v
if (-not ($IsMacOS -or $IsLinux)) {
$windowsDesktopRuntimeVersions += $v
}
}
}
}
if (!$IncludeAspNetCore) {
$aspnetRuntimeVersions = @()
}
if (!$IncludeAspNetCore) {
$aspnetRuntimeVersions = @()
}
Function Get-FileFromWeb([Uri]$Uri, $OutDir) {
$OutFile = Join-Path $OutDir $Uri.Segments[-1]
if (!(Test-Path $OutFile)) {
Write-Verbose "Downloading $Uri..."
if (!(Test-Path $OutDir)) { New-Item -ItemType Directory -Path $OutDir | Out-Null }
try {
(New-Object System.Net.WebClient).DownloadFile($Uri, $OutFile)
} finally {
# This try/finally causes the script to abort
}
}
$OutFile
}
Function Get-InstallerExe(
$Version,
$Architecture,
[ValidateSet('Sdk','Runtime','WindowsDesktop')]
[string]$sku
) {
# Get the latest/actual version for the specified one
$TypedVersion = $null
if (![Version]::TryParse($Version, [ref] $TypedVersion)) {
Write-Error "Unable to parse $Version into an a.b.c.d version. This version cannot be installed machine-wide."
exit 1
}
if ($TypedVersion.Build -eq -1) {
$versionInfo = -Split (Invoke-WebRequest -Uri "https://dotnetcli.blob.core.windows.net/dotnet/$sku/$Version/latest.version" -UseBasicParsing)
$Version = $versionInfo[-1]
}
$majorMinor = "$($TypedVersion.Major).$($TypedVersion.Minor)"
$ReleasesFile = Join-Path $DotNetInstallScriptRoot "$majorMinor\releases.json"
if (!(Test-Path $ReleasesFile)) {
Get-FileFromWeb -Uri "https://dotnetcli.blob.core.windows.net/dotnet/release-metadata/$majorMinor/releases.json" -OutDir (Split-Path $ReleasesFile) | Out-Null
}
$releases = Get-Content $ReleasesFile | ConvertFrom-Json
$url = $null
foreach ($release in $releases.releases) {
$filesElement = $null
if ($release.$sku.version -eq $Version) {
$filesElement = $release.$sku.files
}
if ($filesElement) {
foreach ($file in $filesElement) {
if ($file.rid -eq "win-$Architecture") {
$url = $file.url
Break
}
}
if ($url) {
Break
}
}
}
if ($url) {
Get-FileFromWeb -Uri $url -OutDir $DotNetInstallScriptRoot
} else {
Write-Error "Unable to find release of $sku v$Version"
}
}
Function Install-DotNet($Version, $Architecture, [ValidateSet('Sdk','Runtime','WindowsDesktop','AspNetCore')][string]$sku = 'Sdk') {
Write-Host "Downloading .NET $sku $Version..."
$Installer = Get-InstallerExe -Version $Version -Architecture $Architecture -sku $sku
Write-Host "Installing .NET $sku $Version..."
cmd /c start /wait $Installer /install /passive /norestart
if ($LASTEXITCODE -eq 3010) {
Write-Verbose "Restart required"
} elseif ($LASTEXITCODE -ne 0) {
throw "Failure to install .NET SDK"
}
}
$switches = @()
$envVars = @{
# For locally installed dotnet, skip first time experience which takes a long time
'DOTNET_SKIP_FIRST_TIME_EXPERIENCE' = 'true';
}
if ($InstallLocality -eq 'machine') {
if ($IsMacOS -or $IsLinux) {
$DotNetInstallDir = '/usr/share/dotnet'
} else {
$restartRequired = $false
if ($PSCmdlet.ShouldProcess(".NET SDK $sdkVersion", "Install")) {
Install-DotNet -Version $sdkVersion -Architecture $arch
$restartRequired = $restartRequired -or ($LASTEXITCODE -eq 3010)
if ($IncludeX86) {
Install-DotNet -Version $sdkVersion -Architecture x86
$restartRequired = $restartRequired -or ($LASTEXITCODE -eq 3010)
}
}
$runtimeVersions | Sort-Object | Get-Unique |% {
if ($PSCmdlet.ShouldProcess(".NET runtime $_", "Install")) {
Install-DotNet -Version $_ -sku Runtime -Architecture $arch
$restartRequired = $restartRequired -or ($LASTEXITCODE -eq 3010)
if ($IncludeX86) {
Install-DotNet -Version $_ -sku Runtime -Architecture x86
$restartRequired = $restartRequired -or ($LASTEXITCODE -eq 3010)
}
}
}
$windowsDesktopRuntimeVersions | Sort-Object | Get-Unique |% {
if ($PSCmdlet.ShouldProcess(".NET Windows Desktop $_", "Install")) {
Install-DotNet -Version $_ -sku WindowsDesktop -Architecture $arch
$restartRequired = $restartRequired -or ($LASTEXITCODE -eq 3010)
if ($IncludeX86) {
Install-DotNet -Version $_ -sku WindowsDesktop -Architecture x86
$restartRequired = $restartRequired -or ($LASTEXITCODE -eq 3010)
}
}
}
$aspnetRuntimeVersions | Sort-Object | Get-Unique |% {
if ($PSCmdlet.ShouldProcess("ASP.NET Core $_", "Install")) {
Install-DotNet -Version $_ -sku AspNetCore -Architecture $arch
$restartRequired = $restartRequired -or ($LASTEXITCODE -eq 3010)
if ($IncludeX86) {
Install-DotNet -Version $_ -sku AspNetCore -Architecture x86
$restartRequired = $restartRequired -or ($LASTEXITCODE -eq 3010)
}
}
}
if ($restartRequired) {
Write-Host -ForegroundColor Yellow "System restart required"
Exit 3010
}
return
}
} elseif ($InstallLocality -eq 'repo') {
$DotNetInstallDir = "$DotNetInstallScriptRoot/.dotnet"
$DotNetX86InstallDir = "$DotNetInstallScriptRoot/x86/.dotnet"
} elseif ($env:AGENT_TOOLSDIRECTORY) {
$DotNetInstallDir = "$env:AGENT_TOOLSDIRECTORY/dotnet"
$DotNetX86InstallDir = "$env:AGENT_TOOLSDIRECTORY/x86/dotnet"
} else {
$DotNetInstallDir = Join-Path $HOME .dotnet
}
if ($DotNetInstallDir) {
if (!(Test-Path $DotNetInstallDir)) { New-Item -ItemType Directory -Path $DotNetInstallDir }
$DotNetInstallDir = Resolve-Path $DotNetInstallDir
Write-Host "Installing .NET SDK and runtimes to $DotNetInstallDir" -ForegroundColor Blue
$envVars['DOTNET_MULTILEVEL_LOOKUP'] = '0'
$envVars['DOTNET_ROOT'] = $DotNetInstallDir
}
if ($IncludeX86) {
if ($DotNetX86InstallDir) {
if (!(Test-Path $DotNetX86InstallDir)) { New-Item -ItemType Directory -Path $DotNetX86InstallDir }
$DotNetX86InstallDir = Resolve-Path $DotNetX86InstallDir
Write-Host "Installing x86 .NET SDK and runtimes to $DotNetX86InstallDir" -ForegroundColor Blue
} else {
# Only machine-wide or repo-wide installations can handle two unique dotnet.exe architectures.
Write-Error "The installation location or OS isn't supported for x86 installation. Try a different -InstallLocality value."
return 1
}
}
if ($IsMacOS -or $IsLinux) {
$DownloadUri = "https://raw.githubusercontent.com/dotnet/install-scripts/0b09de9bc136cacb5f849a6957ebd4062173c148/src/dotnet-install.sh"
$DotNetInstallScriptPath = "$DotNetInstallScriptRoot/dotnet-install.sh"
} else {
$DownloadUri = "https://raw.githubusercontent.com/dotnet/install-scripts/0b09de9bc136cacb5f849a6957ebd4062173c148/src/dotnet-install.ps1"
$DotNetInstallScriptPath = "$DotNetInstallScriptRoot/dotnet-install.ps1"
}
if (-not (Test-Path $DotNetInstallScriptPath)) {
Invoke-WebRequest -Uri $DownloadUri -OutFile $DotNetInstallScriptPath -UseBasicParsing
if ($IsMacOS -or $IsLinux) {
chmod +x $DotNetInstallScriptPath
}
}
# In case the script we invoke is in a directory with spaces, wrap it with single quotes.
# In case the path includes single quotes, escape them.
$DotNetInstallScriptPathExpression = $DotNetInstallScriptPath.Replace("'", "''")
$DotNetInstallScriptPathExpression = "& '$DotNetInstallScriptPathExpression'"
$anythingInstalled = $false
$global:LASTEXITCODE = 0
if ($PSCmdlet.ShouldProcess(".NET SDK $sdkVersion", "Install")) {
$anythingInstalled = $true
Invoke-Expression -Command "$DotNetInstallScriptPathExpression -Version $sdkVersion -Architecture $arch -InstallDir $DotNetInstallDir $switches"
if ($LASTEXITCODE -ne 0) {
Write-Error ".NET SDK installation failure: $LASTEXITCODE"
exit $LASTEXITCODE
}
} else {
Invoke-Expression -Command "$DotNetInstallScriptPathExpression -Version $sdkVersion -Architecture $arch -InstallDir $DotNetInstallDir $switches -DryRun"
}
if ($IncludeX86) {
if ($PSCmdlet.ShouldProcess(".NET x86 SDK $sdkVersion", "Install")) {
$anythingInstalled = $true
Invoke-Expression -Command "$DotNetInstallScriptPathExpression -Version $sdkVersion -Architecture x86 -InstallDir $DotNetX86InstallDir $switches"
if ($LASTEXITCODE -ne 0) {
Write-Error ".NET x86 SDK installation failure: $LASTEXITCODE"
exit $LASTEXITCODE
}
} else {
Invoke-Expression -Command "$DotNetInstallScriptPathExpression -Version $sdkVersion -Architecture x86 -InstallDir $DotNetX86InstallDir $switches -DryRun"
}
}
$dotnetRuntimeSwitches = $switches + '-Runtime','dotnet'
$runtimeVersions | Sort-Object -Unique |% {
if ($PSCmdlet.ShouldProcess(".NET $Arch runtime $_", "Install")) {
$anythingInstalled = $true
Invoke-Expression -Command "$DotNetInstallScriptPathExpression -Channel $_ -Architecture $arch -InstallDir $DotNetInstallDir $dotnetRuntimeSwitches"
if ($LASTEXITCODE -ne 0) {
Write-Error ".NET SDK installation failure: $LASTEXITCODE"
exit $LASTEXITCODE
}
} else {
Invoke-Expression -Command "$DotNetInstallScriptPathExpression -Channel $_ -Architecture $arch -InstallDir $DotNetInstallDir $dotnetRuntimeSwitches -DryRun"
}
if ($IncludeX86) {
if ($PSCmdlet.ShouldProcess(".NET x86 runtime $_", "Install")) {
$anythingInstalled = $true
Invoke-Expression -Command "$DotNetInstallScriptPathExpression -Channel $_ -Architecture x86 -InstallDir $DotNetX86InstallDir $dotnetRuntimeSwitches"
if ($LASTEXITCODE -ne 0) {
Write-Error ".NET SDK installation failure: $LASTEXITCODE"
exit $LASTEXITCODE
}
} else {
Invoke-Expression -Command "$DotNetInstallScriptPathExpression -Channel $_ -Architecture x86 -InstallDir $DotNetX86InstallDir $dotnetRuntimeSwitches -DryRun"
}
}
}
$windowsDesktopRuntimeSwitches = $switches + '-Runtime','windowsdesktop'
$windowsDesktopRuntimeVersions | Sort-Object -Unique |% {
if ($PSCmdlet.ShouldProcess(".NET WindowsDesktop $arch runtime $_", "Install")) {
$anythingInstalled = $true
Invoke-Expression -Command "$DotNetInstallScriptPathExpression -Channel $_ -Architecture $arch -InstallDir $DotNetInstallDir $windowsDesktopRuntimeSwitches"
if ($LASTEXITCODE -ne 0) {
Write-Error ".NET SDK installation failure: $LASTEXITCODE"
exit $LASTEXITCODE
}
} else {
Invoke-Expression -Command "$DotNetInstallScriptPathExpression -Channel $_ -Architecture $arch -InstallDir $DotNetInstallDir $windowsDesktopRuntimeSwitches -DryRun"
}
if ($IncludeX86) {
if ($PSCmdlet.ShouldProcess(".NET WindowsDesktop x86 runtime $_", "Install")) {
$anythingInstalled = $true
Invoke-Expression -Command "$DotNetInstallScriptPathExpression -Channel $_ -Architecture x86 -InstallDir $DotNetX86InstallDir $windowsDesktopRuntimeSwitches"
if ($LASTEXITCODE -ne 0) {
Write-Error ".NET SDK installation failure: $LASTEXITCODE"
exit $LASTEXITCODE
}
} else {
Invoke-Expression -Command "$DotNetInstallScriptPathExpression -Channel $_ -Architecture x86 -InstallDir $DotNetX86InstallDir $windowsDesktopRuntimeSwitches -DryRun"
}
}
}
$aspnetRuntimeSwitches = $switches + '-Runtime','aspnetcore'
$aspnetRuntimeVersions | Sort-Object -Unique |% {
if ($PSCmdlet.ShouldProcess(".NET ASP.NET Core $arch runtime $_", "Install")) {
$anythingInstalled = $true
Invoke-Expression -Command "$DotNetInstallScriptPathExpression -Channel $_ -Architecture $arch -InstallDir $DotNetInstallDir $aspnetRuntimeSwitches"
if ($LASTEXITCODE -ne 0) {
Write-Error ".NET SDK installation failure: $LASTEXITCODE"
exit $LASTEXITCODE
}
} else {
Invoke-Expression -Command "$DotNetInstallScriptPathExpression -Channel $_ -Architecture $arch -InstallDir $DotNetInstallDir $aspnetRuntimeSwitches -DryRun"
}
if ($IncludeX86) {
if ($PSCmdlet.ShouldProcess(".NET ASP.NET Core x86 runtime $_", "Install")) {
$anythingInstalled = $true
Invoke-Expression -Command "$DotNetInstallScriptPathExpression -Channel $_ -Architecture x86 -InstallDir $DotNetX86InstallDir $aspnetRuntimeSwitches"
if ($LASTEXITCODE -ne 0) {
Write-Error ".NET SDK installation failure: $LASTEXITCODE"
exit $LASTEXITCODE
}
} else {
Invoke-Expression -Command "$DotNetInstallScriptPathExpression -Channel $_ -Architecture x86 -InstallDir $DotNetX86InstallDir $aspnetRuntimeSwitches -DryRun"
}
}
}
if ($PSCmdlet.ShouldProcess("Set DOTNET environment variables to discover these installed runtimes?")) {
& "$PSScriptRoot/Set-EnvVars.ps1" -Variables $envVars -PrependPath $DotNetInstallDir | Out-Null
}
if ($anythingInstalled -and ($InstallLocality -ne 'machine') -and !$env:TF_BUILD -and !$env:GITHUB_ACTIONS) {
Write-Warning ".NET runtimes or SDKs were installed to a non-machine location. Perform your builds or open Visual Studio from this same environment in order for tools to discover the location of these dependencies."
}