T4Executer VS2019 Extension

Visual Studio 2019 released earlier this week. When I build my experimental TexTran project, I noticed none of the T4 Templates were executed. I used AutoT4 in the past (a VS2012 extension) but this package is not available in the latest 2019 version.

I added an extension to the Visual Studio Marketplace called TT4Executer. When installed, in executes all T4 Templates every time you build or rebuild your solution. This is enabled by default, and an be toggled in the Extensions > T4Executer > Enable/Disable menu.

It’s a pretty simple package, that adds an event to the OnBuildBegin event that fetches all .tt files in all active projects and executes them in before building your projects.

This is my first VSIX project, and it’s open for contributions and improvements, the source code can be found here.

Update: I added an options dialog where you can select which templates to ignore. This is found in the Extensions > T4Executer > Ignore Template menu.

Update 2: I a new option where you can specifiy to execute templates before or after build or ignore them. This can be configured via Extensions > T4Executer > Configure.

T4 Tutorial: Execute templates at build time

This is not so hard to do actually. It ensures your transformations are executed when you build your solution, when in some cases you did not click ‘Build > Transform all T4 Templates’ when changes are made to your .tt files.

If one of your projects uses T4 to generate code, and you want it to execute at build time, consider the next steps:

Unload your project

Righ-click on the project in your solution explorer and click ‘unload’.

Open the .csproj of your project

Right-click again and select ‘edit projectname.csproj’

Edit the .csproj file

Add following PropertyGroup at the beginning of your csproj file (for VS2017):

<PropertyGroup>
    <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">15.0</VisualStudioVersion>
    <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
    <TransformOnBuild>true</TransformOnBuild>
   <OverwriteReadOnlyOutputFiles>true</OverwriteReadOnlyOutputFiles>
    <TransformOutOfDateOnly>false</TransformOutOfDateOnly>
</PropertyGroup>

The TransformOnBuild property set tot true ensures the templates to be transformed when you build your project (false by default).

OverwriteReadOnlyOutputFiles forces overwriting of readonly output files.

Set TransformOutOfDateOnly to false to transform files even if the output is up to date.

At the end of yoru csproj file, add following import:

<Import Project="$(VSToolsPath)\TextTemplating\Microsoft.TextTemplating.targets" />

Now you can reload your project and you are done. All T4 Templates within this project will be executed on each build.