Geo*_*org 13 t4 .net-core visual-studio-2017
我最近将测试项目迁移到.NET Core 2.0.该测试项目使用文本模板生成一些重复的代码.之前的项目有一个构建目标,可以在构建之前生成所有T4模板.因此,生成的代码也不会签入VCS.
我在项目中使用了这个片段来确保构建模板:
<PropertyGroup>
<!-- Default VisualStudioVersion to 15 (VS2017) -->
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">15.0</VisualStudioVersion>
<!-- Determinate VSToolsPath -->
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
<!-- Run T4 generation if there are outdated files -->
<TransformOnBuild>True</TransformOnBuild>
<TransformOutOfDateOnly>True</TransformOutOfDateOnly>
</PropertyGroup>
<!-- Import TextTemplating target -->
<Import Project="$(VSToolsPath)\TextTemplating\Microsoft.TextTemplating.targets" />
Run Code Online (Sandbox Code Playgroud)
我的第一个方法是保留这个片段并将其复制到新的.NET Core项目文件中.
在Visual Studio中,这很有效,因为显然,VSToolsPath设置正确.但是,当我运行.NET Core SDK工具时dotnet test(例如我在构建服务器上),VSToolsPath映射到Program Files\dotnet\sdk\2.0.3那里,无法找到文本模板目标.
因为这不起作用,我也尝试简单地Microsoft.VisualStudio.TextTemplating从Nuget 安装包,但这有两个问题:
为了在构建时支持构建 T4 模板,dotnet build您需要使用Custom Text Template Host.NET Core 中已经存在的 ( https://github.com/atifaziz/t5 )。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>
Run Code Online (Sandbox Code Playgroud)
同时,您应该在 .NET Core 之外调整 Visual Studio 文本模板主机的设置,如下所示: .NET Condition="'$(MSBuildRuntimeType)'=='Full'"Core
您还应该<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" Condition="'$(MSBuildRuntimeType)'=='Full'" />在导入之前添加Microsoft.TextTemplating.targets,以使所有内容都能csproj在 Visual Studio 中与 .NET Core 正常工作。
如果您需要能够清理所有生成的代码,您应该将模板从 重命名为*.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) -->
Run Code Online (Sandbox Code Playgroud)
如果您不想重命名模板文件并且不需要清理它们,请替换:
<TextTemplate Include="**\*.Generated.tt" />
<Generated Include="**\*.Generated.cs" />
Run Code Online (Sandbox Code Playgroud)
和:
<TextTemplate Include="**\*.tt" />
Run Code Online (Sandbox Code Playgroud)
并删除:
<Target Name="TextTemplateClean" AfterTargets="Clean">
<Delete Files="@(Generated)" />
</Target>
Run Code Online (Sandbox Code Playgroud)
欲了解更多信息,请参阅:
如何设置代码生成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-来自项目的变量/
从单个 T4 模板生成多个文件的完整示例: https://github.com/Konard/T4GenericsExample
更新:
小智 1
您受到为 dotnet core 编写端口的人的摆布。
\n\n这是旧的:http ://www.bricelam.net/2015/03/12/t4-on-aspnet5.html
\n\n或者完全使用不同的模板工具,尽管我当然知道 \xe2\x80\x99s 是否不是一个选项。
\n| 归档时间: |
|
| 查看次数: |
902 次 |
| 最近记录: |