How do I get a T4 template to generate its output on every build? As it is now, it only regenerates it when I make a change to the template.
I have found other ques
Check out C:\Program Files (x86)\Common Files\Microsoft Shared\TextTemplating there is a command line transformation exe in there. Alternatively write a MSBuild task with a custom host and do the transform yourself.
If you're using Visual Studio 2010, you can use the Visual Studio Modeling and Visualization SDK: http://code.msdn.microsoft.com/vsvmsdk
This contains msbuild tasks for executing T4 templates at build time.
Have a look at Oleg's blog for more explanation: http://www.olegsych.com/2010/04/understanding-t4-msbuild-integration
T4Executer does this for VS2019. You can specify templates to ignore on build, and there is a execute after build option.
Probably the simplest way is to install a Visual Studio extension called AutoT4.
It runs all T4 templates on build automagically.
Thanks to GitHub.com/Mono/T4, at the moment you can do it for both .NET Core and Visual Studio builds by adding this to your .csproj
file:
<ItemGroup>
<DotNetCliToolReference Include="dotnet-t4-project-tool" Version="2.0.5" />
<TextTemplate Include="**\*.tt" />
</ItemGroup>
<Target Name="TextTemplateTransform" BeforeTargets="BeforeBuild">
<ItemGroup>
<Compile Remove="**\*.cs" />
</ItemGroup>
<Exec WorkingDirectory="$(ProjectDir)" Command="dotnet t4 %(TextTemplate.Identity)" />
<ItemGroup>
<Compile Include="**\*.cs" />
</ItemGroup>
</Target>
If you transform your templates to different programming languages you should add something like <Compile Remove="**\*.vb" />
and <Compile Include="**\*.vb" />
in order to get these files compiled even if you don't have generated files yet.
Remove
and Include
trick only needed for first time generation, or you can make the XML-shorter like this:
<ItemGroup>
<DotNetCliToolReference Include="dotnet-t4-project-tool" Version="2.0.5" />
<TextTemplate Include="**\*.tt" />
</ItemGroup>
<Target Name="TextTemplateTransform" BeforeTargets="BeforeBuild">
<Exec WorkingDirectory="$(ProjectDir)" Command="dotnet t4 %(TextTemplate.Identity)" />
</Target>
and just run build twice (for the first time). If you already have generated files committed to the repository there will be no problems on rebuilds with both examples.
In the Visual Studio you might want to see something like this:
instead of this:
So add something like this to your project file:
<ItemGroup>
<Compile Update="UInt16Class.cs">
<DependentUpon>UInt16Class.tt</DependentUpon>
</Compile>
<Compile Update="UInt32Class.cs">
<DependentUpon>UInt32Class.tt</DependentUpon>
</Compile>
<Compile Update="UInt64Class.cs">
<DependentUpon>UInt64Class.tt</DependentUpon>
</Compile>
<Compile Update="UInt8Class.cs">
<DependentUpon>UInt8Class.tt</DependentUpon>
</Compile>
</ItemGroup>
Complete example here: GitHub.com/Konard/T4GenericsExample (includes generation of multiple files from single template).
In Visual Studio 2017 (probably next versions too), you should add this in Pre-build event:
"$(DevEnvDir)TextTransform.exe" -out "$(ProjectDir)YourTemplate.cs" "$(ProjectDir)YourTemplate.tt"
p.s. Change path to your template if it's located not in root project directory.