Private/Get-NuGetPath.ps1
$script:ModuleRoot = $MyInvocation.ScriptName | Split-Path -Parent function Get-NuGetPath { [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Low')] [OutputType([string])] param ( # LibraryDirectory [Parameter(Mandatory = $true, Position = 0, ValueFromPipelineByPropertyName = $true)] [ValidateNotNullOrEmpty()] [String]$LibraryDirectory, # Force download of NuGet if not found on system [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true)] [switch]$Force ) process { Write-Verbose "Finding NuGet.exe path" $NUGET_URL = "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe" $NUGET_EXE = Join-Path $LibraryDirectory "nuget.exe" # Try find NuGet.exe in path if not exists if (-not (Test-Path $NUGET_EXE)) { Write-Verbose -Message "Trying to find nuget.exe in PATH..." $existingPaths = $Env:Path -Split ';' | Where-Object { (![string]::IsNullOrEmpty($_)) -and (Test-Path $_ -PathType Container) } $NUGET_EXE_IN_PATH = Get-ChildItem -Path $existingPaths -Filter "nuget.exe" | Select-Object -First 1 if ($NUGET_EXE_IN_PATH -ne $null -and (Test-Path $NUGET_EXE_IN_PATH.FullName)) { Write-Verbose -Message "Found in PATH at $($NUGET_EXE_IN_PATH.FullName)." $NUGET_EXE = $NUGET_EXE_IN_PATH.FullName } } if (-not (Test-Path $NUGET_EXE)) { if ($Force -or $PSCmdlet.ShouldContinue("NuGet.exe not found on your computer", "Would you like to download NuGet?") ) { # Ensure Lib folder exists if (-not (Test-Path $LibraryDirectory -PathType Container)) { Write-Verbose -Message "Creating lib directory..." New-Item -Path $LibraryDirectory -ItemType Directory -Force | Out-Null } Invoke-WebRequest -Uri $NUGET_URL -OutFile $NUGET_EXE | Out-Null Unblock-File -Path $NUGET_EXE -ErrorAction:SilentlyContinue | Out-Null } } # Attempt to export the NuGet.exe path Write-Verbose "Exporting NuGet.exe path as variable NUGET_EXE" if (Test-Path $NUGET_EXE) { $NUGET_EXE } else { Throw "Could not find any path to NuGet.exe." } } } |