public/Update-ChocolateyInstallVarsFromConfig.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
111
112
113
114
115
116
117
118
119
120
function Update-ChocolateyInstallVarsFromConfig() {
    Param(
        [PSCustomObject] $Config,

        [String] $ConfigUri
    )
    <#
        {
            install: {
                path: ""
                toolsLocation: ""
                downloadUrl: ""
                version: ""
                useWindowsCompression:
                proxy: {
                    ignore: false
                    location:
                    user:
                    password:
                }
            }
        }
    #>

    
    if(!$Config) {
        if(!$ConfigUri) {
            $ConfigUri = $Env:ChocolateyInstallConfig

            if(!$ConfigUri) {
                $ConfigUri = $Env:CHOCOLATEY_INSTALL_CONFIG
            } 
        }
            
        if(!$ConfigUri) {
            Write-Debug "ConfigUri is not set."
            return;
        }
       
        $tmpDir = Get-ChocolateyTempInstallDirectory

        $test = [Uri]$ConfigUri
        $configPath = $null;
        if(!$test.IsFile) {
            $configPath = Join-Path $tmpDir "config.json"
            Get-WebRequestContentAsFile -Uri $ConfigUri $configPath   
            $ConfigUri = $configPath    
        } 
    
        try {
            $config  = Get-Content $ConfigUri -Raw | ConvertFrom-Json 
        } catch {
            Write-Warning "Could not load $ConfigUri" 
            Write-Debug $_.Exception
            return;
        }
    }
   

    $install = $config.install

    if($install -is [string]) {
        if($install -match "chocolatey") {
            
            if($Env:ChocolateyInstall -ne $install.path) {
                $Env:ChocolateyInstall = $install;
                if(Test-IsAdmin) {
                    [System.Environment]::SetEnvironmentVariable("ChocolateyInstall", $install.Path, "Machine")
                } else {
                    [System.Environment]::SetEnvironmentVariable("ChocolateyInstall", $install.Path, "User")
                }
            }
            return;
        }

        $Env:ChocolateyToolsLocation = $install;
        $p = Join-Path $install "chocolatey"
        if($Env:ChocolateyInstall -ne $p) {
            $Env:ChocolateyInstall = $p;
            if(Test-IsAdmin) {
                [System.Environment]::SetEnvironmentVariable("ChocolateyInstall", $install.Path, "Machine")
            } else {
                [System.Environment]::SetEnvironmentVariable("ChocolateyInstall", $install.Path, "User")
            }
        }
        return;
    }

    if($install.path) {
        if($Env:ChocolateyInstall -ne $install.path) {
            if(Test-IsAdmin) {
                [System.Environment]::SetEnvironmentVariable("ChocolateyInstall", $install.Path, "Machine")
            } else {
                [System.Environment]::SetEnvironmentVariable("ChocolateyInstall", $install.Path, "User")
            }
        }
        $Env:ChocolateyInstall = $install.path         
    } 
    if($install.toolsLocation) {
        $Env:ChocolateyToolsLocation = $install.toolsLocation
    }
    if($install.version) {
        $Env:ChocolateyVersion = $install.version 
    }
    if($install.downloadUrl) {
        $Env:ChocolatelyDownloadUrl = $install.downloadUrl;
    }
    if($install.useWindowsCompression) {
        $Env:ChocolateyUseWindowsCompression = 'true';
    }
    if($install.proxy) {
        $p = $install.proxy;
        if($p.ignore) {
            $env:ChocolateyIgnoreProxy = 'true'
        } else {
            if($p.location) { $Env:ChocolateyProxyLocation = $p.location; }
            if($p.user) { $Env:ChocolateyProxyUser = $p.user }
            if($p.password) { $Env:ChocolateyProxyPassword = $p.password; }
        }
    }   
}