Created
November 23, 2012 15:13
-
-
Save mckn/4136080 to your computer and use it in GitHub Desktop.
Semver stuff in powershell
This file contains 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 Bump-Version | |
{ | |
param([string]$part = $(throw "Part is a required parameter.")) | |
$version = Get-AssemblyInfoVersion -Directory ..\Source -GlobalAssemblyInfo $true | |
$bumpedVersion = Clone-Object -Object $version | |
switch -wildcard ($part) | |
{ | |
"ma*" { $bumpedVersion.Major = Bump-NumericVersion -Current $version.Major } | |
"mi*" { $bumpedVersion.Minor = Bump-NumericVersion -Current $version.Minor } | |
"p*" { $bumpedVersion.Patch = Bump-NumericVersion -Current $version.Patch } | |
"b*" { $bumpedVersion.Build = Bump-SpecialVersion -Current $version.Build } | |
default { throw "Parameter Part should be: minor, major, patch or build!"} | |
} | |
if($bumpedVersion.Major -eq $version.Major -and | |
$bumpedVersion.Minor -eq $version.Minor -and | |
$bumpedVersion.Patch -eq $version.Patch -and | |
$bumpedVersion.Build -eq $version.build) | |
{ | |
throw "Version didn't change due to some error..." | |
} | |
Update-AssemblyInfoVersion -Directory ..\Source -GlobalAssemblyInfo $true -BumpedVersion $bumpedVersion | |
} | |
function Bump-NumericVersion | |
{ | |
param([int] $current = $(throw "Current is a required parameter.")) | |
return $current + 1; | |
} | |
function Bump-SpecialVersion | |
{ | |
param([string]$current = $(throw "Current is a required paramter.")) | |
throw "Not implemented...." | |
} | |
function Clone-Object | |
{ | |
param([PSObject] $object = $(throw "Object is a required parameter.")) | |
$clone = New-Object PSObject | |
$object.psobject.properties | % { $clone | Add-Member -MemberType $_.MemberType -Name $_.Name -Value $_.Value } | |
return $clone | |
} | |
function Get-AssemblyInfoVersion | |
{ | |
param([string] $directory = $(throw "Directory is a required parameter."), | |
[bool] $globalAssemblyInfo = $false) | |
$fileName = "AssemblyInfo.cs" | |
$versionPattern = 'AssemblyVersion\("([0-9])+\.([0-9])+\.([0-9])+\-?(.*)?"\)' | |
if($globalAssemblyInfo) | |
{ | |
$fileName = "GlobalAssemblyInfo.cs" | |
} | |
$assemblyInfo = Get-ChildItem $directory -Recurse | | |
Where-Object {$_.Name -eq $fileName} | | |
Select-Object -First 1 | |
if(!$assemblyInfo) | |
{ | |
throw "Could not find assembly info file" | |
} | |
$matchedLine = Get-Content $assemblyInfo.FullName | | |
Where-Object { $_ -match $versionPattern } | | |
Select-Object -First 1 | |
if(!$matchedLine) | |
{ | |
throw "Could not find line containing assembly version in assembly info file" | |
} | |
$major, $minor, $patch, $build = ([regex]$versionPattern).matches($matchedLine) | | |
foreach {$_.Groups } | | |
Select-Object -Skip 1 | |
return New-Object PSObject -Property @{ | |
Minor = $minor.Value | |
Major = $major.Value | |
Patch = $patch.Value | |
Build = $build.Value | |
} | |
} | |
function Update-AssemblyInfoVersion | |
{ | |
param([PSObject] $bumpedVersion = $(throw "BumpedVersion is a required parameter."), | |
[string] $directory = $(throw "Directory is a required parameter."), | |
[bool] $globalAssemblyInfo = $false) | |
$assemblyVersionPattern = 'AssemblyVersion\("([0-9])+\.([0-9])+\.([0-9])+\-?(.*)?"\)' | |
$assemblyFileVersionPattern = 'AssemblyFileVersion\("([0-9])+\.([0-9])+\.([0-9])+\-?(.*)?"\)' | |
$version = ("{0}.{1}.{2}" -f $bumpedVersion.Major, $bumpedVersion.Minor, $bumpedVersion.Patch) | |
if($bumpedVersion.Build) | |
{ | |
$version = "{0}-{1}" -f $version, $bumpedVersion.Build | |
} | |
$assemblyVersion = 'AssemblyVersion("' + $version + '")' | |
$fileVersion = 'AssemblyFileVersion("' + $version + '")' | |
$fileName = "AssemblyInfo.cs" | |
if($globalAssemblyInfo) | |
{ | |
$fileName = "GlobalAssemblyInfo.cs" | |
} | |
Get-ChildItem $directory -Recurse -Filter $fileName | ForEach-Object { | |
$currentFile = $_.FullName | |
$tempFile = ("{0}.tmp" -f $_.FullName) | |
Get-Content $currentFile | ForEach-Object { | |
% { $_ -Replace $assemblyVersionPattern, $assemblyVersion } | | |
% { $_ -Replace $assemblyFileVersionPattern, $fileVersion } | |
} | Set-Content $tempFile | |
Remove-Item $currentFile | |
Rename-Item $tempFile $currentFile | |
Write-Host "Updated version to: $version in $currentFile" | |
} | |
} | |
Bump-Version -part "patch" -useGlobalAssemblyInfo $true |
Perfect, thanks for sharing!
thanks this is great!
however is there a small bug - the regex seems to only work with single-digit versions like '1.2.3' but not '1.2.31'
(it takes the 1st digit of each part)
I'm using a slightly modified regex to deal with multiple digits:
([0-9]+).([0-9]+).([0-9]+)-?(.*)?
I'm on PowerShell 4
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great work!