Click here to Skip to main content
Click here to Skip to main content

Walkthrough: How to increment AssemblyFileVersion automatically at each build using T4

, 25 Sep 2013 Ms-PL
Rate this:
Please Sign up or sign in to vote.
Sometimes it could be useful to automatically increment the file version of an assembly at each build; this can be done even if you don't know MSBuild, using T4.

Introduction 

Sometimes it could be useful to automatically increment the file version of an assembly at each build; this can be done even if you don't know MSBuild, using T4.

Background

A T4 text template is a mixture of text blocks and control logic that can generate a text file. The control logic is written as fragments of program code in Visual C# or Visual Basic. The generated file can be text of any kind, such as a Web page, or a resource file, or program source code in any language. The T4 text templates can be translated by Visual Studio or by the TextTransform utility, TextTransform.exe. TextTransform.exe is a command-line tool that you can use to transform a text template. When you call TextTransform.exe, you specify the name of a text template file as an argument. TextTransform.exe calls the text transformation engine and processes the text template. So, using a simple T4 template, and running the transformation on the pre-build event, it is possible to autoincrement easily the Assembly File Version of one (or more) assemblies. This can be achieved by incrementing, for example, the revision number (the fourth from left, the 'X' in 1.0.0.X) each time the project is built.

Using the code

In order to achieve the automatic increment of the revision number:

  1. Right click on you csproj file, click on "Properties" on the context menu, click on the "Build Events" tab, then, add the following pre build action to the project: 
  2. First line:  

    set textTemplatingPath="%CommonProgramFiles(x86)%\Microsoft Shared\TextTemplating\$(VisualStudioVersion)\texttransform.exe"

    Second line:  

    if %textTemplatingPath%=="\Microsoft Shared\TextTemplating\$(VisualStudioVersion)\texttransform.exe" set textTemplatingPath="%CommonProgramFiles%\Microsoft Shared\TextTemplating\$(VisualStudioVersion)\texttransform.exe"

    Third line: 

    %textTemplatingPath% "$(ProjectDir)AssemblyFileVersion.tt" 

    The first two lines of code sets a custom environment variable, "%textTemplatingPath%", with the value of the "Common Files" folder that we need.  

    It is different on 32 bit machines and 64 bit machines. Using the MSBuild variable "$(VisualStudioVersion)" we make sure that the prebuild action we're writing will work on any VS version. The last line of code uses one environment variable, "%textTemplatingPath%", and another MSBuild variable, "$(ProjectDir)". Writing the path like this ensures that our solution is going to work with any version of Visual Studio
    on a 32 or a 64 bit machine.

    The first two lines of code sets a custom environment variable, "%textTemplatingPath%", with the value of the "Common Files" folder that we need. It is different on 32 bit machines and 64 bit machines. Using the MSBuild variable "$(VisualStudioVersion)" we make sure that the prebuild action we're writing will work on any VS version. The last line of code uses one environment variable, "%textTemplatingPath%", and another MSBuild variable, "$(ProjectDir)". Writing the path like this ensures that our solution is going to work with any version of Visual Studio on a 32 or a 64 bit machine.

  3. Remove the following line from the file AssemblyInfo.cs (usually located in the "Property" folder inside your C# project):
  4. [assembly: AssemblyFileVersion("1.0.0.0")]

    (Scroll down to the bottom in order to find it.)

  5. Add a new Text Template file "AssemblyFileVersion.tt" to the root folder of the project, copy inside of it the following t4 code, then you're done.
  6. <#@ template language="C#" hostSpecific="True"#>
    <#@ output extension="cs" #>
    <#@ import namespace="System.IO" #>
    <#
        int revisionNumber;
        try
        {
            //If we cannot find the file, the revision number is set to zero,
            //so even if the file doesn't exists the generation will run anyway.
            //NOTE: we suppose we're not messing with the generated file
            using(var f = File.OpenText(Host.ResolvePath("AssemblyFileVersion.cs")))
            {
                //We're reading the previous revision number; in order to make the
                //code as simple as we can, we're just going to rewrite it on the first row, commented.
                //This is not elegant, but it's simple enough and quite effective.
                string s = f.ReadLine().Replace("//","");
                revisionNumber = int.Parse(s) + 1; 
            }
        }catch
        {     
            revisionNumber = 0; 
        }
    #>
    //<#=revisionNumber#>
    // 
    // This code was generated by a tool. Any changes made manually will be lost
    // the next time this code is regenerated.
    // 
    
    using System.Reflection;
    
    [assembly: AssemblyFileVersion("1.0.0.<#= revisionNumber #>")]

  7. Now build the project. The generated "AssemblyFileVersion.cs" will be like the following:
  8. //0
    // 
    // This code was generated by a tool. Please do not change this document in any of its parts.
    // 
    
    using System.Reflection;
    
    [assembly: AssemblyFileVersion("1.0.0.0")]

Points of Interest

There are many other ways to achieve this result (Build server, MSBuild, ...), but I particularly like this one because this could be an easy way to get started with T4, even if you never used it. Of course the proposed solution can be easily improved. The good point of this solution is that the automatic increment mechanism can be easily spotted, understood and modified, by any developer of your team.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)

Share

About the Author

Bruno Tagliapietra
Software Developer Teorema http://www.teorema.net
Italy Italy
Consultant Developer on C#, .NET, SQL Server.
 
MCTS certified on WCF, 70-513.
 
Experienced in Silverlight 4, ASP.NET 2, Sharepoint 2007 - 2010

Comments and Discussions

 
QuestionMy vote of 5 PinmemberSB525-Sep-13 9:54 
GeneralMy vote of 5 Pinmemberfredatcodeproject25-Sep-13 6:23 
GeneralMy vote of 5 Pinmemberjohannesnestler25-Sep-13 4:34 
GeneralNotes Of Reading PinmemberChampion Chen24-Sep-13 21:01 
GeneralRe: Notes Of Reading PinmemberBruno Tagliapietra24-Sep-13 23:22 
GeneralRe: Notes Of Reading PinmemberChampion Chen25-Sep-13 21:34 
QuestionRe: Notes Of Reading PinmemberBruno Tagliapietra25-Sep-13 23:01 
AnswerRe: Notes Of Reading PinmemberChampion Chen25-Sep-13 23:23 
QuestionRe: Notes Of Reading PinmemberBruno Tagliapietra25-Sep-13 23:28 
AnswerRe: Notes Of Reading PinmemberChampion Chen26-Sep-13 0:03 
GeneralRe: Notes Of Reading PinmemberBruno Tagliapietra27-Sep-13 2:43 
GeneralMy vote of 5 [modified] PinmemberChampion·Chen24-Sep-13 17:11 
QuestionRe: My vote of 5 PinmemberBruno Tagliapietra24-Sep-13 22:38 
AnswerRe: My vote of 5 [modified] PinmemberChampion Chen24-Sep-13 22:48 
AnswerRe: My vote of 5 PinmemberBruno Tagliapietra24-Sep-13 23:03 
GeneralMy vote of 5 Pinmembervalery possoz23-Sep-13 10:18 
GeneralNice tip... PinmemberKishor Deshpande23-Sep-13 1:09 
GeneralNice article Pinmemberciberman7722-Sep-13 23:12 
SuggestionRe: Nice article PinmemberBruno Tagliapietra22-Sep-13 23:38 
GeneralMy vote of 5 PinprofessionalPaulo Zemek20-Sep-13 8:56 
GeneralRe: My vote of 5 Pinmemberorsobrown22-Sep-13 3:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141216.1 | Last Updated 25 Sep 2013
Article Copyright 2013 by Bruno Tagliapietra
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid