Skip to content

Commit 4d2bf72

Browse files
committed
make the hunt for the dotnet executable more generic and try harder to find the exe
1 parent 2df5451 commit 4d2bf72

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

build.psm1

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ function Start-ScriptAnalyzerBuild
130130
# don't allow the build to be started unless we have the proper Cli version
131131
if ( -not (Test-SuitableDotnet) ) {
132132
$requiredVersion = Get-GlobalJsonSdkVersion
133-
throw "No suitable dotnet CLI found, requires version '$requiredVersion'"
133+
$foundVersion = Get-InstalledCLIVersion
134+
throw "No suitable dotnet CLI found, requires version '$requiredVersion' found only '$foundVersion'"
134135
}
135136
}
136137
END {
@@ -201,7 +202,7 @@ function Start-ScriptAnalyzerBuild
201202
try {
202203
Push-Location $projectRoot/Rules
203204
Write-Progress "Building ScriptAnalyzer for PSVersion '$PSVersion' using framework '$framework' and configuration '$Configuration'"
204-
$buildOutput = dotnet build --framework $framework --configuration "$config"
205+
$buildOutput = & $script:dotnetExe build --framework $framework --configuration "$config"
205206
if ( $LASTEXITCODE -ne 0 ) { throw "$buildOutput" }
206207
}
207208
catch {
@@ -285,7 +286,7 @@ function Get-TestFailures
285286
function Install-Dotnet
286287
{
287288
[CmdletBinding(SupportsShouldProcess=$true)]
288-
param (
289+
param (
289290
[Parameter()][Switch]$Force,
290291
[Parameter()]$version = $( Get-GlobalJsonSdkVersion )
291292
)
@@ -363,7 +364,7 @@ function ConvertTo-PortableVersion {
363364
Add-Member -inputobject $customObject -Type ScriptMethod -Name IsContainedIn -Value {
364365
param ( [object[]]$collection )
365366
foreach ( $object in $collection ) {
366-
if (
367+
if (
367368
$this.Major -eq $object.Major -and
368369
$this.Minor -eq $object.Minor -and
369370
$this.Patch -eq $object.Patch -and
@@ -422,10 +423,10 @@ function Get-InstalledCLIVersion {
422423
# earlier versions of dotnet do not support --list-sdks, so we'll check the output
423424
# and use dotnet --version as a fallback
424425

425-
$sdkList = dotnet --list-sdks 2>&1
426+
$sdkList = & $script:dotnetExe --list-sdks 2>&1
426427
$sdkList = "Unknown option"
427428
if ( $sdkList -match "Unknown option" ) {
428-
$installedVersions = dotnet --version
429+
$installedVersions = & $script:dotnetExe --version
429430
}
430431
else {
431432
$installedVersions = $sdkList | Foreach-Object { $_.Split()[0] }
@@ -475,3 +476,28 @@ function Receive-DotnetInstallScript
475476

476477
return $installScript.FullName
477478
}
479+
480+
function Get-DotnetExe
481+
{
482+
$discoveredDotNet = Get-Command -CommandType Application dotnet
483+
if ( $discoveredDotNet ) {
484+
$discoveredDotNet | Select-Object -First 1 | Foreach-Object { $_.Source }
485+
return
486+
}
487+
# it's not in the path, try harder to find it
488+
# check the usual places
489+
if ( ! (test-path variable:IsWindows) -or $IsWindows ) {
490+
$dotnetHuntPath = "$HOME\AppData\Local\Microsoft\dotnet\dotnet.exe"
491+
if ( test-path $dotnetHuntPath ) {
492+
return $dotnetHuntPath
493+
}
494+
}
495+
else {
496+
$dotnetHuntPath = "$HOME/.dotnet/dotnet"
497+
if ( test-path $dotnetHuntPath ) {
498+
return $dotnetHuntPath
499+
}
500+
}
501+
throw "Could not find dotnet executable"
502+
}
503+
$script:dotnetExe = Get-DotnetExe

0 commit comments

Comments
 (0)