简体   繁体   中英

T4 templates not transformed during build

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?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM