Pre-build versioning with PowerShell script and TFS Build

Posted by

There are a lot of scenarios where the PowerShell script requires to be run as a Build task in your Build definition. In of the cases, we had a requirement to change version attribute in the AssemblyInfo.cs files in all Projects of our solution. The version number required to be in the format “1.0.0.”. This version number should be visible in the exe properties after installation.

To achieve this, I created a PowerShell script, which recursively found all the AssemblyInfo.cs files in all Projects, checking for the Attribute pattern and replace it.

Create a PowerShell script file in the ISE and copy the following code:

Define the Params:

Param
(
[Parameter(Mandatory=$true)]
[string]$productVersion
)

$YYYY = (Get-Date).year
$SrcPath = $env:BUILD_SOURCESDIRECTORY

$AllVersionFiles = Get-ChildItem $SrcPath AssemblyInfo.cs -recurse
$versions = $productVersion.Split('.')
$major = $versions[0]
$minor = $versions[1]
$patch = $versions[2]
$build = $versions[3]

$assemblyVersion = "$major.$minor.$patch.$build"
$assemblyFileVersion = "$major.$minor.$patch.$build"
$assemblyInformationalVersion = "$major.$minor.$patch.$build"

Replace the Assembly attributes:

foreach ($file in $AllVersionFiles)
{

(Get-Content $file.FullName) |
%{$_ -replace 'AssemblyDescription\(""\)', "AssemblyDescription(""assembly built by TFS Build $productVersion"")" } |
%{$_ -replace 'AssemblyVersion\("[0-9]+(\.([0-9]+|\*)){1,3}"\)', "AssemblyVersion(""$assemblyVersion"")" } |
%{$_ -replace 'AssemblyFileVersion\("[0-9]+(\.([0-9]+|\*)){1,3}"\)', "AssemblyFileVersion(""$assemblyFileVersion"")" } |
%{$_ -replace 'AssemblyInformationalVersion\("[0-9]+(\.([0-9]+|\*)){1,3}"\)', "AssemblyInformationalVersion(""$assemblyInformationalVersion"")" } |
Set-Content $file.FullName -Force

}

The script requires to following parameters which requires to be passed in the Build task:

PSVersion1

The code should be checked in to TFS and mapped to a local folder from the Repository tab so it can be downloaded on to the Agent machine locally.

PSVersion12

This PowerShell build step should be the first step in your Build definition before the Visual Studio Build task so that the version changes can take place as soon as the code is fetched on the TFS Agent work folder.

Tip: Make sure your AssemblyInfo.cs files has the above attributes which you’re replacing with the PS Script in the specified format else some dlls may miss out on the updated versions.
Advertisements

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.