Public/Step-NcrementVersionNumber.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
<#
.SYNOPSIS
Increments the specified [Manifest] version number.
 
.DESCRIPTION
This function increments the [Manifest] version number. Also when invoked, the version will be incremented then the modified [Manifest] object will be saved to disk as well.
 
.PARAMETER Manifest
The [Manifest] object.
 
.PARAMETER Major
Determines whether the 'Major' version number should be incremented.
 
.PARAMETER Minor
Determines whether the 'Minor' version number should be incremented.
 
.PARAMETER Patch
Determines whether the 'Patch' version number should be incremented.
 
.PARAMETER Branch
The source control branch. The value provided will be used to determine the version suffix. If not set 'git branch' will be used as default.
 
.PARAMETER DoNotSave
Determines whether to not save the modified [Manifest] object to disk.
 
.INPUTS
[Manifest]
 
.OUTPUTS
[Version]
 
.EXAMPLE
$version = Get-NcrementManifest | Step-NcrementVersionNumber -Minor;
 
.EXAMPLE
$version = Get-NcrementManifest | Step-NcrementVersionNumber "master" -Patch;
 
.LINK
Get-NcrementManifest
 
.LINK
New-NcrementManifest
 
.LINK
Save-NcrementManifest
#>


function Step-NcrementVersionNumber
{
    Param(
        [Parameter(Mandatory, ValueFromPipeline, Position = 1)]
        $Manifest,

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

        [Alias("break")]
        [switch]$Major,

        [Alias("feature")]
        [switch]$Minor,

        [Alias("fix", "bug")]
        [switch]$Patch,

        [Alias("no-save")]
        [switch]$DoNotSave
    )

    # Incrementing the [Manifest] version number.
    $version = $Manifest.Version;
    if ($Major)
    {
        $version.Major += 1;
        $version.Minor = 0;
        $version.Patch = 0;
    }
    elseif ($Minor)
    {
        $version.Minor += 1;
        $version.Patch = 0;
    }
    elseif ($Patch)
    {
        $version.Patch += 1;
    }

    # Resolving the current branch name if it was not given.
    try
    {
        [string]$repo = "";
        if ([string]::IsNullOrEmpty($Manifest.Path)) { $repo = $PWD; }
        elseif (Test-Path $Manifest.Path -PathType Container) { $repo = $Manifest.Path; }
        elseif (Test-Path $Manifest.Path -PathType Leaf) { $repo = Split-Path $Manifest.Path -Parent; }

        Push-Location $repo;
        if ([string]::IsNullOrEmpty($Branch) -and (Test-GitRepository))
        {
            $context = (&git branch | Out-String);
            $regex = New-Object Regex -ArgumentList @('(?i)\*\s+(?<name>\w+)');
            $match = $regex.Match($context);

            if ($match.Success)
            {
                $Branch = $match.Groups["name"].Value;
            }
        }
    }
    finally { Pop-Location; }

    # Adding the 'Suffix' property to [Version] if it is missing.
    if ($version.PSObject.Properties.Match("Suffix").Count -eq 0)
    {
        $version | Add-Member -MemberType NoteProperty -Name "Suffix" -Value "";
    }

    # Getting the branch suffix.
    if ($Manifest.PSObject.Properties.Match("BranchSuffixMap").Count -gt 0)
    {
        $match = $Manifest.BranchSuffixMap.PSObject.Properties.Match($Branch);
        if ($match.Count -eq 1)
        {
            $version.Suffix = $match.Item(0).Value;
        }
        elseif ($Manifest.BranchSuffixMap.PSObject.Properties.Match("*").Count -gt 0)
        {
            $version.Suffix = $Manifest.BranchSuffixMap."*";
        }
    }
    else { Write-Warning "The 'BranchSuffixMap' property is undefined."; }

    # Saving the changes made to [Manifest].
    if (-not ($DoNotSave)) { $Manifest | Save-NcrementManifest; }

    return $Manifest;
}