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
function Step-NcrementVersionNumber 
{
    Param(
        [Parameter(Mandatory, ValueFromPipeline)]
        $InputObject,

        [switch]$Major,
        
        [switch]$Minor,

        [switch]$Patch
    )

    PROCESS
    {
        [string]$path = ConvertTo-Path $InputObject;

        $manifest = $null;
        try { if (Test-Path $path -PathType Leaf -ErrorAction SilentlyContinue) { $manifest = Get-Content $path | ConvertFrom-Json; } } catch { }
        if ($InputObject | Get-Member "version") { $manifest = $InputObject; }

        if ($manifest -ne $null)
        {
            if ($Major)
            {
                $manifest.version.major++;
                $manifest.version.minor = 0;
                $manifest.version.patch = 0;
            }
            elseif ($Minor)
            {
                $manifest.version.minor++;
                $manifest.version.patch = 0;
            }
            else
            {
                $manifest.version.patch++;
            }

            return $manifest;
        }
    }
}