要在构建 dotnet build 时支持构建 T4 模板,您需要使用 .NET Core (https://github.com/atifaziz/t5) 已经存在的 Custom Text Template Host。要包含它,请在您的项目中添加任何 ItemGroup 这个元素:
<DotNetCliToolReference Include="T5.TextTransform.Tool" Version="1.1.0-*" />。
由于 Visual Studio 已经拥有自己的 Text Template Host 实现,因此您添加的元素应仅适用于 .NET Core。例如:
<ItemGroup Condition="'$(MSBuildRuntimeType)'=='Core'">
<DotNetCliToolReference Include="T5.TextTransform.Tool" Version="1.1.0-*" />
</ItemGroup>
同时,您应该在 .NET Core 之外调整您对 Visual Studio 的文本模板主机的设置,如下所示:Condition="'$(MSBuildRuntimeType)'=='Full'"。
您还应该在导入 Microsoft.TextTemplating.targets 之前添加 <Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" Condition="'$(MSBuildRuntimeType)'=='Full'" /> 以使一切在 Visual Studio 中使用 .NET Core csproj 正常工作。
如果您需要能够清理所有生成的代码,您应该将您的模板从*.tt 重命名为*.Generated.tt,所有代码将在*.Generated.cs 下生成,并且可以过滤掉这些文件在dotnet clean 行动。
csproj 中的完整示例:
<!-- T4 build support for .NET Core (Begin) -->
<ItemGroup Condition="'$(MSBuildRuntimeType)'=='Core'">
<DotNetCliToolReference Include="T5.TextTransform.Tool" Version="1.1.0-*" />
<TextTemplate Include="**\*.Generated.tt" />
<Generated Include="**\*.Generated.cs" />
</ItemGroup>
<Target Name="TextTemplateTransform" BeforeTargets="BeforeBuild" Condition="'$(MSBuildRuntimeType)'=='Core'">
<ItemGroup>
<Compile Remove="**\*.cs" />
</ItemGroup>
<Exec WorkingDirectory="$(ProjectDir)" Command="dotnet tt %(TextTemplate.Identity)" />
<ItemGroup>
<Compile Include="**\*.cs" />
</ItemGroup>
</Target>
<Target Name="TextTemplateClean" AfterTargets="Clean">
<Delete Files="@(Generated)" />
</Target>
<!-- T4 build support for .NET Core (End) -->
<!-- T4 build support for Visual Studio (Begin) -->
<PropertyGroup Condition="'$(MSBuildRuntimeType)'=='Full'">
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
<!-- This is what will cause the templates to be transformed when the project is built (default is false) -->
<TransformOnBuild>true</TransformOnBuild>
<!-- Set to true to force overwriting of read-only output files, e.g. if they're not checked out (default is false) -->
<OverwriteReadOnlyOutputFiles>true</OverwriteReadOnlyOutputFiles>
<!-- Set to false to transform files even if the output appears to be up-to-date (default is true) -->
<TransformOutOfDateOnly>false</TransformOutOfDateOnly>
</PropertyGroup>
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" Condition="'$(MSBuildRuntimeType)'=='Full'" />
<Import Project="$(VSToolsPath)\TextTemplating\Microsoft.TextTemplating.targets" Condition="'$(MSBuildRuntimeType)'=='Full'" />
<!-- T4 build support for Visual Studio (End) -->
如果您不想重命名模板文件并且不需要清理它们,请替换:
<TextTemplate Include="**\*.Generated.tt" />
<Generated Include="**\*.Generated.cs" />
与:
<TextTemplate Include="**\*.tt" />
然后删除:
<Target Name="TextTemplateClean" AfterTargets="Clean">
<Delete Files="@(Generated)" />
</Target>
欲了解更多信息,请参阅:
如何在dotnet build 上设置代码生成:
https://notquitepure.info/2018/12/12/T4-Templates-at-Build-Time-With-Dotnet-Core/
如何在构建时为 Visual Studio 和 .NET Core csproj 设置代码生成:
https://thomaslevesque.com/2017/11/13/transform-t4-templates-as-part-of-the-build-and-pass-variables-from-the-project/
从单个 T4 模板生成多个文件的完整示例:
https://github.com/Konard/T4GenericsExample
更新:
GitHub.com/Mono/T4is even better.