Public/Step-NcrementVersionNumberUsingDate.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 137 138 139 140 |
<#
.SYNOPSIS Increments the specified [Manifest] version number using the [DateTime]::UtcNow. .DESCRIPTION This function increments the [Manifest] version number using the [DateTime]::UtcNow object. If values passed to the 'Major', 'Minor' and 'Patch' parameters are not intergers, the their values will be used as format strings for the [DateTime]::ToString method. 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 The major version number. Accpets Defaults to 'yyMM'. .PARAMETER Minor The minor version number. Defaults to 'ddHH'. .PARAMETER Patch The patch version number. Defaults to 'Path + 1'. .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 [Manifest] .EXAMPLE $version = Get-NcrementManifest | Step-NcrementVersionNumberUsingDate -Major "yyMM" -Minor "ddmm"; In this example, because DateTime format strings were passed, each value will be used as the argument for the [DateTime]::UtcNow.ToString() method to replace their respective values. .EXAMPLE $version = Get-NcrementManifest | Step-NcrementVersionNumberUsingDate -Major "1709" -Minor "1123" -Patch "4586"; In this example, because integers were passed the function will return "1709.1123.4586". .LINK Get-NcrementManifest .LINK New-NcrementManifest .LINK Save-NcrementManifest #> function Step-NcrementVersionNumberUsingDate { Param( [Parameter(Mandatory, ValueFromPipeline, Position=4)] $Manifest, [Alias("break")] [Parameter(Position=0)] [string]$Major = "yyMM", [Alias("feature")] [Parameter(Position=1)] [string]$Minor = "ddHH", [Alias("fix", "bug")] [Parameter(Position=2)] [string]$Patch = "", [Alias('b')] [Parameter(Position=3)] [string]$Branch, [switch]$DoNotSave ) # Incrementing the version number. $Manifest.Version.Major = ConvertTo-Version $Major $Manifest.Version.Major; $Manifest.Version.Minor = ConvertTo-Version $Minor $Manifest.Version.Minor; $Manifest.Version.Patch = ConvertTo-Version $Patch ($Manifest.Version.Patch + 1); # Resolving the current branch name if it was not given. try { [string]$repo = ""; if ([string]::IsNullOrEmpty($Manifest.Path)) { $repo = $PWD; } else { $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 ($Manifest.Version.PSObject.Properties.Match("Suffix").Count -eq 0) { $Manifest.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) { $Manifest.Version.Suffix = $match.Item(0).Value; } elseif ($Manifest.BranchSuffixMap.PSObject.Properties.Match("*").Count -gt 0) { $Manifest.Version.Suffix = $Manifest.BranchSuffixMap."*"; } } else { Write-Warning "The 'BranchSuffixMap' property is undefined."; } # Saving the [Manifest]. if (-not ($DoNotSave)) { $Manifest | Save-NcrementManifest; } return $Manifest; } function ConvertTo-Version([string]$value, [int]$fallback) { [int]$num = 0; if ([string]::IsNullOrEmpty($value)) { return $fallback; } elseif ([int]::TryParse($value, [ref]$num)) { return $num; } else { return [System.DateTime]::UtcNow.ToString($value); } } |