Created
July 11, 2016 15:01
-
-
Save worldspawn/b512fe3edb9b749d838a21c32b615bb3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function New-Version() { | |
param ($currentVersion) | |
$matches = $null | |
$currentVersion -match "^v?(?:(?<major>\d+)\.)?(?:(?<minor>\d+)\.)?(?<patch>\*|\d+)?(?:\-(?<release>[A-Za-z0-9\.]+))?(?:\+(?<meta>[A-Za-z0-9\.]+))?$" | Out-Null | |
#$groups = [regex]::matches($currentVersion, "^(?:(?<major>\d+)\.)?(?:(?<minor>\d+)\.)?(?<patch>\*|\d+)?(?:\.(?<build>\d+))?(?:\-(?<release>[A-Za-z0-9\.]+))?(?:\+(?<meta>[A-Za-z0-9\.]+))?$") | |
$major = [convert]::ToInt32($matches.major) | |
$minor = [convert]::ToInt32($matches.minor) | |
$patch = [convert]::ToInt32($matches.patch) | |
if ($matches.release -eq $null) { | |
$release = @() | |
} | |
else { | |
$release = $matches.release.split('.') | |
} | |
if ($matches.meta -eq $null) { | |
$meta = @() | |
} | |
else { | |
$meta = $matches.meta.split('.') | |
} | |
$version = new-object PSObject | select-object Major, Minor, Patch, Build, Release, Meta | |
$version.Major = $major | |
$version.Minor = $minor | |
$version.Patch = $patch | |
if ($release -eq $null) { | |
$version.Release = @() | |
} | |
else { | |
$version.Release = $release | |
} | |
if ($meta -eq $null) { | |
$version.Meta = @() | |
} | |
else { | |
$version.Meta = $meta | |
} | |
return $version | |
} | |
function VersionAsString () { | |
param ($version) | |
$v = "$($version.Major).$($version.Minor).$($version.Patch)" | |
if ($version.Release.Count -gt 0) { | |
$v += "-" | |
$v += $version.Release -join '.' | |
} | |
if ($version.Meta.Count -gt 0) { | |
$v += "+" | |
$v += $version.Meta -join '.' | |
} | |
return $v | |
} | |
$build_count = 678 #fake number to be provided externally | |
# | |
$isDevelopMerged = $false | |
$isMasterMerged = $false | |
$isDevelop = $false | |
$isMaster = $false | |
$isFeatureBranch = $false | |
$isHotfix = $false | |
$currentbranch = git rev-parse --abbrev-ref HEAD | |
$lasttag = git @("describe", "$currentbranch", "--tags", "--abbrev=0") | |
$versionObj = New-Version ($lasttag) | |
#$lasttag = "1.0.65" | |
$commitcount = git @("rev-list", "$lasttag..HEAD", "--count") | |
$currenthash = git rev-parse HEAD | |
$mergedwith = git branch -r --merged | ForEach { | |
if ($_ -match "origin/master") { | |
$isMasterMerged = $true | |
} | |
if ($_ -match "origin/develop") { | |
$isDevelopMerged = $true | |
} | |
} | |
if ($currentbranch -eq "develop") { | |
$isDevelop = $true | |
} | |
if ($currentbranch -eq "master") { | |
$isMaster = $true | |
} | |
if ($isMaster -eq $false) { | |
$changes = git @("log", "--oneline", "$lasttag..HEAD", "--grep=^change") | |
$changehash = git @("rev-list", "-n", "1", "$lasttag") | |
if ($changes.length -gt 0) { | |
$versionObj.Major += 1; | |
$versionObj.Minor = 0; | |
$versionObj.Patch = 0; | |
if ($changes -is [system.array]) { | |
$changehash = $changes[0].substring(0, 7) | |
} | |
else{ | |
$changehash = $changes.substring(0, 7) | |
} | |
} | |
$changes = git @("log", "--oneline", "$changehash..HEAD", "--grep=^feat") | |
if ($changes.length -gt 0) { | |
$changecount = $changes.length | |
if ($changes -is [system.array]) { | |
$changehash = $changes[0].substring(0, 7) | |
} | |
else{ | |
$changehash = $changes.substring(0, 7) | |
$changecount = 1 | |
} | |
$versionObj.Minor = $changecount; | |
$versionObj.Patch = 0; | |
} | |
$changes = git @("log", "--oneline", "$changehash..HEAD", "--grep=patch\|bug") | |
if ($changes.length -gt 0) { | |
if ($changes -is [system.array]) { | |
$versionObj.Patch = $changes.length | |
} | |
else{ | |
$changehash = $changes.substring(0, 7) | |
$versionObj.Patch = 1 | |
} | |
} | |
} | |
if ($isDevelopMerged) { | |
if ($isDevelop -eq $false) { | |
$isFeatureBranch = $true | |
$versionObj.Release += 'alpha' | |
$versionObj.Release += $currentbranch | |
$versionObj.Release += $build_count | |
} | |
else { | |
$isReleaseCandidate = $true | |
} | |
} | |
else { | |
if ($isMasterMerged -and $isMaster -eq $false) { | |
$isHotfix = $true | |
$versionObj = New-Version ($lasttag) | |
$versionObj.Release = 'hf' | |
$versionObj.Release += $build_count | |
} | |
} | |
$versionObj.Meta = @($currenthash.substring(0, 7)) | |
$version = VersionAsString($versionObj) | |
"##teamcity[setParameter name='env.currentBuildNumber' value='$version']" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment