public/Step-SemanticVersion.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
132
133
134
135
136
function Step-SemanticVersion {
    <#
     .SYNOPSIS
        Increments a Semantic Version number.
 
     .DESCRIPTION
        The Step-SemanticVersion function increments the elements of a Semantic Version number in a way that is
        compliant with the Semantic Version 2.0 specification.
 
        - Incrementing the Major number will reset the Minor number and the Patch number to 0. A pre-release version
          will be incremented to the normal version number.
        - Incrementing the Minor number will reset the Patch number to 0. A pre-release version will be incremented to
          the normal version number.
        - Incrementing the Patch number does not change any other parts of the version number. A pre-release version
          will be incremented to the normal version number.
        - Incrementing the PreRelease number does not change any other parts of the version number.
        - Incrementing the Build number does not change any other parts of the version number.
 
     .EXAMPLE
        '1.1.1' | Step-SemanticVersion
 
        Major : 1
        Minor : 1
        Patch : 2
        PreRelease : 0
        Build :
 
        This command takes a semantic version string from the pipeline and increments the pre-release version. Because
        the element to increment was not specified, the default value of 'PreRelease was used'.
 
     .EXAMPLE
        Step-SemanticVersion -Version 1.1.1 -Level Minor
 
        Major : 1
        Minor : 2
        Patch : 0
        PreRelease :
        Build :
 
        This command converts the string '1.1.1' to the semantic version object equivalent of '1.2.0'.
 
     .EXAMPLE
        Step-SemanticVersion -v 1.1.1 -i patch
 
        Major : 1
        Minor : 1
        Patch : 2
        PreRelease :
        Build :
 
        This command converts the string '1.1.1' to the semantic version object equivalent of '1.1.2'. This example
        shows the use of the parameter aliases "v" and "i" for Version and Level (increment), respectively.
 
     .EXAMPLE
        Step-SemanticVersion 1.1.1 Major
 
        Major : 2
        Minor : 0
        Patch : 0
        PreRelease :
        Build :
 
        This command converts the string '1.1.1' to the semantic version object equivalent of '2.0.0'. This example
        shows the use of positional parameters.
 
    #>

    [CmdletBinding()]
    [Alias('stsemver')]
    [OutputType('PoshSemanticVersion')]
    param (
        # The Semantic Version number to be incremented.
        [Parameter(Mandatory=$true,
                   ValueFromPipeline=$true,
                   Position=0)]
        [ValidateScript({
            if (Test-SemanticVersion -InputObject $_) {
                return $true
            }
            else {
                $erHash = Debug-SemanticVersion -InputObject $_ -ParameterName InputObject
                #$er = Write-Error @erHash 2>&1
                $er = New-Object -TypeName System.Management.Automation.ErrorRecord -ArgumentList $erHash['Exception'], $erHash['ErrorId'], $erHash['Category'], $_
                throw ($er)
            }
        })]
        [Alias('Version', 'v')]
        $InputObject,

        # The desired increment type.
        # Valid values are Build, PreRelease, PrePatch, PreMinor, PreMajor, Patch, Minor, or Major.
        # The default value is PreRelease.
        [Parameter(Position=1)]
        [string]
        [ValidateSet('Build', 'PreRelease', 'PrePatch', 'PreMinor', 'PreMajor', 'Patch', 'Minor', 'Major')]
        [Alias('Level', 'Increment', 'i')]
        $Type = 'PreRelease',

        # The metadata label to use with an incrament type of Build, PreRelease, PreMajor, PreMinor, or PrePatch.
        # If specified, the value replaces the existing label. If not specified, the existing label will be incremented.
        # This parameter is ignored for an increment type of Major, Minor, or Patch.
        [Parameter(Position=2)]
        [string]
        [Alias('preid', 'Identifier')]
        $Label
    )

    process {
        foreach ($obj in $InputObject) {
            $newSemVer = New-SemanticVersion -InputObject $obj

            if ($PSBoundParameters.ContainsKey('Label')) {
                try {
                    $newSemVer.Increment($Type, $Label)
                }
                catch [System.ArgumentOutOfRangeException],[System.ArgumentException] {
                    $ex = $_.Exception
                    $er = New-Object -TypeName System.Management.Automation.ErrorRecord -ArgumentList $ex, 'InvalidSemVer', ([System.Management.Automation.ErrorCategory]::InvalidArgument), $obj
                    $PSCmdlet.ThrowTerminatingError($er)
                }
                catch {
                    $ex = $_.Exception
                    $er = New-Object -TypeName System.Management.Automation.ErrorRecord -ArgumentList $ex, 'UnableToIncrement', ([System.Management.Automation.ErrorCategory]::InvalidOperation), $obj
                    $PSCmdlet.ThrowTerminatingError($er)
                }
            }
            else {
                $newSemVer.Increment($Type)
            }

            $newSemVer
        }
    }
}


Export-ModuleMember -Function Step-SemanticVersion -Alias stsemver