public/Test-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
function Test-SemanticVersion {
    <#
     .SYNOPSIS
        Tests if a string is a valid Semantic Version.
 
     .DESCRIPTION
        The Test-SemanticVersion function verifies that a supplied string meets the Semantic Version 2.0 specification.
 
        If an invalid Semantic Version string is supplied to Test-SemanticVersion and the Verbose switch is used, the
        verbose output stream will include additional details that may help when troubleshooting an invalid version.
 
     .EXAMPLE
        Test-SemanticVersion '1.2.3-alpha.1+build.456'
 
        True
 
        This example shows the result if the provided string is a valid Semantic Version.
 
     .EXAMPLE
        Test-SemanticVersion '1.2.3-alpha.01+build.456'
 
        False
 
        This example shows the result if the provided string is not a valid Semantic Version.
 
     .INPUTS
        System.Object
 
            Any object you pipe to this function will be converted to a string and tested for validity.
 
    #>

    [CmdletBinding(DefaultParameterSetName='BoolOutput')]
    [Alias('tsemver')]
    [OutputType([bool])]
    param (
        # The Semantic Version string to validate.
        [Parameter(Mandatory=$true,
                   ValueFromPipeline=$true,
                   Position=0)]
        [object[]]
        [Alias('Version', 'v')]
        $InputObject
    )

    process {
        foreach ($item in $InputObject) {
            [string] $version = $item -as [string]

            $debugHash = Debug-SemanticVersion -InputObject $item -ParameterName InputObject
            Write-Verbose -Message ($debugHash.Message + ' ' + $debugHash.RecommendedAction)

            $version -match ('^' + $SemanticVersionPattern + '$')
        }
    }
}


Export-ModuleMember -Function Test-SemanticVersion -Alias tsemver