Reputation: 2103
I have tried to compile my T4 templates into a c# file.
What I have tried from Microsoft: Invoke text transformation in the build process
By adding in my .csproj file:
<Import Project="TextTemplating\Microsoft.TextTemplating.targets" />
<PropertyGroup>
<TransformOnBuild>true</TransformOnBuild>
<OverwriteReadOnlyOutputFiles>true</OverwriteReadOnlyOutputFiles>
<TransformOutOfDateOnly>false</TransformOutOfDateOnly>
</PropertyGroup>
<ItemGroup>
<T4ParameterValues Include="ProjectDir">
<Value>$(ProjectDir)</Value>
<Visible>false</Visible>
</T4ParameterValues>
</ItemGroup>
TextTemplating
is a directory containing the TextTemplating files from my editor located at:
C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\msbuild\Microsoft\VisualStudio\v16.0\TextTemplating
The base template (named ModelTemplate.tt):
<#@ template language="C#" #>
<#@ parameter name="ClassName" type="System.String"#>
<#@ parameter name="Namespace" type="System.String"#>
namespace <#= Namespace #>
{
public class <#= ClassName #>
{
}
}
And finally the template for testing the ModelTemplate.tt (named ModelTemplateTest.tt):
<#@ template debug="false" language="C#" #>
<#@ output extension=".cs" #>
<#
_ClassNameField = "Model";
_NamespaceField = "MyNamespace";
#>
<#@ include file="$(ProjectDir)\Templates\ModelTemplate.tt"#>
A custom tool 'TextTemplatingFilePreprocessor' is associated with file 'Templates\ModelTemplate.tt', but the output of the custom tool was not found in the project. You may try re-running the custom tool by right-clicking on the file in the Solution Explorer and choosing Run Custom Tool.
BUT the ModelTemplateTest.tt is compiled into:
namespace MyNamespace
{
public class Model
{
}
}
How I can call TextTemplatingFilePreprocessor
in my build?
Upvotes: 5
Views: 2325
Reputation: 2103
The solution (with Visual Studio Enterprise 2019) was to import $(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\TextTemplating\Microsoft.TextTemplating.targets
in the .csproj
file as follows:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<TransformOnBuild>true</TransformOnBuild>
</PropertyGroup>
<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\TextTemplating\Microsoft.TextTemplating.targets" />
</Project>
Related StackOverflow thread: Product of build-time T4 transformation is used only in the next build
Upvotes: 4
Reputation: 612
T4Executer might be a solution, it is an VS extension that allows you to trigger the execution of specific T4 files on project build time.
Upvotes: 1