Classes/Providers/ChocoParcelProvider.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
class ChocoParcelProvider : ParcelProvider
{
    ChocoParcelProvider() : base('Chocolatey', $false, 'chocolatey') {}

    [bool] TestProviderInstalled([hashtable]$_context)
    {
        $cmd = Get-Command -Name 'choco' -ErrorAction Ignore
        return ($null -ne $cmd)
    }

    [scriptblock] GetProviderInstallScriptBlock([hashtable]$_context)
    {
        return {
            Set-ExecutionPolicy Bypass -Scope Process -Force | Out-Null
            Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1')) | Out-Null
        }
    }

    [string] GetPackageInstallScript([ParcelPackage]$_package, [hashtable]$_context)
    {
        return "choco install $($_package.Name) --no-progress -y -f --allow-unofficial --allow-downgrade"
    }

    [string] GetPackageUninstallScript([ParcelPackage]$_package, [hashtable]$_context)
    {
        return "choco uninstall $($_package.Name) --no-progress -y -f -x --allversions"
    }

    [string] GetProviderRemoveSourceScript([string]$_name)
    {
        return "choco source remove --name $($_name) -f -y"
    }

    [string] GetProviderAddSourceScript([string]$_name, [string]$_url)
    {
        return "choco source add --name $($_name) --source $($_url) -f -y --no-progress --allow-unofficial"
    }

    [bool] TestPackageInstalled([ParcelPackage]$_package)
    {
        $result = Invoke-Expression -Command "choco list -lo $($_package.Name) $($this.GetVersionArgument($_package))"
        return ((@($result) -imatch "$($_package.Name)\s+[0-9\.]+").Length -gt 0)
    }

    [bool] TestPackageUninstalled([ParcelPackage]$_package)
    {
        $result = Invoke-Expression -Command "choco list -lo $($_package.Name)"
        return ((@($result) -imatch "$($_package.Name)\s+[0-9\.]+").Length -eq 0)
    }

    [string] GetPackageLatestVersion([ParcelPackage]$_package)
    {
        $result = Invoke-Expression -Command "choco search $($_package.Name) --exact $($this.GetSourceArgument($_package)) --allow-unofficial"

        $regex = "$($_package.Name)\s+(?<version>[0-9\._]+)"
        $result = @(@($result) -imatch $regex)

        if (($result.Length -gt 0) -and ($result[0] -imatch $regex)) {
            return $Matches['version']
        }

        return [string]::Empty
    }

    [bool] TestExitCode([int]$_code, [string]$_output, [string]$_action)
    {
        # valid exit codes
        if (@(0, 3010) -icontains $_code) {
            return $true
        }

        # valid outputs
        switch ($_action.ToLowerInvariant()) {
            'install' {
                return (($_output -ilike '*has been successfully installed*') -or ($_output -ilike '*has been installed*'))
            }

            'uninstall' {
                return (($_output -ilike '*has been successfully uninstalled*') -or ($_output -ilike '*Cannot uninstall a non-existent package*'))
            }

            'source' {
                return $true
            }
        }

        return $false
    }

    [string] GetVersionArgument([ParcelPackage]$_package)
    {
        if ($_package.IsLatest) {
            return [string]::Empty
        }

        return "--version $($_package.Version)"
    }

    [string] GetSourceArgument([ParcelPackage]$_package)
    {
        $_source = $_package.Source
        if ([string]::IsNullOrWhiteSpace($_source)) {
            $_source = @($this.DefaultSource)
        }

        if ([string]::IsNullOrWhiteSpace($_source[0])) {
            return [string]::Empty
        }

        return "--source $($_source[0])"
    }
}