Public/Convert-NcrementVersionNumberToString.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
<#
.SYNOPSIS
Returns the string representation of a [Manifest] version object.
 
.PARAMETER InputObject
The [Manifest] object or [Version] object.
 
.PARAMETER Branch
The current branch name from source control.
 
.PARAMETER AppendSuffix
Determine wether to append the version suffix.
 
.INPUTS
[Manifest]
[Version]
 
.OUTPUTS
[String]
 
.EXAMPLE
$manifest | Convert-NcrementVersionNumberToString;
This example returns the [Manifest] version number as a string. eg: Major.Minor.Path-Suffix
#>


function Convert-NcrementVersionNumberToString
{
    Param(
        [Parameter(Mandatory, ValueFromPipeline)]
        $InputObject,

        [Parameter(Position=0)]
        [string]$Branch = "",

        [Alias("suffix")]
        [switch]$AppendSuffix
    )

    [string]$version = "", $suffix;
    if ($InputObject.PSObject.Properties.Match("Version").Count -gt 0)
    {
        if (-not [string]::IsNullOrEmpty($Branch))
        {
            $match = $InputObject.BranchSuffixMap.PSObject.Properties.Match($Branch);
            if ($match.Count -gt 0)
            {
                $suffix = $match.Item(0).Value;
            }
            elseif ($InputObject.BranchSuffixMap.PSObject.Properties.Match("*").Count -gt 0)
            {
                $suffix = $InputObject.BranchSuffixMap."*";
            }
        }
        else { $suffix = $InputObject.Version.Suffix; }
        $version = "$($InputObject.Version.Major).$($InputObject.Version.Minor).$($InputObject.Version.Patch)";
    }
    else
    {
        $suffix = $InputObject.Suffix;
        $version = "$($InputObject.Major).$($InputObject.Minor).$($InputObject.Patch)";
    }

    if ($AppendSuffix.IsPresent -and (-not [string]::IsNullOrEmpty($suffix)))
    {
        $version += "-$suffix";
    }

    return $version;
}