Classes/Providers/ScoopParcelProvider.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
class ScoopParcelProvider : ParcelProvider
{
    ScoopParcelProvider() : base('Scoop', $true, [string]::Empty) {}

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

    [scriptblock] GetProviderInstallScriptBlock([hashtable]$_context)
    {
        return {
            Set-ExecutionPolicy RemoteSigned -Scope Process -Force | Out-Null
            Invoke-Expression (New-Object System.Net.WebClient).DownloadString('https://get.scoop.sh') | Out-Null
        }
    }

    [string] GetPackageInstallScript([ParcelPackage]$_package, [hashtable]$_context)
    {
        $_version = $this.GetVersionArgument($_package)
        if (![string]::IsNullOrWhiteSpace($_version)) {
            $_version = "@$($_version)"
        }

        return "scoop install $($_package.Name)$($_version)"
    }

    [string] GetPackageUninstallScript([ParcelPackage]$_package, [hashtable]$_context)
    {
        return "scoop uninstall $($_package.Name) -p"
    }

    [string] GetProviderRemoveSourceScript([string]$_name)
    {
        return "scoop bucket rm $($_name); if (`$LASTEXITCODE -eq 0) { `$LASTEXITCODE = 0 }"
    }

    [string] GetProviderAddSourceScript([string]$_name, [string]$_url)
    {
        return "scoop bucket add $($_name) $($_url)"
    }

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

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

    [string] GetPackageLatestVersion([ParcelPackage]$_package)
    {
        $result = Invoke-ParcelPowershell -Command "scoop search $($_package.Name)"

        $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
    }

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

        return $_package.Version
    }
}