Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions build.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ function Start-PSBuild {
}

# Verify if the dotnet in-use is the required version
$dotnetCLIInstalledVersion = Get-LatestInstalledSDK
$dotnetCLIInstalledVersion = Find-RequiredSDK $dotnetCLIRequiredVersion

If ($dotnetCLIInstalledVersion -ne $dotnetCLIRequiredVersion) {
Write-Warning @"
Expand Down Expand Up @@ -2096,7 +2096,7 @@ function Start-PSBootstrap {
$dotNetExists = precheck 'dotnet' $null
$dotNetVersion = [string]::Empty
if($dotNetExists) {
$dotNetVersion = Get-LatestInstalledSDK
$dotNetVersion = Find-RequiredSDK $dotnetCLIRequiredVersion
}

if(!$dotNetExists -or $dotNetVersion -ne $dotnetCLIRequiredVersion -or $Force.IsPresent) {
Expand Down Expand Up @@ -2138,10 +2138,30 @@ function Start-PSBootstrap {
}
}

function Get-LatestInstalledSDK {
Start-NativeExecution -sb {
dotnet --list-sdks | Select-String -Pattern '\d*.\d*.\d*(-\w*\.\d*)?' | ForEach-Object { [System.Management.Automation.SemanticVersion]::new($_.matches.value) } | Sort-Object -Descending | Select-Object -First 1
} -IgnoreExitcode 2> $null
## If the required SDK version is found, return it.
## Otherwise, return the latest installed SDK version that can be found.
function Find-RequiredSDK {
param(
[Parameter(Mandatory, Position = 0)]
[string] $requiredSdkVersion
)

$output = Start-NativeExecution -sb { dotnet --list-sdks } -IgnoreExitcode 2> $null

$installedSdkVersions = $output | ForEach-Object {
# this splits strings like
# '6.0.202 [C:\Program Files\dotnet\sdk]'
# '7.0.100-preview.2.22153.17 [C:\Users\johndoe\AppData\Local\Microsoft\dotnet\sdk]'
# into version and path parts.
($_ -split '\s',2)[0]
}

if ($installedSdkVersions -contains $requiredSdkVersion) {
$requiredSdkVersion
}
else {
$installedSdkVersions | Sort-Object -Descending | Select-Object -First 1
}
}

function Start-DevPowerShell {
Expand Down Expand Up @@ -2293,7 +2313,7 @@ function Find-Dotnet() {
if (precheck dotnet) {
# Must run from within repo to ensure global.json can specify the required SDK version
Push-Location $PSScriptRoot
$dotnetCLIInstalledVersion = Get-LatestInstalledSDK
$dotnetCLIInstalledVersion = Find-RequiredSDK $chosenDotNetVersion
Pop-Location

Write-Verbose -Message "Find-DotNet: dotnetCLIInstalledVersion = $dotnetCLIInstalledVersion; chosenDotNetVersion = $chosenDotNetVersion"
Expand Down