functions/helpers.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

######## helpers: ###################


function install-chocolatey ($version = $null) {
    if (!(test-choco)) {
            Write-Warning "chocolatey not found, installing"

            #$version = "0.9.8.33"
            $s = (new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1')
            if ($version -ne $null) {
                $s = $s -replace "https://chocolatey.org/api/v2/package/chocolatey","https://chocolatey.org/api/v2/package/chocolatey/$version"
                $s = $s -replace "https://packages.chocolatey.org/.*\.nupkg","https://chocolatey.org/api/v2/package/chocolatey/$version"
            }
            iex $s
            cmd /c "SET PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"
            $env:Path = [System.Environment]::GetEnvironmentVariable("PATH",[System.EnvironmentVariableTarget]::Machine) + ";" + [System.Environment]::GetEnvironmentVariable("PATH",[System.EnvironmentVariableTarget]::User) 
            if (!(test-choco)) {
                write-error "chocolatey still not found! Installation failed?"
            }
            $path = "$env:ChocolateyInstall\chocolateyinstall\chocolatey.config"
            if (test-path $path) {
                write-host "setting chocolatey config 'ksMessage' to 'false' in config file '$path'"
                $xml = [xml](Get-Content $path)
                $xml.chocolatey.ksMessage = "false"
                $xml.Save($path)
            }
    } 
    else 
    {
        write-host "chocolatey is already installed"       
    }
}

######## chocolatey helpers
$global:installed = $null

function test-choco {
    test-command "choco"
}

function test-command([string] $cmd) {
    return Get-Command $cmd -ErrorAction Ignore
}

function _ensure-choco() {
    if (!( test-command "choco")) {
        install-chocolatey 
    }
}

function refresh-modulepath {
 $curpath = $env:PSModulePath
 
 $paths = @($curpath.split(";"))
 $paths = $paths | select -Unique

 $p = [System.Environment]::GetEnvironmentVariable("PSModulePath", [System.EnvironmentVariableTarget]::User)
 if ($p -ne $null) {
    $toadd = $p.Split(";") | select -Unique | ? { $_ -cnotin $paths }        
    $paths = @($toadd) + $paths
 }
 
 $p = [System.Environment]::GetEnvironmentVariable("PSModulePath", [System.EnvironmentVariableTarget]::machine)
 if ($p -ne $null) {
     $toadd = $p.Split(";") | select -Unique | ? { $_ -cnotin $paths }        
     $paths = @($toadd) + $paths
}

 $env:PSModulePath = [string]::Join(";", $paths)
}