Functions/Install-ToolsPackageFromNuget.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
function Install-ToolsPackageFromNuget {
    [CmdletBinding()]
    param($PackagePath, $package, $subPath, $version, $environmentVariablesToSet
        ,$nugetextraparams    )

    Write-Host "Installing Package $package"
    Write-Verbose "Using nuget in $($env:nugetPath)"
    $testPath = Join-Path(Join-Path $PackagePath $package) $subPath
    
    if (-not (Test-path $testPath)) {
        $nugetparams = @("install", $package, "-ExcludeVersion", "-OutputDirectory", $PackagePath)
        if ($version) { $nugetparams += "-version", $version }
        &($env:nugetPath) $nugetparams @nugetextraparams
    }
    if ($environmentVariablesToSet) {
        $environmentVariablesToSet | foreach-object {
            Write-verbose "Setting $_ to $testPath"
           if ($_ -eq "path" -and $env:PATH.split([io.path]::PathSeparator) -notcontains $testPath) {
                $env:PATH= $env:PATH + [io.path]::PathSeparator + "$testPath"
           }
               else{
                set-item -path "env:$_" -value $testPath
            } 
        }
    }
}