Skip to content

Instantly share code, notes, and snippets.

@kumichou
Last active July 9, 2020 01:19
Show Gist options
  • Save kumichou/acefc48476957aad6b0c9abf203c304c to your computer and use it in GitHub Desktop.
Save kumichou/acefc48476957aad6b0c9abf203c304c to your computer and use it in GitHub Desktop.
Powershell script that will bump version numbers in C# AssemblyInfo files, commit them, and tag the commit.
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True,Position=1)]
[string]$bumpKind
)
function getVersion() {
$tag = Invoke-Expression "git describe --tags --always 2>&1"
$tag = $tag.Split('-')[0]
$tag = $tag -replace 'v', ''
if ($tag -notmatch "\d+\.\d+\.\d+\.\d+") {
$tag = '1.0.0.0'
}
Write-Verbose "Version found: $tag"
return $tag
}
function bumpVersion($kind, $version) {
$major, $minor, $patch, $build = $version.split('.')
switch ($kind) {
"major" {
$major = [int]$major + 1
}
"minor" {
$minor = [int]$minor + 1
}
"patch" {
$patch = [int]$patch + 1
}
"build" {
$build = [int]$build + 1
}
}
return [string]::Format("{0}.{1}.{2}.{3}", $major, $minor, $patch, $build)
}
function commitVersion($kind, $version) {
Invoke-Expression "git add **/*/AssemblyInfo.cs 2>&1 | Write-Verbose"
Invoke-Expression "git commit -m 'Bumped $kind number. Version now $version' 2>&1 | Write-Verbose"
Invoke-Expression "git tag v$version 2>&1 | Write-Verbose"
}
# Some functionality from https://gist.github.com/jokecamp/a2a314d62490fca1517a9a031c5606e9
function SetVersion ($file, $version) {
Write-Verbose "Changing version in $file to $version"
$fileObject = get-item $file
$sr = new-object System.IO.StreamReader( $file, [System.Text.Encoding]::GetEncoding("utf-8") )
$content = $sr.ReadToEnd()
$sr.Close()
$content = [Regex]::Replace($content, "(\d+)\.(\d+)\.(\d+)[\.(\d+)]*", $version);
$sw = new-object System.IO.StreamWriter( $file, $false, [System.Text.Encoding]::GetEncoding("utf-8") )
$sw.Write( $content )
$sw.Close()
}
function setVersionInDir($dir, $version) {
if ($version -eq "") {
Write-Verbose "version not found"
exit 1
}
# Set the Assembly version
$info_files = Get-ChildItem $dir -Recurse -Include "AssemblyInfo.cs"
foreach($file in $info_files)
{
Setversion $file $version
}
}
$validCommands = @("major", "minor", "patch", "build")
if ($bumpKind -eq '')
{
Write-Output "Missing which number to bump up!"
exit 1
}
if (-not $validCommands.Contains($bumpKind))
{
Write-Output "Invalid command!"
exit 1
}
$oldVersion = getVersion
$newVersion = bumpVersion $bumpKind $oldVersion
$dir = "./"
setVersionInDir $dir $newVersion
commitVersion $bumpKind $newVersion
# Dump the updated version number to Stdout so we can pick it up in CI when needed!
Write-Output $newVersion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment