Nikhil
Nikhil

Reputation: 21

Increment the build revision number in visual studio

I have created the template for sharedAssemblyInfo.tt its updating version number only once for the first build or rebuild. If I change some code and try to build the whole project it’s not reflecting the version number.

Upvotes: 2

Views: 2481

Answers (2)

Dominic Jonas
Dominic Jonas

Reputation: 5025

If you use gitlab you could use GitVersionTask (https://www.nuget.org/packages/GitVersionTask).

On every commit, it automatically increases the Version of your build and even rename the output file. You just have to install the nuget to your project and you're done.


Example output for branch test-complex-udt on 2 commits:

HtOPC.Local.1.5.2-test-complex-udt0002

Upvotes: 0

FilipA
FilipA

Reputation: 612

If you want to increment the build number every time you build the project regardless of the selected configuration you can follow the steps below and you can also modify it manually in Application ➤ Assembly Information

Code below will read the existing AssemblyInfo.cs file, and use regex to find the AssemblyVersion information and then increment the revision and build numbers based on input from TextTransform.exe.

  1. Delete your existing AssemblyInfo.cs file.
  2. Create a AssemblyInfo.tt file in its place. Visual Studio should create AssemblyInfo.cs and group it with the T4 file after you save the T4 file.

Code:

<#@ template debug="true" hostspecific="true" language="C#" #>
<#@ output extension=".cs" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Text.RegularExpressions" #>
<#
    string output = File.ReadAllText(this.Host.ResolvePath("AssemblyInfo.cs"));
    Regex pattern = new Regex("AssemblyVersion\\(\"(?<major>\\d+)\\.(?<minor>\\d+)\\.(?<revision>\\d+)\\.(?<build>\\d+)\"\\)");
    MatchCollection matches = pattern.Matches(output);
    if( matches.Count == 1 )
    {
        major = Convert.ToInt32(matches[0].Groups["major"].Value);
        minor = Convert.ToInt32(matches[0].Groups["minor"].Value);
        build = Convert.ToInt32(matches[0].Groups["build"].Value) + 1;
        revision = Convert.ToInt32(matches[0].Groups["revision"].Value);
        if( this.Host.ResolveParameterValue("-","-","BuildConfiguration") == "Release" )
            revision++;
    }
#>

using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Resources;

// General Information
[assembly: AssemblyTitle("Insert title here")]
[assembly: AssemblyDescription("Insert description here")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Insert company here")]
[assembly: AssemblyProduct("Insert product here")]
[assembly: AssemblyCopyright("Insert copyright here")]
[assembly: AssemblyTrademark("Insert trademark here")]
[assembly: AssemblyCulture("")]

// Version informationr(
[assembly: AssemblyVersion("<#= this.major #>.<#= this.minor #>.<#= this.revision #>.<#= this.build #>")]
[assembly: AssemblyFileVersion("<#= this.major #>.<#= this.minor #>.<#= this.revision #>.<#= this.build #>")]
[assembly: NeutralResourcesLanguageAttribute( "en-US" )]

<#+
    int major = 1;
    int minor = 0;
    int revision = 0;
    int build = 0;
#>
  1. Add this to your pre-build event:
"%CommonProgramFiles(x86)%\microsoft shared\TextTemplating\$(VisualStudioVersion)\TextTransform.exe" -a !!BuildConfiguration!$(Configuration) "$(ProjectDir)Properties\AssemblyInfo.tt"

Let me know if something won't work with this solution as I use it often in my projects.

#Edit:

For many projects you can try with VS extension which maybe would be helpful in this case. There is an extension that my colleagues used. It can be found in Visual Studio Marketplace, in the Releases area, and inside Visual Studio under "Extensions and Updates" called "Intentional Solution Version Editor".

Upvotes: 4

Related Questions