diff --git a/PSDepend/PSDependScripts/DotnetSdk.ps1 b/PSDepend/PSDependScripts/DotnetSdk.ps1 index 454c4f7..479e708 100644 --- a/PSDepend/PSDependScripts/DotnetSdk.ps1 +++ b/PSDepend/PSDependScripts/DotnetSdk.ps1 @@ -86,22 +86,11 @@ if ($PSDependAction -contains 'Install') { } Install-Dotnet -Channel $Channel -Version $Version -InstallDir $InstallTo } + + Import-Dotnet -Version $Version -InstallDir $InstallDir } # Handle 'Import' if ($PSDependAction -contains 'Import') { - # If the InstallDir was specified and the .NET Core SDK exists in it, add it to the path - if ($InstallDir -and (Test-Dotnet -Version $Version -InstallDir $InstallDir)) { - $env:PATH = "$InstallDir$([IO.Path]::PathSeparator)$env:PATH" - } else { - # Test if it's in the path already and if it's not check if it's in the 'global' location - $dotnetInPath = Get-Command 'dotnet' -ErrorAction SilentlyContinue - if (!$dotnetInPath) { - if (!(Test-Dotnet -Version $Version -InstallDir $globalDotnetSdkLocation)) { - throw ".NET SDK cannot be located. Try installing using PSDepend." - } else { - $env:PATH = "$globalDotnetSdkLocation$([IO.Path]::PathSeparator)$env:PATH" - } - } - } + Import-Dotnet -Version $Version -InstallDir $InstallDir } diff --git a/PSDepend/Private/Import-Dotnet.ps1 b/PSDepend/Private/Import-Dotnet.ps1 new file mode 100644 index 0000000..c9fc63d --- /dev/null +++ b/PSDepend/Private/Import-Dotnet.ps1 @@ -0,0 +1,40 @@ +# Adds the .NET Core SDK to the PATH if it is a valid version. +function Import-Dotnet { + [CmdletBinding()] + param( + [Parameter(Mandatory=$true)] + [string] + $Version, + + [Parameter()] + [string] + $InstallDir + ) + + # The 'global' install location is different per platform + $IsWindowsEnv = !$PSVersionTable.Platform -or $PSVersionTable.Platform -eq "Win32NT" + $globalDotnetSdkLocation = if ($IsWindowsEnv) { "$env:LocalAppData\Microsoft\dotnet" } else { "$env:HOME/.dotnet" } + + $dotnetInPath = Get-Command 'dotnet' -ErrorAction SilentlyContinue + $dotnetInPathIsGood = $dotnetInPath -and (Test-Dotnet -Version $Version -InstallDir $dotnetInPath.Source) + + # Check if the one in the PATH is good enough + if ($dotnetInPathIsGood) { + return + } + + # If the InstallDir was specified and the .NET Core SDK exists in it, add it to the path + if ($InstallDir) { + if (Test-Dotnet -Version $Version -InstallDir $InstallDir) { + $env:PATH = "$InstallDir$([IO.Path]::PathSeparator)$env:PATH" + } else { + throw ".NET SDK cannot be located or it's not new enough. Try installing using PSDepend." + } + } else { + if (Test-Dotnet -Version $Version -InstallDir $globalDotnetSdkLocation) { + $env:PATH = "$globalDotnetSdkLocation$([IO.Path]::PathSeparator)$env:PATH" + } else { + throw ".NET SDK cannot be located or it's not new enough. Try installing using PSDepend." + } + } +} diff --git a/Tests/PSModuleGallery.Type.Tests.ps1 b/Tests/PSModuleGallery.Type.Tests.ps1 index 03e53d8..e0b1f7d 100644 --- a/Tests/PSModuleGallery.Type.Tests.ps1 +++ b/Tests/PSModuleGallery.Type.Tests.ps1 @@ -908,6 +908,7 @@ InModuleScope 'PSDepend' { Invoke-PSDepend @Verbose -Path "$TestDepends\dotnetsdk.complex.depend.psd1" -Force Test-Path $SavePath | Should -BeTrue + { Get-Command 'dotnet' } | Should -Not -Throw } It 'Does nothing if the .NET Core SDK is found' { @@ -916,6 +917,7 @@ InModuleScope 'PSDepend' { Invoke-PSDepend @Verbose -Path "$TestDepends\dotnetsdk.complex.depend.psd1" -Force Assert-MockCalled -CommandName Install-Dotnet -Times 0 -Exactly + { Get-Command 'dotnet' } | Should -Not -Throw } AfterAll { @@ -960,7 +962,7 @@ InModuleScope 'PSDepend' { It 'Throws if the path cannot be found' { Mock Test-Dotnet { return $false } { Invoke-PSDepend @Verbose -Path "$TestDepends\dotnetsdk.simple.depend.psd1" -Force -Import -ErrorAction Stop } | - Should -Throw -ExpectedMessage ".NET SDK cannot be located. Try installing using PSDepend." + Should -Throw -ExpectedMessage ".NET SDK cannot be located or it's not new enough. Try installing using PSDepend." } AfterEach { $env:PATH = $originalPath