Skip .NET SDK and runtime install when unneeded

This commit is contained in:
Andrew Arnott 2020-09-11 05:59:22 -06:00
Родитель e3449f0b07
Коммит 0c212b6459
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: A9B9910CDCCDA441
3 изменённых файлов: 106 добавлений и 15 удалений

Просмотреть файл

@ -0,0 +1,41 @@
<#
.SYNOPSIS
Checks whether a given .NET Core runtime is installed.
#>
[CmdletBinding()]
Param (
[Parameter()]
[ValidateSet('Microsoft.AspNetCore.App','Microsoft.NETCore.App')]
[string]$Runtime='Microsoft.NETCore.App',
[Parameter(Mandatory=$true)]
[Version]$Version
)
$dotnet = Get-Command dotnet -ErrorAction SilentlyContinue
if (!$dotnet) {
# Nothing is installed.
Write-Output $false
exit 1
}
Function IsVersionMatch {
Param(
[Parameter()]
$actualVersion
)
return $actualVersion -and
$Version.Major -eq $actualVersion.Major -and
$Version.Minor -eq $actualVersion.Minor -and
(($Version.Build -eq -1) -or ($Version.Build -eq $actualVersion.Build)) -and
(($Version.Revision -eq -1) -or ($Version.Revision -eq $actualVersion.Revision))
}
$installedRuntimes = dotnet --list-runtimes |? { $_.Split()[0] -ieq $Runtime } |% { $v = $null; [Version]::tryparse($_.Split()[1], [ref] $v); $v }
$matchingRuntimes = $installedRuntimes |? { IsVersionMatch -actualVersion $_ }
if (!$matchingRuntimes) {
Write-Output $false
exit 1
}
Write-Output $true
exit 0

31
tools/Check-DotNetSdk.ps1 Normal file
Просмотреть файл

@ -0,0 +1,31 @@
<#
.SYNOPSIS
Checks whether the .NET Core SDK required by this repo is installed.
#>
[CmdletBinding()]
Param (
)
$dotnet = Get-Command dotnet -ErrorAction SilentlyContinue
if (!$dotnet) {
# Nothing is installed.
Write-Output $false
exit 1
}
# We need to set the current directory so dotnet considers the SDK required by our global.json file.
Push-Location "$PSScriptRoot\.."
try {
dotnet -h 2>&1 | Out-Null
if ($LASTEXITCODE -eq 129) {
# This error code indicates no matching SDK exists.
Write-Output $false
exit 2
}
# The required SDK is already installed!
Write-Output $true
exit 0
} finally {
Pop-Location
}

Просмотреть файл

@ -15,11 +15,14 @@
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 Force
Installs the preferred .NET SDK and runtime versions even if compatible versions are found to already be installed.
#>
[CmdletBinding(SupportsShouldProcess=$true,ConfirmImpact='Medium')]
Param (
[ValidateSet('repo','user','machine')]
[string]$InstallLocality='user'
[string]$InstallLocality='user',
[switch]$Force
)
$DotNetInstallScriptRoot = "$PSScriptRoot/../obj/tools"
@ -98,15 +101,23 @@ if ($InstallLocality -eq 'machine') {
$DotNetInstallDir = '/usr/share/dotnet'
} else {
$restartRequired = $false
if ($PSCmdlet.ShouldProcess(".NET Core SDK $sdkVersion", "Install")) {
Install-DotNet -Version $sdkVersion
$restartRequired = $restartRequired -or ($LASTEXITCODE -eq 3010)
if ($Force -or (-not (& "$PSScriptRoot/Check-DotNetSdk.ps1"))) {
if ($PSCmdlet.ShouldProcess(".NET Core SDK $sdkVersion", "Install")) {
Install-DotNet -Version $sdkVersion
$restartRequired = $restartRequired -or ($LASTEXITCODE -eq 3010)
}
} else {
Write-Verbose "A .NET SDK version compatible with $sdkVersion is already installed."
}
$runtimeVersions | Get-Unique |% {
if ($PSCmdlet.ShouldProcess(".NET Core runtime $_", "Install")) {
Install-DotNet -Version $_ -Runtime
$restartRequired = $restartRequired -or ($LASTEXITCODE -eq 3010)
if ($Force -or (-not (& "$PSScriptRoot/Check-DotNetRuntime.ps1" -Version $_))) {
if ($PSCmdlet.ShouldProcess(".NET Core runtime $_", "Install")) {
Install-DotNet -Version $_ -Runtime
$restartRequired = $restartRequired -or ($LASTEXITCODE -eq 3010)
}
} else {
Write-Verbose ".NET runtime $_ is already installed."
}
}
@ -150,21 +161,29 @@ if (-not (Test-Path $DotNetInstallScriptPath)) {
$anythingInstalled = $false
if ($PSCmdlet.ShouldProcess(".NET Core SDK $sdkVersion", "Install")) {
$anythingInstalled = $true
Invoke-Expression -Command "$DotNetInstallScriptPath -Version $sdkVersion $switches"
if ($Force -or (-not (& "$PSScriptRoot/Check-DotNetSdk.ps1"))) {
if ($PSCmdlet.ShouldProcess(".NET Core SDK $sdkVersion", "Install")) {
$anythingInstalled = $true
Invoke-Expression -Command "$DotNetInstallScriptPath -Version $sdkVersion $switches"
} else {
Invoke-Expression -Command "$DotNetInstallScriptPath -Version $sdkVersion $switches -DryRun"
}
} else {
Invoke-Expression -Command "$DotNetInstallScriptPath -Version $sdkVersion $switches -DryRun"
Write-Verbose "A .NET SDK version compatible with $sdkVersion is already installed."
}
$switches += '-Runtime','dotnet'
$runtimeVersions | Get-Unique |% {
if ($PSCmdlet.ShouldProcess(".NET Core runtime $_", "Install")) {
$anythingInstalled = $true
Invoke-Expression -Command "$DotNetInstallScriptPath -Channel $_ $switches"
if ($Force -or (-not (& "$PSScriptRoot/Check-DotNetRuntime.ps1" -Version $_))) {
if ($PSCmdlet.ShouldProcess(".NET Core runtime $_", "Install")) {
$anythingInstalled = $true
Invoke-Expression -Command "$DotNetInstallScriptPath -Channel $_ $switches"
} else {
Invoke-Expression -Command "$DotNetInstallScriptPath -Channel $_ $switches -DryRun"
}
} else {
Invoke-Expression -Command "$DotNetInstallScriptPath -Channel $_ $switches -DryRun"
Write-Verbose ".NET runtime $_ is already installed."
}
}