Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add to PATH if we don't need to install #93

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
17 changes: 3 additions & 14 deletions PSDepend/PSDependScripts/DotnetSdk.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
40 changes: 40 additions & 0 deletions PSDepend/Private/Import-Dotnet.ps1
Original file line number Diff line number Diff line change
@@ -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."
}
}
}
4 changes: 3 additions & 1 deletion Tests/PSModuleGallery.Type.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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' {
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down