Functions/Install-Nuget.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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<#
    .SYNOPSIS
        Installs latest version of nuget
    .DESCRIPTION
        If not installed will download the latest copy of nuget and put it in the folder
    .PARAMETER fallbackNugetPath
        Location to store nuget
    .EXAMPLE
        Install-Nuget
    .NOTES
        FunctionName : Install-Nuget
 
        Created by : Sabin.io
    .LINK
#>

function Get-Platform(){
    "{0}" -f $PSVersionTable.Platform
}
function Install-Nuget {
    [CmdletBinding()]
    param([string]$fallbackNugetPath )
    Write-Verbose "Installing Nuget"

    if("" -eq "$fallbackNugetPath"){
        if ("Unix" -eq (Get-Platform)){
            $fallbackNugetPath = [IO.Path]::Combine("root","Programdata","Nuget","Nuget.exe")
        }
        else{
            $fallbackNugetPath = [IO.Path]::Combine("c:\","Programdata","Nuget","Nuget.exe")
        }
    }
    if (Test-Path $fallbackNugetPath) {
        $nugetPath = $fallbackNugetPath
    }
    else {

        New-ToolPath (Split-Path $fallbackNugetPath -Parent);

        Write-Verbose "Downloading&Installing Nuget latest\"
        Invoke-WebRequest -Uri "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe" -OutFile $fallbackNugetPath
        $nugetpath = $fallbackNugetPath
    }
    Write-Verbose "Nuget found $($nugetPath)"
    $env:nugetPath = $nugetPath

    $nugetDir =[System.io.Path]::GetDirectoryName($env:nugetPath);
    If (($env:PATH -split [IO.Path]::PathSeparator) -notcontains $nugetDir) {
        $env:PATH += [IO.Path]::PathSeparator + $nugetDir
}

}