MSBuild support for T4 templates in Visual Studio

2019-01-18 03:38发布

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.

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

An Exception was thrown while running the transformation code. The process cannot continue. The following Exception was thrown: System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.CodeAnalysis, Version=1.3.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified. File name: 'Microsoft.CodeAnalysis, Version=1.3.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'

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

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.

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?

3条回答
我想做一个坏孩纸
2楼-- · 2019-01-18 04:09

I found the right solution.

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

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)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
    <TransformOnBuild>True</TransformOnBuild>
    <TransformOutOfDateOnly>false</TransformOutOfDateOnly>
</PropertyGroup>

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

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

查看更多
啃猪蹄的小仙女
3楼-- · 2019-01-18 04:16

I had a similar problem. My T4 wouldn't generate on build, but would on save. This was bizarre as I didn't get an error but upon reading @Sam's answer I figured something is wrong with my VS installation. And I was right. VS 2017 15.9.4 installs in it's own install directory, but doesn't copy Tools to the VSToolsPath folder. Instead it just leaves them where they are. So, for me, the correct solution was to use this <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(VsInstallRoot)\MSBuild\Microsoft\VisualStudio\v15.0\TextTemplating\Microsoft.TextTemplating.targets" />

查看更多
一纸荒年 Trace。
4楼-- · 2019-01-18 04:26

I've had a similar issue where my Hosted Agent on Visual Studio Team Service did not generate the template output thus breaking my build server since it was missing the generated CS files.

The CS template output is generated just fine when building from Visual Studio 2015 on my development machine.

Looking at various solutions such as the one above it became clear to me that the more expedient fix was simply to commit the generated files to my source control system. That has the added advantage that I can code review any changes in the output and not just the template.

查看更多
登录 后发表回答