script-modules/GitVersioning.psm1
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 |
function Add-MajorVersionTag { Param ( [Parameter(Mandatory = $true, Position = 0)][string]$Message, [switch]$CurrentBranchOnly ) $elements = GetLatestVersionElements -CurrentBranchOnly:$CurrentBranchOnly $major = [int]::Parse($($elements[0])) $major++ $NewTag = "v$major.0.0" git tag -a $NewTag -m $Message WriteSuccessMessage $($elements[3]) $NewTag $Message } function Add-MinorVersionTag { Param ( [Parameter(Mandatory = $true, Position = 0)][string]$Message, [switch]$CurrentBranchOnly ) $elements = GetLatestVersionElements -CurrentBranchOnly:$CurrentBranchOnly $minor = [int]::Parse($($elements[1])) $minor++ $NewTag = "v$($elements[0]).$minor.0" git tag -a $NewTag -m $Message WriteSuccessMessage $($elements[3]) $NewTag $Message } function Add-PatchVersionTag { Param ( [Parameter(Mandatory = $true, Position = 0)][string]$Message, [switch]$CurrentBranchOnly ) $elements = GetLatestVersionElements -CurrentBranchOnly:$CurrentBranchOnly $patch = [int]::Parse($($elements[2])) $patch++ $NewTag = "v$($elements[0]).$($elements[1]).$patch" git tag -a $NewTag -m $Message WriteSuccessMessage $($elements[3]) $NewTag $Message } function New-VersionTag { Param ( [Parameter(Mandatory = $true, Position = 0)][string]$Tag, [Parameter(Mandatory = $true, Position = 1)][string]$Message ) if ($Tag -notmatch "v[0-9]+\.[0-9]+\.[0-9]+") { throw "$Tag is not a valid version number. Use the format 'v1.2.3'." } git tag -a $Tag -m $Message Write-Host "New tag created`: $Tag $Message" -ForegroundColor Cyan } function WriteSuccessMessage { param( [Parameter(Mandatory = $true, Position = 0)][string]$OldTag, [Parameter(Mandatory = $true, Position = 1)][string]$NewTag, [Parameter(Mandatory = $true, Position = 2)][string]$Message ) Write-Host "Version Incremented`: $OldTag --> $NewTag $Message" -ForegroundColor Cyan } function GetLatestVersionElements { param ( [switch]$CurrentBranchOnly ) if ($CurrentBranchOnly) { $lastTag = git describe $index = $lastTag.indexOf('-') if ($index -lt 0) { $lastTag.Substring(1).split('.') # return array of version numbers $lastTag.Substring(1) # return the unsplit original } else { $lastTag.Substring(1, $index - 1).split('.') # return array of version numbers $lastTag.Substring(1, $index - 1) # return the unsplit original } } else { $lastTag = git for-each-ref refs/tags/v* --format="%(refname:short)" --sort=-v:refname --count=1 if ($null -eq $lastTag) { throw "Couldn't find any previous version to increment!" } $lastTag.Substring(1).split('.') # return array of version numbers $lastTag # return the unsplit original } } Export-ModuleMember *-* |