visual-studio-2017t4

How to run a T4 transform on a release build only


I am using a T4 template to (among other things) increment build numbers in the AssemblyInfo.cs file.

As I only want the transformation to occur when I run a Release build, I have previously done this by having the build action on the T4 file as being "None" and having a kludge of a pre-build command on the project of:

if $(Configuration) == Release "%CommonProgramFiles(x86)%\microsoft shared\TextTemplating\12.0\TextTransform.exe" -a !!build!true  "$(ProjectDir)Transforms\AssemblyInfo.tt"

I am now moving to VS2017 and I want to know if there are any better ways of doing this. Answers to questions like Get Visual Studio to run a T4 Template on every build basically offer:

  1. A souped up version of what I am already doing (but with batch files)
  2. Point to lots of VS extensions that try to run the T4 stuff on every build.
  3. Propose manually modifying the project file (which I don't want to do as it makes the process invisible when using the IDE)

in 2017 is there a better approach in VS2017 to what I want to do? Something allows me to run my T4 on a release build only, that integrates into the solution from the IDE and doesn't rely on batch files?

Another question Determine solution configuration (debug/release) when running a T4 template shows how to get the build type from within the T4 template, but by then it's too late for me.


Solution

  • For Visual Studio 2022 (and likely earlier versions), you can use a conditional ItemGroup in your *.csproj file. For example, to include the ItemGroup except when compiling a Debug version, you can use <ItemGroup Condition="'$(Configuration)' != 'Debug'">.

    For my needs, I have the major and minor version number hardcoded in my VersionAutoIncrement.tt T4 template. This gets updated as needed. Additional version number components are computed from the "project start date" (also hardcoded in VersionAutoIncrement.tt) and the time that the project is compiled. This gives me a unique monotonically increasing version numbers for each non-`Debug

    So, to auto-increment my assembly version number for all except Debug builds, I have the following:

    In my ProjectNameGoesHere.csproj file:

        <ItemGroup Condition="'$(Configuration)' != 'Debug'">
            <None Update="VersionAutoIncrement.tt">
                <Generator>TextTemplatingFileGenerator</Generator>
                <LastGenOutput>VersionAutoIncrement.cs</LastGenOutput>
            </None>
            <Compile Update="VersionAutoIncrement.cs">
                <DesignTime>True</DesignTime>
                <AutoGen>True</AutoGen>
                <DependentUpon>VersionAutoIncrement.tt</DependentUpon>
            </Compile>
        </ItemGroup>
    

    VersionAutoIncrement.tt contains:

    <#@ template debug="false" hostspecific="false" language="C#" #>
    <#@ output extension=".cs" #>
    
    using System.Reflection;
    
    [assembly: AssemblyVersion("<#= this.Major #>.<#= this.Minor #>.<#= this.DaysSinceProjectStarted #>.<#= this.MinutesSinceMidnight #>")]
    [assembly: AssemblyInformationalVersion("<#= this.Major #>.<#= this.Minor #>.<#= this.DaysSinceProjectStarted #>.0")]
    [assembly: AssemblyFileVersion ("<#= this.Year #>.<#= this.Month #>.<#= this.Day #>.<#= this.MinutesSinceMidnight #>")]
    
    <#+
    
     int Major = 1;
     int Minor = 2;
    
     static DateTime ProjectStartedDate = new DateTime(year: 2022, month: 1, day: 1);
     static DateTime uNow = DateTime.UtcNow;
    
     int DaysSinceProjectStarted = (int)((uNow - ProjectStartedDate).TotalDays);
    
     int MinutesSinceMidnight = (int)uNow.TimeOfDay.TotalMinutes;
    
     int Year = uNow.Year;
     int Month = uNow.Month;
     int Day = uNow.Day;
    
    #>
    

    In my case, the generated VersionAutoIncrement.cs (currently) contains:

    using System.Reflection;
    
    [assembly: AssemblyVersion("1.2.940.1336")]
    [assembly: AssemblyInformationalVersion("1.2.940.0")]
    [assembly: AssemblyFileVersion ("2024.7.29.1336")]