Darren Wainwright
Darren Wainwright

Reputation: 30757

Update AssemblyInfo during build of a NuGet package in TFS

We are evaluating TFS On Premise, including the Package Management feature.

I have several build configurations that successfully create NuGet packages and publish them to the feed within TFS.

Versions of the package are controlled via the AssemblyInfo.cs file within the solution. For example: [assembly: AssemblyInformationalVersion("1.0.0.0")] results in a NuGet version 1.0.0.0; using AssemblyInformationalVersion because you can apply pre-release tags to your version, such as [assembly: AssemblyInformationalVersion("1.0.0.0-dev")] resulting in verison 1.0.0.0-dev

Is there a way of setting up the build configuration to modify the AssemblyInformationalVersion based on the branch being used for the build?

So that we can have [assembly: AssemblyInformationalVersion("1.0.0.0")] in code, but when building off branch "myNewFeature" it result in "1.0.0.0-myNewFeature" and building off master can result in ""1.0.0.0"? Rather than having to make this change to the AssemblyInfo.cs file each time we merge to a different branch.

Upvotes: 1

Views: 665

Answers (2)

PatrickLu-MSFT
PatrickLu-MSFT

Reputation: 51183

To update the AssemblyInfo you could use a powershell script or use build number to version the nuget package.

For build definitions, $(SourceBranchName) can be used in the build number format. The list of available variables is found here-- Predefined variables

So that when you generate Nuget packages at the end of the build, they can be versioned using the version of the branch that is being built. For more details please refer this related question: Use Branch Name in TFS 2015 Automated Build

And for version nuget package you could also take a look at below blog which provides an overview of the automatic package versioning available here:

Upvotes: 1

Darren Wainwright
Darren Wainwright

Reputation: 30757

I have managed to cobble together a Powershell script that is ran as the first build step in the build configuration.

I used some code from these 2 sources GithubGist pietergheysens/Update-AssemblyInfoVersionFiles.ps1 and also this StackOverflow answer Get and Replace AssemblyVersion from AssemblyInfo.cs

I personally do not know Powershell syntax and the following script may not be the most suitable. Any advice will be appreciated.

The powershell script is as:

Param ( [Parameter(Mandatory=$true)] [string]$branchName, [string]$projectDirectory )

$SrcPath = $env:BUILD_SOURCESDIRECTORY
if ($projectDirectory -ne ""){
    $SrcPath = "$SrcPath$projectDirectory"
}      

Write-Verbose "Executing Update-AssemblyInfoVersionFiles in path $SrcPath for product versionon branch $branchName"  -Verbose

if ($branchName.ToLower() -eq "master")
{
    $branchName = ""
}
else
{
    $branchName = "-$branchName"
}

$AllVersionFiles = Get-ChildItem $SrcPath AssemblyInfo.cs -recurse   
$pattern = '\[assembly: AssemblyInformationalVersion\("(.*)"\)\]'
foreach ($file in $AllVersionFiles) 
{ 
    #version replacements
    (Get-Content $file.FullName) | ForEach-Object{
        if($_ -match $pattern){
            # We have found the matching line
            # Edit the version number and put back.
            $currentVersion = [version]$matches[1]
            Write-Verbose " $currentVersion" -Verbose
            $newVersion = "$currentVersion$branchName"
            '[assembly: AssemblyInformationalVersion("{0}")]' -f $newVersion
            Write-Verbose "New AssemblyInformationalVersion is $newVersion"  -Verbose
        } else {
            # Output line as is
            $_
        }
    } | Set-Content $file.FullName -Force
}
Write-Verbose "Transformed Assembly Informational Version is $assemblyInformationalVersion" -Verbose

My build step looks like this:

enter image description here

This is successfully changing the assembly version for me, based on the branch I use to build the package.

Upvotes: 1

Related Questions