What I have done
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 templates
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"#>
Output from the build
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?