Wednesday, July 3, 2013

TFS Build number used as AssemblyVersion in AssemblyInfo.cs

 

Finally, after years of struggling with AssemblyInfo.cs files to be updated to track assembly versioning, here IS an EASY WAY to do this thing, which should have been made easy in the first place in TFS. In the past, I used a more sophisticated way of doing it.

The credit to this method goes to Steve Fenton, who explained his method called "Automatically Updating Your AssemblyInfo The Wrong Way".

To me it is less elegent than doing a full workflow foundation solution, but it solves my problem quick ! So that a good solution. Secondly, maintenability is also great, as it is based on a plain text Batch file. No need to recompile deploy, …

The secret lies the following steps :

  • Using a Team Foundation Build Activities called “InvokeProcess” that is dropped inside the “Run on Agent” process, just after the “Initialize Workspace”.

    More details on a old Post of mine back in 2010, where and how precisely how to operate it.

    This Process simply calls a Batch file that creates on the fly an AssemblyInfo.cs.
  • This “AssemblyInfo.cs” file is inserted on ALL the *.csproj that are part of your solution “As a Link” image


image

  • the only mini-change that one could barely notice the link icon on the AssemblyInfo.cs file. Here is a representation in both Visual Studio 2013 Ultimate preview and Visual Studio 2012 Ultimate



ZOOM ! on AssemblyInfo.cs
image
image

 

The result is instantaneous … on every single step of my software factory, it is highly traceable with the unique ID of the TFS Team Build of 370, as illustrated below.

image

(1) Displays on an “About Page” the exact version of the application, based on the assembly version of the AssemblyInfo.cs,

(2) This version number is dynamically generated by TFS Team Build during the Continuous Deployment process (called _CD).
This guaranties the uniqueness of this number. In this example, it is Build# 370, which gives a version 1.0.0.370.
As a result, we could go up to 1.0.0. 65534,
where 65534 = UInt16.MaxValue – 1= 65535-1
Then you can open the TFS Drop Folder to verify that ALL your assemblies have the correct assembly version.

(3) From TFS Cloud, you’ll get a Zip file that has got the unique suffix “CD_370”, and in the App.publish, you could unzip the Windows Azure package *.cspkg

(4) Once unzipped into the folder “???CD_370”, you’ll see the DLLs

(5) Check the first DLL; we could see the magic happened: The assembly is tagged as 1.0.0.370

(6) Check another Assembly: still 1.0.0.370 !!

DONE ! and Enjoy !!

And the most time consuming part to was to find the proper solution, since many great alternatives are present in CodePlex. I’m sharing this trick hoping it’ll save you plenty of time… and another wish is that one day, an easy feature to activate will appear TFS / VSO !!

 

Note: If needed, here is an example of a batch file.

SET version=%~1
SET outfile=%~2

SET year=%DATE:~6,4%
ECHO off
if "%outfile%"=="" SET /p "outfile=[Full Path] "
if "%version%"=="" SET /p "version=[0.0.0.0] "
REM %tfstool% checkout %outfile%
ATTRIB -r %outfile%
ECHO using System.Reflection; > %outfile%
ECHO using System.Resources; >> %outfile%
ECHO using System.Runtime.CompilerServices; >> %outfile%
ECHO using System.Runtime.InteropServices; >> %outfile%
ECHO

ECHO //// This file is auto generated by AssemblyVersion.bat >> %outfile%
ECHO [assembly: AssemblyTitle("Viseo")] >> %outfile%
ECHO [assembly: AssemblyDescription("This AssemblyInfo.cs is automatically generated by TFS Build")] >> %outfile%

ECHO [assembly: AssemblyConfiguration("")] >> %outfile%
ECHO [assembly: AssemblyCompany("Viseo")] >> %outfile%
ECHO [assembly: AssemblyProduct("Viseo")] >> %outfile%
ECHO [assembly: AssemblyCopyright("Copyright © 2013-%year% Viseo. All rights reserved.")] >> %outfile%
ECHO [assembly: AssemblyTrademark("")] >> %outfile%
ECHO [assembly: AssemblyCulture("")] >> %outfile%
ECHO [assembly: ComVisible(false)] >> %outfile%

ECHO //// Version information for an assembly consists of the following four values:
ECHO ////
ECHO //// Major Version
ECHO //// Minor Version
ECHO //// Build Number
ECHO //// Revision
ECHO ////
ECHO //// You can specify all the values or you can default the Revision and Build Numbers
ECHO //// by using the '*' as shown below:
ECHO [assembly: AssemblyVersion("1.0.0.%version%")] >> %outfile%
ECHO [assembly: AssemblyFileVersion("1.0.0.%version%")] >> %outfile%
ECHO [assembly: NeutralResourcesLanguageAttribute("en")] >> %outfile%

REM %tfstool% checkin %outfile% /comment:"***NO_CI*** Assembly Info Increment" /noprompt

5 comments:

  1. Hi, thanks for your post. Could you provide a download link to the batch file that is used in the workflow InvokeProcess?

    ReplyDelete
  2. Hi Shakar,

    Please find a Copy/Past below (faster for me than creating a downloadable link!)
    As a bonus, the file below even complies to StyleCop !

    =======

    set version=%~1
    set outfile=%~2

    set year=%DATE:~6,4%
    echo off
    if "%outfile%"=="" set /p "outfile=[Full Path] "
    if "%version%"=="" set /p "version=[0.0.0.0] "
    REM %tfstool% checkout %outfile%
    attrib -r %outfile%
    echo using System.Reflection; > %outfile%
    echo using System.Resources; >> %outfile%
    echo using System.Runtime.CompilerServices; >> %outfile%
    echo using System.Runtime.InteropServices; >> %outfile%
    echo

    echo //// This file is auto generated by AssemblyVersion.bat >> %outfile%
    echo [assembly: AssemblyTitle("Objet Direct")] >> %outfile%
    echo [assembly: AssemblyDescription("This AssemblyInfo.cs is automatically generated by TFS Build")] >> %outfile%

    echo [assembly: AssemblyConfiguration("")] >> %outfile%
    echo [assembly: AssemblyCompany("Objet Direct")] >> %outfile%
    echo [assembly: AssemblyProduct("Objet Direct")] >> %outfile%
    echo [assembly: AssemblyCopyright("Copyright © 2013-%year% Objet Direct. All rights reserved.")] >> %outfile%
    echo [assembly: AssemblyTrademark("")] >> %outfile%
    echo [assembly: AssemblyCulture("")] >> %outfile%
    echo [assembly: ComVisible(false)] >> %outfile%


    echo //// Version information for an assembly consists of the following four values:
    echo ////
    echo //// Major Version
    echo //// Minor Version
    echo //// Build Number
    echo //// Revision
    echo ////
    echo //// You can specify all the values or you can default the Revision and Build Numbers
    echo //// by using the '*' as shown below:
    echo [assembly: AssemblyVersion("1.0.0.%version%")] >> %outfile%
    echo [assembly: AssemblyFileVersion("1.0.0.%version%")] >> %outfile%
    echo [assembly: NeutralResourcesLanguageAttribute("en")] >> %outfile%

    REM %tfstool% checkin %outfile% /comment:"***NO_CI*** Assembly Info Increment" /noprompt

    ReplyDelete
  3. This comment has been removed by a blog administrator.

    ReplyDelete
    Replies
    1. Vincent, Thanks also delete this comment from Jason/looks like SPAM.

      Delete
    2. Check my new Post, it should interrests you :http://blog.thavo.com/2015/02/more-azure-in-tfs-vnext-2015-and-no.html !

      Delete

Note: Only a member of this blog may post a comment.