Classes/ParcelFactory.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
113
114
115
class ParcelFactory
{
    hidden [hashtable] $Providers
    static [ParcelFactory] $_instance

    static [ParcelFactory] Instance()
    {
        if ($null -eq [ParcelFactory]::_instance) {
            [ParcelFactory]::_instance = [ParcelFactory]::new()
        }

        return [ParcelFactory]::_instance
    }

    ParcelFactory()
    {
        $this.Providers = @{}
    }

    [ParcelProvider] GetProvider([string]$_name)
    {
        $this.AddProvider($_name)
        return $this.Providers[$_name]
    }

    [void] AddProvider([string]$_name)
    {
        $_provider = $this.Providers[$_name]
        if ($null -ne $_provider) {
            return
        }

        $_provider = $this.GetProviderInternal($_name)
        $this.Providers[$_name] = $_provider
    }

    [int] InstallProviders([hashtable]$_context, [bool]$_dryRun)
    {
        $_installed = 0

        foreach ($_name in $this.Providers.Keys)
        {
            $_provider = $this.Providers[$_name]

            # do nothing if provider is installed
            if ($_provider.TestProviderInstalled($_context)) {
                continue
            }

            # otherwise, attempt at installing it
            Write-ParcelPackageHeader -Message "$($_provider.Name) [Provider]"

            $result = $_provider.InstallProvider($_context, $_dryRun)
            $result.WriteStatusMessage($_dryRun)
            $_installed++

            Write-Host ([string]::Empty)
        }

        return $_installed
    }

    [ParcelProvider] GetProviderInternal([string]$_name)
    {
        if ([string]::IsNullOrWhiteSpace($_name)) {
            throw "Provider name in Factory cannot be empty"
        }

        $_provider = $null

        switch ($_name.ToLowerInvariant()) {
            { @('choco', 'chocolatey') -icontains $_name } {
                $_provider = [ChocoParcelProvider]::new()
            }

            { @('psgallery', 'ps-gallery') -icontains $_name } {
                $_provider = [PSGalleryParcelProvider]::new()
            }

            { @('scoop') -icontains $_name} {
                $_provider = [ScoopParcelProvider]::new()
            }

            { @('brew', 'homebrew') -icontains $_name } {
                $_provider = [BrewParcelProvider]::new()
            }

            { @('docker') -icontains $_name} {
                $_provider = [DockerParcelProvider]::new()
            }

            { @('winfeature', 'win-feature', 'windows-feature', 'windowsfeatures', 'windows-features') -icontains $_name } {
                $_provider = [WindowsFeatureParcelProvider]::new()
            }

            { @('dism', 'windowsdism', 'win-dism') -icontains $_name } {
                $_provider = [WindowsDISMParcelProvider]::new()
            }

            { @('aptget', 'apt-get') -icontains $_name} {
                $_provider = [AptGetParcelProvider]::new()
            }

            { @('yum') -icontains $_name} {
                $_provider = [YumParcelProvider]::new()
            }

            default {
                throw "Invalid package provider supplied: $($_name)"
            }
        }

        return $_provider
    }
}