From 511936606e1162e59dc201c3ea3fd0e1112078d5 Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Mon, 20 Jun 2016 11:59:14 -0700 Subject: [PATCH] Add Find-Dotnet to make build module smarter New function smartly adds the probable location of the .NET CLI tools to the path if `dotnet` is not found. If it is still not found, it restores the path. All functions using `dotnet` should call `Find-Dotnet` first. --- build.psm1 | 41 +++++++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/build.psm1 b/build.psm1 index 39e56d5d73..7c4b9ead2e 100644 --- a/build.psm1 +++ b/build.psm1 @@ -68,14 +68,8 @@ function Start-PSBuild { $FullCLR = $true } - if (-not $NoPath) { - Write-Verbose "Appending probable .NET CLI tool path" - if ($IsWindows) { - $env:Path += ";$env:LocalAppData\Microsoft\dotnet" - } else { - $env:PATH += ":$env:HOME/.dotnet" - } - } + # Add .NET CLI tools to PATH + Find-Dotnet if ($IsWindows) { # use custom package store - this value is also defined in nuget.config under config/repositoryPath @@ -241,6 +235,9 @@ function New-PSOptions { [switch]$FullCLR ) + # Add .NET CLI tools to PATH + Find-Dotnet + if ($FullCLR) { $Top = "$PSScriptRoot/src/Microsoft.PowerShell.ConsoleHost" } else { @@ -344,6 +341,7 @@ function Start-PSPester { function Start-PSxUnit { [CmdletBinding()]param() + if ($IsWindows) { throw "xUnit tests are only currently supported on Linux / OS X" } @@ -353,6 +351,9 @@ function Start-PSxUnit { return } + # Add .NET CLI tools to PATH + Find-Dotnet + $Arguments = "--configuration", "Linux", "-parallel", "none" if ($PSCmdlet.MyInvocation.BoundParameters["Verbose"].IsPresent) { $Arguments += "-verbose" @@ -588,6 +589,9 @@ function Publish-NuGetFeed [string]$VersionSuffix ) + # Add .NET CLI tools to PATH + Find-Dotnet + @( 'Microsoft.PowerShell.Commands.Management', 'Microsoft.PowerShell.Commands.Utility', @@ -885,6 +889,9 @@ function Start-TypeGen throw "Start-TypeGen is not supported on non-windows. Use src/TypeCatalogGen/build.sh instead" } + # Add .NET CLI tools to PATH + Find-Dotnet + Push-Location "$PSScriptRoot/src/TypeCatalogParser" try { @@ -933,6 +940,24 @@ function Start-ResGen } +function Find-Dotnet() { + $originalPath = $env:PATH + $dotnetPath = if ($IsWindows) { + "$env:LocalAppData\Microsoft\dotnet" + } else { + "$env:HOME/.dotnet" + } + + if (-not (precheck 'dotnet' "Could not find 'dotnet', appending $dotnetPath to PATH.")) { + $env:PATH += [IO.Path]::PathSeparator + $dotnetPath + } + + if (-not (precheck 'dotnet' "Still could not find 'dotnet', restoring PATH.")) { + $env:PATH = $originalPath + } +} + + function script:log([string]$message) { Write-Host -Foreground Green $message }