Classes/ParcelPackage.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
class ParcelPackage
{
    [string] $Name
    [string] $ProviderName
    [string[]] $Source
    [ParcelArguments] $Arguments

    [string] $Version
    [bool] $IsLatest = $false

    [ParcelEnsureType] $Ensure
    [string] $When
    [string[]] $Environment
    [ParcelOSType] $OS

    [ParcelScripts] $Scripts

    # base constructor
    ParcelPackage([hashtable]$_package)
    {
        # fail on no name
        if ([string]::IsNullOrWhiteSpace($_package.name)) {
            throw "No name supplied for $($_package.provider) package"
        }

        # set ensure to default
        if ([string]::IsNullOrWhiteSpace($_package.ensure)) {
            $_package.ensure = 'neutral'
        }

        # set environment to default
        if ([string]::IsNullOrWhiteSpace($_package.environment)) {
            $_package.environment = @('all')
        }

        # set os to default
        if ([string]::IsNullOrWhiteSpace($_package.os)) {
            $_package.os = 'all'
        }

        # if version is empty, assume latest
        if ([string]::IsNullOrWhiteSpace($_package.version)) {
            $_package.version = 'latest'
        }

        # are we using the latest version?
        if ([string]::IsNullOrWhiteSpace($_package.version) -or ($_package.version -ieq 'latest')) {
            $this.IsLatest = $true
        }

        # set the properties
        $this.Name = $_package.name
        $this.Version = $_package.version
        $this.ProviderName = $_package.provider
        $this.Source = $_package.source
        $this.Arguments = [ParcelArguments]::new($_package.args)

        $this.Ensure = [ParcelEnsureType]$_package.ensure
        $this.OS = $_package.os
        $this.Environment = @($_package.environment)
        $this.When = $_package.when

        # set the scripts
        $this.Scripts = [ParcelScripts]::new($_package.pre, $_package.post)
    }

    [ParcelStatus] TestPackage([hashtable]$_context)
    {
        # check the environment
        $status = $this.TestEnvironment($_context.environment)
        if ($null -ne $status) {
            return $status
        }

        # check the os
        $status = $this.TestOS($_context.os.type)
        if ($null -ne $status) {
            return $status
        }

        # check the when script
        $status = $this.TestWhen()
        if ($null -ne $status) {
            return $status
        }

        return $null
    }

    [ParcelStatus] TestEnvironment([string]$_environment)
    {
        if (($this.Environment.Length -eq 0) -or ($this.Environment.Length -eq 1 -and $this.Environment[0] -ieq 'all')) {
            return $null
        }

        $valid = ($this.Environment -icontains $_environment)
        if ($valid) {
            return $null
        }

        return [ParcelStatus]::new('Skipped', "Wrong environment [$($this.Environment -join ', ') =/= $($_environment)]")
    }

    [ParcelStatus] TestOS([string]$_os)
    {
        if ([string]::IsNullOrWhiteSpace($this.OS) -or ('all' -ieq $this.OS)) {
            return $null
        }

        $valid = ($_os -ieq $this.OS)
        if ($valid) {
            return $null
        }

        return [ParcelStatus]::new('Skipped', "Wrong OS [$($this.OS) =/= $($_os)]")
    }

    [ParcelStatus] TestWhen()
    {
        if ([string]::IsNullOrWhiteSpace($this.When)) {
            return $null
        }

        $result = [bool](Invoke-Command -ScriptBlock ([scriptblock]::Create($this.When)) -ErrorAction Stop)
        if ($result) {
            return $null
        }

        return [ParcelStatus]::new('Skipped', 'When evaluated to false')
    }
}