Private/Chocolatey/Chocolatey.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
# Some types/classes for our chocolatey cmdlets
class ChocolateyPackage
{
    [string]$name
    [string]$version

    # This constructor allows us to instantiate the class with a PSCustomObject (useful for ingesting CSV files and the like)
    ChocolateyPackage([pscustomobject]$ChocolateyPackage)
    {
        if (!$ChocolateyPackage.version)
        {
            $this.version = 'any'
        }
        else
        {
            $this.version = $ChocolateyPackage.version
        }
        $this.name = $ChocolateyPackage.name
        $this.VersionCheck()
    }
    # This constructor allows us to instantiate the raw class by passing in the two string values
    ChocolateyPackage([string]$name, [string]$version)
    {
        $this.name = $name
        $this.version = $version
        $this.VersionCheck()
    }
    # This constructor allows us to instantiate the raw class by passing in the just the package name
    ChocolateyPackage([string]$name)
    {
        $this.name = $name
        $this.version = 'any'
    }
    # This constructor allows us to instantiate the class from a hashtable (useful for when we want to create objects on the fly)
    ChocolateyPackage([hashtable]$ChocolateyPackage)
    {
        # If the user hasn't specified a version then we default to 'any'
        if (!$ChocolateyPackage.version)
        {
            $this.version = 'any'
        }
        else
        {
            $this.version = $ChocolateyPackage.version
        }
        $this.VersionCheck()
        $this.name = $ChocolateyPackage.name
    }
    <#
        A hidden method that performs some rudimentary validation to make sure the value of 'version' is either in our list
        of accepted strings OR is a valid version number
    #>

    hidden VersionCheck()
    {
        if ($this.version -notin @('present', 'any', 'installed'))
        {
            [version]$this.version
        }
    }
}