Classes/Providers/AptGetParcelProvider.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
class AptGetParcelProvider : ParcelProvider
{
    AptGetParcelProvider() : base('Apt-Get', $false, [string]::Empty) {}

    [bool] TestProviderInstalled([hashtable]$_context)
    {
        # fail if apt-get isn't available
        if ($null -eq (Get-Command -Name 'apt-get' -ErrorAction Ignore)) {
            throw 'The provider apt-get is not installed'
        }

        return $true
    }

    [string] GetPackageInstallScript([ParcelPackage]$_package, [hashtable]$_context)
    {
        return "sudo apt-get install --yes $($_package.Name)=$($this.GetVersionArgument($_package)) 2>&1"
    }

    [string] GetPackageUninstallScript([ParcelPackage]$_package, [hashtable]$_context)
    {
        return "sudo apt-get remove --purge --yes $($_package.Name) 2>&1"
    }

    [bool] TestPackageInstalled([ParcelPackage]$_package)
    {
        $result = (Invoke-Expression -Command "`$r = dpkg -s $($_package.Name) 2>&1; if (!`$?) { return `$null }; return `$r")
        if ($null -eq $result) {
            return $false
        }

        return (($result -imatch "^version\:\s+$($this.GetVersionArgument($_package))").Length -gt 0)
    }

    [bool] TestPackageUninstalled([ParcelPackage]$_package)
    {
        $result = (Invoke-Expression -Command "`$r = dpkg -s $($_package.Name) 2>&1; if (!`$?) { return `$null }; return `$r")
        return ($null -eq $result)
    }

    [string] GetPackageLatestVersion([ParcelPackage]$_package)
    {
        $result = Invoke-Expression -Command "apt-cache madison $($_package.Name) 2>&1"
        if (($null -eq $result) -or ($result.Length -eq 0)) {
            throw "The $($_package.Name) package was not found on apt-get"
        }

        return ($result[0] -split '\|')[1].Trim()
    }

    [string] GetVersionArgument([ParcelPackage]$_package)
    {
        return $_package.Version
    }
}