Skip to content

Commit 34835b8

Browse files
committed
Improve logic for handling a system where dotnet has never been installed
1 parent 020c086 commit 34835b8

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

build.psm1

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,13 @@ function Start-ScriptAnalyzerBuild
128128

129129
BEGIN {
130130
# don't allow the build to be started unless we have the proper Cli version
131+
# this will not actually install dotnet if it's already present, but it will
132+
# install the proper version
133+
Install-Dotnet
131134
if ( -not (Test-SuitableDotnet) ) {
132135
$requiredVersion = Get-GlobalJsonSdkVersion
133136
$foundVersion = Get-InstalledCLIVersion
134-
throw "No suitable dotnet CLI found, requires version '$requiredVersion' found only '$foundVersion'"
137+
Write-Warning "No suitable dotnet CLI found, requires version '$requiredVersion' found only '$foundVersion'"
135138
}
136139
}
137140
END {
@@ -202,6 +205,9 @@ function Start-ScriptAnalyzerBuild
202205
try {
203206
Push-Location $projectRoot/Rules
204207
Write-Progress "Building ScriptAnalyzer for PSVersion '$PSVersion' using framework '$framework' and configuration '$Configuration'"
208+
if ( -not $script:DotnetExe ) {
209+
$script:dotnetExe = Get-DotnetExe
210+
}
205211
$buildOutput = & $script:dotnetExe build --framework $framework --configuration "$config"
206212
if ( $LASTEXITCODE -ne 0 ) { throw "$buildOutput" }
207213
}
@@ -308,6 +314,11 @@ function Install-Dotnet
308314
If ( $PSCmdlet.ShouldProcess("$installScriptName for $version")) {
309315
& "${installScriptPath}" -c release -version $version
310316
}
317+
# this may be the first time that dotnet has been installed,
318+
# set up the executable variable
319+
if ( -not $script:DotnetExe ) {
320+
$script:DotnetExe = Get-DotnetExe
321+
}
311322
}
312323
catch {
313324
throw $_
@@ -336,7 +347,10 @@ function ConvertTo-PortableVersion {
336347
foreach ( $v in $strVersion ) {
337348
$ver, $pre = $v.split("-",2)
338349
try {
339-
[int]$major, [int]$minor, [int]$patch = $ver.Split(".")
350+
[int]$major, [int]$minor, [int]$patch, $unused = $ver.Split(".",4)
351+
if ( -not $pre ) {
352+
$pre = $unused
353+
}
340354
}
341355
catch {
342356
Write-Warning "Cannot convert '$v' to portable version"
@@ -420,6 +434,11 @@ function Test-SuitableDotnet {
420434

421435
# these are mockable functions for testing
422436
function Get-InstalledCLIVersion {
437+
# dotnet might not have been installed _ever_, so just return 0.0.0.0
438+
if ( -not $script:DotnetExe ) {
439+
Write-Warning "Dotnet executable not found"
440+
return (ConvertTo-PortableVersion 0.0.0)
441+
}
423442
try {
424443
# earlier versions of dotnet do not support --list-sdks, so we'll check the output
425444
# and use dotnet --version as a fallback
@@ -482,20 +501,26 @@ function Get-DotnetExe
482501
{
483502
$discoveredDotNet = Get-Command -CommandType Application dotnet -ErrorAction SilentlyContinue
484503
if ( $discoveredDotNet ) {
485-
$discoveredDotNet | Select-Object -First 1 | Foreach-Object { $_.Source }
486-
return
504+
$dotnetFoundPath = $discoveredDotNet | Select-Object -First 1 | Foreach-Object { $_.Source }
505+
Write-Verbose -Verbose "Found dotnet here: $dotnetFoundPath"
506+
$script:DotnetExe = $dotnetFoundPath
507+
return $dotnetFoundPath
487508
}
488509
# it's not in the path, try harder to find it
489510
# check the usual places
490511
if ( ! (test-path variable:IsWindows) -or $IsWindows ) {
491512
$dotnetHuntPath = "$HOME\AppData\Local\Microsoft\dotnet\dotnet.exe"
513+
Write-Verbose -Verbose "checking $dotnetHuntPath"
492514
if ( test-path $dotnetHuntPath ) {
515+
$script:DotnetExe = $dotnetHuntPath
493516
return $dotnetHuntPath
494517
}
495518
}
496519
else {
497520
$dotnetHuntPath = "$HOME/.dotnet/dotnet"
521+
Write-Verbose -Verbose "checking $dotnetHuntPath"
498522
if ( test-path $dotnetHuntPath ) {
523+
$script:DotnetExe = $dotnetHuntPath
499524
return $dotnetHuntPath
500525
}
501526
}

tools/appveyor.psm1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ function Invoke-AppVeyorInstall {
3131
Install-Module -Name platyPS -Force -Scope CurrentUser -RequiredVersion $platyPSVersion
3232
}
3333

34-
Write-Verbose -Verbose "Installing required .Net CORE SDK $requiredDotNetCoreSDKVersion"
3534
# the build script sorts out the problems of WMF4 and earlier versions of dotnet CLI
35+
Write-Verbose -Verbose "Installing required .Net CORE SDK"
36+
Write-Verbose "& $buildScriptDir/build.ps1 -bootstrap"
3637
$buildScriptDir = (Resolve-Path "$PSScriptRoot/..").Path
3738
& "$buildScriptDir/build.ps1" -bootstrap
3839
}

0 commit comments

Comments
 (0)