Updating assembly version on local build


When you want to update the assembly version to the current changeset of your workspace when a solution is build (for example before deploying your solution), you can do this with PowerShell in combination with TF commandline.


The following script updates the assembly version, in the (shared) AssemblyInfo before the actual build command is executed. When the build is ready, the change to the AssemblyInfo is undone.

$tf = "${env:ProgramFiles(x86)}\Microsoft Visual Studio 14.0\Common7\IDE\TF.exe"
$msbuild = "${env:ProgramFiles(x86)}\MSBuild\14.0\Bin\MSBuild.exe"

# Get the data with the tf history command.
# ..\..\ is the path to the root of your current workspace.
$result = & $tf history ..\..\*.* /recursive /stopafter:1 /version:W
$changeSetNumber = $result[2].Split(" ")[0]

$maxNumber = 65535
$buildVersion = "0"
$revisionVersion = $changeSetNumber

# Calculate the version. If the current changeset is higher
# than 65535, the buildversion will be incremented.
[int]$buildVersion = [math]::floor($changeSetNumber / $maxNumber)
[int]$revisionVersion = $changeSetNumber % $maxNumber

$newVersion = "$buildVersion.$revisionVersion"

# Open the GlobalAssemblyInfo file (used by every project) and replace
# our custom token 9999.9999 with the new build.revision version.
(Get-Content "..\GlobalAssemblyInfo.cs").Replace('9999.9999', $newVersion)
| Set-Content "..\GlobalAssemblyInfo.cs"

# Do the building of the project here, for example with msbuild. It will
# execute the build using a defined *.proj file located in the same directory.
& $msbuild

# Undo the change of the GlobalAssemblyInfo so we don't checkin the new version
# and every other developer needs to rebuild the solution.
& $tf undo "..\GlobalAssemblyInfo.cs"

With this simple script you can create your deployment packages when you are not using Team Foundation Server for building and deploying your solution.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s