@@ -128,10 +128,13 @@ function Start-ScriptAnalyzerBuild
128
128
129
129
BEGIN {
130
130
# 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
131
134
if ( -not (Test-SuitableDotnet ) ) {
132
135
$requiredVersion = Get-GlobalJsonSdkVersion
133
136
$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 '"
135
138
}
136
139
}
137
140
END {
@@ -202,6 +205,9 @@ function Start-ScriptAnalyzerBuild
202
205
try {
203
206
Push-Location $projectRoot / Rules
204
207
Write-Progress " Building ScriptAnalyzer for PSVersion '$PSVersion ' using framework '$framework ' and configuration '$Configuration '"
208
+ if ( -not $script :DotnetExe ) {
209
+ $script :dotnetExe = Get-DotnetExe
210
+ }
205
211
$buildOutput = & $script :dotnetExe build -- framework $framework -- configuration " $config "
206
212
if ( $LASTEXITCODE -ne 0 ) { throw " $buildOutput " }
207
213
}
@@ -308,6 +314,11 @@ function Install-Dotnet
308
314
If ( $PSCmdlet.ShouldProcess (" $installScriptName for $version " )) {
309
315
& " ${installScriptPath} " - c release - version $version
310
316
}
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
+ }
311
322
}
312
323
catch {
313
324
throw $_
@@ -336,7 +347,10 @@ function ConvertTo-PortableVersion {
336
347
foreach ( $v in $strVersion ) {
337
348
$ver , $pre = $v.split (" -" , 2 )
338
349
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
+ }
340
354
}
341
355
catch {
342
356
Write-Warning " Cannot convert '$v ' to portable version"
@@ -420,6 +434,11 @@ function Test-SuitableDotnet {
420
434
421
435
# these are mockable functions for testing
422
436
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
+ }
423
442
try {
424
443
# earlier versions of dotnet do not support --list-sdks, so we'll check the output
425
444
# and use dotnet --version as a fallback
@@ -482,20 +501,26 @@ function Get-DotnetExe
482
501
{
483
502
$discoveredDotNet = Get-Command - CommandType Application dotnet - ErrorAction SilentlyContinue
484
503
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
487
508
}
488
509
# it's not in the path, try harder to find it
489
510
# check the usual places
490
511
if ( ! (test-path variable:IsWindows) -or $IsWindows ) {
491
512
$dotnetHuntPath = " $HOME \AppData\Local\Microsoft\dotnet\dotnet.exe"
513
+ Write-Verbose - Verbose " checking $dotnetHuntPath "
492
514
if ( test-path $dotnetHuntPath ) {
515
+ $script :DotnetExe = $dotnetHuntPath
493
516
return $dotnetHuntPath
494
517
}
495
518
}
496
519
else {
497
520
$dotnetHuntPath = " $HOME /.dotnet/dotnet"
521
+ Write-Verbose - Verbose " checking $dotnetHuntPath "
498
522
if ( test-path $dotnetHuntPath ) {
523
+ $script :DotnetExe = $dotnetHuntPath
499
524
return $dotnetHuntPath
500
525
}
501
526
}
0 commit comments