T4Editor VS2019 Extension: Syntax highlighting for T4 Templates

Several developers complain about the lack of a proper T4 editor experience, as everything is just shown as plain text. See my other T4 tutorials to see the regular T4 text files in the header image.

I created a Visual Studio extension called T4Editor that adds colors to different blocks in you code. You can easily spot the difference between output code, control blocks and class feature blocks. This increases coding productivity and readability of your T4 templates.

In the future, C# code inside these blocks will also be highlighted with the propper C# syntax colors, I’m still working on that.

Update: Now you can set custom colors! Navigate to Extensions – T4Editor – Adjust Colors and pick whatever colors you want.

Update 2: There is now code completion/minor custom intellisense for codeblocks!

Update 4: I have implemented brace matching and improved RegEx. Typing ‘var string = “#>” for example, will not act as a closing tag.

Click here to see the code. Feel free to give feedback or even contribute!

T4Executer VS2019 Extension

When working with T4 templates in Visual Studio, you have to manually execute the custom for it to trigger the code generation. I used AutoT4 in the past (a VS2012 extension) to execute template on build time but this extension is not supported in the latest version(s) of Visual Studio.

T4Executer is open for contributions and improvements, the source code can be found here. To use, just install the extension. You can download it at the marketplace.

It is a pretty simple package, that ties execution of specified T4 templates to BuildEvents (BuildEvents interface). It’s possible to configure which templates to run before build, after build or just ignore a template completely.

Usage

You can configure T4Executer viaExtensions - T4Executer - Configure. A window will open that lists all templates found in your solutions’ projects.

Click on the template you want and specify to run before or after build by clicking the button under the list. You can also select multiple templates at a once. Templates you move to ignore will never be run, this can be useful for .tt files which don’t generate code or just hold some Class Feature Blocks or a collection of Import Directives. Note that the extension filters out .ttinclude files as this extension is mainly used for files that do not output generated code.

By default all templates are executed before your projects build event, when building or rebuilding your solution. Selecting Extensions - T4Executer - Disable will disable this default behaviour.

Timestamps

The timespamp of the generated the generated file is by default not preserved when the content of the file is not changed. There is a option to toggle this via Extensions - T4Executer - Preserve Timestamps.

I hope this helps you while working with T4 Templates in your solution. If you have any questions or feedback, please leave a comment below.

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 your 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.