用户登录
用户注册

分享至

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

  • 作者: 可可12263586
  • 来源: 51数据库
  • 2023-02-13

问题描述

在 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.

软件
前端设计
程序设计
Java相关