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

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

    [string] GetPackageInstallScript([ParcelPackage]$_package, [hashtable]$_context)
    {
        $_script = "`$env:HOMEBREW_NO_AUTO_UPDATE = '1'; "

        if ($this.TestIsOnlineCask($_package)) {
            if ($_context.os.type -ine 'macos') {
                throw "Brew casks are only supported on MacOS"
            }

            $_script += "brew cask install --force $($_package.Name)"
        }
        else {
            $_script += "brew install --force $($_package.Name)"
        }

        return "$($_script) @PARCEL_NO_VERSION"
    }

    [string] GetPackageUninstallScript([ParcelPackage]$_package, [hashtable]$_context)
    {
        if ($this.TestIsLocalCask($_package)) {
            return "brew cask uninstall --force $($_package.Name)"
        }
        else {
            return "brew uninstall --force $($_package.Name)"
        }
    }

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

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

    [string] GetPackageLatestVersion([ParcelPackage]$_package)
    {
        return [string]::Empty
    }

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

        return $_package.Version
    }

    [bool] TestIsOnlineCask([ParcelPackage]$_package)
    {
        $result = @(Invoke-Expression -Command "brew search --casks $($_package.Name)")
        return ($result[0] -ilike '*casks*')
    }

    [bool] TestIsLocalCask([ParcelPackage]$_package)
    {
        $result = @(Invoke-Expression -Command "brew cask list --versions $($_package.Name) 2>&1")
        return (($result -imatch "$($_package.Name)\s+[0-9\._]+").Length -gt 0)
    }
}