Skip to content

Instantly share code, notes, and snippets.

@RedTahr
Forked from bradjolicoeur/gist:e77c508089aea6614af3
Last active December 17, 2018 17:31
Show Gist options
  • Save RedTahr/5e68b5f4c8973ceb64096573744d06af to your computer and use it in GitHub Desktop.
Save RedTahr/5e68b5f4c8973ceb64096573744d06af to your computer and use it in GitHub Desktop.
Powershell Script to Increment Build Number in AssemblyInfo.cs
#
# This script will increment the build number in a file
# refactored from the AssemblyInfo.cs version incrementing gist for use with AndroidManifest.xml
#
#$assemblyInfoPath = "C:\Data\Temp\AssemblyInfo.cs"
$mani = "C:\Data\Temp\AndroidManifest.xml"
#$contents = Get-Content $assemblyInfoPath # looses file formatting
$contents = [System.IO.File]::ReadAllText($mani)
#$versionString = [RegEx]::Match($contents,"(AssemblyFileVersion\("")(?:\d+\.\d+\.\d+\.\d+)(""\))")
# for any four digit version number, such as in an AndroidManifest.xml file
#$versionString = [RegEx]::Match($contents,"((?:\d+\.\d+\.\d+\.\d+))")
# or to be more accurate, should the manifest have multiple strings in that format
#$versionString = [RegEx]::Match($contents,"(android:versionName=""(?:\d+\.\d+\.\d+\.\d+)"")")
$versionString = [RegEx]::Match($contents,"(android:versionName=""(.+?)"")") # lazily get the version, matches any version format
$versionName = [RegEx]::Match($versionString,"((?:\d+\.\d+\.\d+\.\d+))") # bit specific, needs to be in the right format
$version = [version] $versionName.Value
#Write-Host ("AssemblyFileVersion: " +$versionString)
#$version = [version]$versionString.Groups[1].Value
#Parse out the current build number from the AssemblyFileVersion
#$currentBuild = [RegEx]::Match($versionString,"(\.)(\d+)").Groups[2]
#Write-Host ("Current Build: " + $currentBuild.Value)
Write-Host ("Current Major: " + $version.Major)
Write-Host ("Current Minor: " + $version.Minor)
Write-Host ("Current Build: " + $version.Build)
Write-Host ("Current Revision: " + $version.Revision)
#Increment the revision number
$newRevision= [int]$version.Revision + 1
Write-Host ("New Revision: " + $newRevision)
#work in progress, I don't know regex so haven't got it writing the new file version yet.
# android:versionName="" $version.Major.ToString \. $version.Minor.ToString() \. $version.Build.ToString() \. $newRevision.ToString() ""
# android:versionCode="" $version.Major.ToString $version.Minor.ToString() $version.Build.ToString() $newRevision.ToString() ""
#update AssemblyFileVersion and AssemblyVersion, then write to file
Write-Host ("Setting version in assembly info file ")
$contents = [RegEx]::Replace($contents, "(AssemblyVersion\(""\d+\.\d+\.\d+\.)(?:\d+)(""\))", ("`${1}" + $newBuild.ToString() + "`${2}"))
$contents = [RegEx]::Replace($contents, "(AssemblyFileVersion\(""\d+\.\d+\.\d+\.)(?:\d+)(""\))", ("`${1}" + $newBuild.ToString() + "`${2}"))
[System.IO.File]::WriteAllText($assemblyInfoPath, $contents)
@RedTahr
Copy link
Author

RedTahr commented Feb 2, 2017

the original assemblyinfo.cs version increment:
$assemblyInfoPath = "D:\temp\AssemblyInfo.cs"

$contents = [System.IO.File]::ReadAllText($assemblyInfoPath)

$versionString = [RegEx]::Match($contents,"(AssemblyFileVersion("")(?:\d+.\d+.\d+.\d+)(""))")
Write-Host ("AssemblyFileVersion: " +$versionString)

#Parse out the current build number from the AssemblyFileVersion
$currentBuild = [RegEx]::Match($versionString,"(.)(\d+)(""))").Groups[2]
Write-Host ("Current Build: " + $currentBuild.Value)

#Increment the build number
$newBuild= [int]$currentBuild.Value + 1
Write-Host ("New Build: " + $newBuild)

#update AssemblyFileVersion and AssemblyVersion, then write to file
Write-Host ("Setting version in assembly info file ")
$contents = [RegEx]::Replace($contents, "(AssemblyVersion(""\d+.\d+.\d+.)(?:\d+)(""))", ("${1}" + $newBuild.ToString() + "${2}"))
$contents = [RegEx]::Replace($contents, "(AssemblyFileVersion(""\d+.\d+.\d+.)(?:\d+)(""))", ("${1}" + $newBuild.ToString() + "${2}"))
[System.IO.File]::WriteAllText($assemblyInfoPath, $contents)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment