public/Install-Chocolatey.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110

function Install-Chocolatey() {
    Param(
        [Parameter(Position = 0, ValueFromPipeline)]
        [PsCustomObject] $Config,

        [Parameter(ValueFromPipeline = $true)]
        [String] $ConfigUri,

        [Switch] $Force 
    )

    Write-Banner;
    
    $choco = Get-Command choco -ErrorAction SilentlyContinue
    $location = Get-ChocolateyInstallLocation 

    if($choco -or (Test-Path $location)) {
        if(!$Force.ToBool()) {
            Write-Debug "Chocolatey is already installed. Use -Force to re-install."
            return;
        }
    }

    function Update-ProcessPath() {

        $chocolateyInstall = Get-ChocolateyInstallLocation
        $bin = Join-Path $chocolateyInstall 'bin'

        if(! ($env:Path -match ($bin.Replace("\", "\\")))) {
            $Env:Path = [System.Environment]::GetEnvironmentVariable("Path", "Machine")
        }

        if(! ($env:Path -match ($bin.Replace("\", "\\")))) {
            $Env:Path = [System.Environment]::GetEnvironmentVariable("Path", "User")
        }
    }

    function Update-SystemPath() {

        $chocolateyInstall = Get-ChocolateyInstallLocation
        $bin = Join-Path $chocolateyInstall 'bin'
        
        $isAdmin = Test-IsAdmin;
        if($isAdmin) {
            $path =  [System.Environment]::GetEnvironmentVariable("Path", "Machine")
            if(! ($path -match ($bin.Replace("\", "\\")))) {
                [System.Environment]::SetEnvironmentVariable("Path", "$path;$bin",  "Machine")
            }
        } else {
            $path =  [System.Environment]::GetEnvironmentVariable("Path", "User")
            if(! ($path -match ($bin.Replace("\", "\\")))) {
                [System.Environment]::SetEnvironmentVariable("Path", "$path;$bin",  "User")
            }
        }
    }

    function Copy-ChocolateyPackage() {
        Param(
            [Parameter(Position = 1)]
            [String] $Path 
        )

        $chocolateyInstall = Get-ChocolateyInstallLocation
        $chocolateyPkgDir = Join-Path $chocolateyInstall 'lib'
        $chocolateyPkgDir = Join-Path $chocolateyPkgDir "chocolatey"
        $chocolateyNupkg = Join-Path $chocolateyPkgDir 'chocolatey.nupkg'

        if(-not (Test-Path $chocolateyPkgDir)) {
            New-Item $chocolateyPkgDir -ItemType Directory -Force | Out-Null 
        }

        Write-Debug 'Ensuring chocolatey.nupkg is in the lib folder'
        Copy-Item $Path $chocolateyNupkg -Force  
    }

    Update-PowerShellOutputRedirection
    Update-SecurityProtocol
    Update-ChocolateyInstallVarsFromConfig -Config $Config -ConfigUri $ConfigUri


    $uri = Get-ChocolateyDownloadUri
    $tmpDir = Get-ChocolateyTempInstallDirectory
    $zip = Join-Path $tmpDir "chocolatey.zip"



    Write-Host "Getting Chocolatey from $uri." -ForegroundColor Green
    Write-Host "Downloading $uri to $zip" -ForegroundColor Green
    Save-WebRequestContentAsFile -Uri $uri -Destination $zip  
    Write-Host "Expanding $zip in $tmpDir"
    Expand-ChocolateyArchive -Path $zip -Destination $tmpDir -Force

    Write-Host "Executing Chocolatey Install"  -ForegroundColor Green
    Invoke-ChocolateySetupScript

    Write-Host "Ensure Path for System" -ForegroundColor Green
    Update-SystemPath 

    
    Write-Host "Ensure Path for current session is updated"  -ForegroundColor Green
    Update-ProcessPath

    Write-Host "Copy chocolatey.nupkg to install location"  -ForegroundColor Green
    Copy-ChocolateyPackage -Path $tmpDir


}