MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板

MSBuild support for T4 templates in Visual Studio 2017 RTM(MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板)
本文介绍了MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Visual Studio 2015 中,我使用的是 NuGet 包 Unofficial.Microsoft.VisualStudio.TextTemplating.14.0.0,它允许我在构建项目时直接从 MSBuild 转换 T4 模板.

In Visual Studio 2015, I'm using the NuGet package Unofficial.Microsoft.VisualStudio.TextTemplating.14.0.0 which allows me to transform T4 templates directly from MSBuild, whenever a project is built.

然而,在 Visual Studio 2017 RTM 中,这会通过以下消息中断构建:

In Visual Studio 2017 RTM however, this breaks the build with the following messages:

运行转换代码时引发异常.该过程无法继续.引发了以下异常:System.IO.FileNotFoundException:无法加载文件或程序集Microsoft.CodeAnalysis,Version=1.3.1.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35"或其依赖项之一.该系统找不到指定的文件.文件名:'Microsoft.CodeAnalysis,版本=1.3.1.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35'

这是由此包中的文件 Unofficial.Microsoft.VisualStudio.TextTemplating.targets(396,5) 引发的.

This is raised by the file Unofficial.Microsoft.VisualStudio.TextTemplating.targets(396,5) which is in this package.

我的猜测是,由于环境不匹配,试图从 VS 2017 构建中使用这些目标,但我不知道如何追踪确切的问题.我还没有看到 v15 的更新包.

My guess is that the error comes from trying to use these targets from a VS 2017 build due to mismatched environments, but I don't know how to trace the exact issue. There is no updated package yet for v15 that I can see.

如何从适用于 VS 2017 的 MSBuild 进行 T4 转换?是否会在某个时候使用来自 NuGet 的新包,还是不再支持?

How can I do T4 transforms from MSBuild that will work for VS 2017? Will there be a new package from NuGet to use at some point or is this not going to be supported anymore?

推荐答案

我找到了正确的解决方案.

I found the right solution.

事实证明,T4 SDK 现在包含在 Visual Studio 2017 中(而不是像过去那样包含在单独的建模 SDK 中),但是您必须通过 Visual Studio 扩展安装它VS2017 安装程序中的开发工具集(文本模板转换功能).

Turns out that the T4 SDK is now included as part of Visual Studio 2017 (and not part of the separate Modeling SDK as it has been in the past), BUT you have to install it via the Visual Studio extension development toolset in the VS2017 installer (Text Template Transformation feature).

安装后,您可以使用 MSBuild 通过将相关目标导入 MSBuild 项目来转换模板:

Once this is installed, you can use MSBuild to transform templates by importing the relevant targets into the MSBuild project:

<PropertyGroup>
    <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">15.0</VisualStudioVersion>
    <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)MicrosoftVisualStudiov$(VisualStudioVersion)</VSToolsPath>
    <TransformOnBuild>True</TransformOnBuild>
    <TransformOutOfDateOnly>false</TransformOutOfDateOnly>
</PropertyGroup>

<!-- add AFTER import for $(MSBuildToolsPath)Microsoft.CSharp.targets -->
<Import Project="$(VSToolsPath)TextTemplatingMicrosoft.TextTemplating.targets" />

这解决了我的问题,也不再需要单独的非官方 NuGet 包.

This solved my problem and also removes the need for the separate unofficial NuGet package.

这篇关于MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)