How to run the TextTemplatingFileGenerator on Build (VS 2017)
Asked Answered
D

3

6

I've found this question asked before but all the answers I've come across have been specific to earlier versions of Visual Studio. Seems mostly VS2015 and earlier.

The main issue with most of the answers is that they rely on the existence of Microsoft.TextTemplating.targets and/or TextTransform.exe which previously was installed with earlier versions of Visual Studio and VS2017 doesn't any longer install the corresponding directories or files; from my understanding, its due to the change in architecture in this respect.

I've attempted to use Clarius.TransformOnBuild and it worked fine (once) but then started throwing a "TransformOnBuildTask" task failure due to some access denied issue that I've seen others have.

Downgrading to an earlier version of the package resolves the error, but then it doesn't run the TextTemplatingFileGenerator on build any longer either. This just doesn't seem to be a very reliable approach.

Hadn't tried AutoT4 as others have suggested because the approach needs to be simple and without having need for all development team members to modify their environments.

Other solutions suggest adding TextTransform.exe to the %PATH% variable, which again requires the team to perform mods to their environments. Not to mention the fact that I don't have the TextTransform.exe because of the first point and there is no guarantee that other developers on the team will either. Everyone is running VS2017

I just need a very simple approach to have all the .tt files in my project executed during any build without having need for the entire development team to make individual system mods for this to work.

If anyone else every had a similar requirement running under VS2017 I'd be interested in the solution.

Deste answered 1/2, 2018 at 17:51 Comment(0)
A
10

How to run the TextTemplatingFileGenerator on Build (VS 2017)

Just as you know, if you want to execute all the .tt files in you project during the build, you have to use the Microsoft.TextTemplating.targets, TextTransform.exe, AutoT4 or any other extension. All of these methods require our development team to configure their environment individual more or less.

In order to reduce the development team members personal configuration, we usually use Microsoft.TextTemplating.targets. Since 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), so we have to install it via the Visual Studio extension development toolset in the VS2017 installer (Text Template Transformation feature):

enter image description here

After install this workload, then you can use MSBuild to transform templates by importing the relevant targets into the MSBuild project:

  <PropertyGroup>
    <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
    <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
    <TransformOnBuild>true</TransformOnBuild>
    <OverwriteReadOnlyOutputFiles>true</OverwriteReadOnlyOutputFiles>
    <TransformOutOfDateOnly>false</TransformOutOfDateOnly>
  </PropertyGroup>

  <!-- This is the important line: -->
  <Import Project="$(VSToolsPath)\TextTemplating\Microsoft.TextTemplating.targets" />

See Code Generation in a Build Process for details.

Hope this helps.

Ambulant answered 2/2, 2018 at 4:36 Comment(3)
Thanks! Actually this solution works great. I was really looking for a solution where nothing additional was required on any of the other developers systems but this approach isn't that bad. I did note that the template generator is only getting triggered on a "Rebuild" and not a "Build". Do you have any suggestions to get it to execute on a Build? Or is it working that way for you as well?Deste
Important The import must be after the Microsoft.CSharp.targets import! Otherwise it doesn't recognize the .tt-files! This is explained in the Microsoft.TextTemplating.targets file itself.Knotted
I use exactly the same thing, but it doesn't work for me for some reason. Any ideas? Key points? Actually, we setup this a while ago, but can't run it anymore. We used VS2017 but have now switched to VS2019 and it doesn't work for ALL .tt, only for some (no idea)Shovelhead
L
4

In Visual Studio 2017 (probably next versions too), you should add this in Pre-build event:

"$(DevEnvDir)TextTransform.exe" -out "$(ProjectDir)YourTemplate.cs" "$(ProjectDir)YourTemplate.tt"

Simple solution without need to install Visual Studio extension development.

p.s. Change path to your template if it's located not in root project directory.

Larock answered 21/6, 2019 at 15:36 Comment(1)
I honestly feel like this is the more appropriate answer since it does not rely on the user having the VS Extensions development options installed. This is great for users who only require a minimal installation which includes the TextTemplate.exe in the software when installed. If I were to transfer the solution to a coworker or friend's computer then they would also need the extensions development installed.Inattentive
M
0

After trying, for simplicity, you can add the following to your project's .csproj file,implement automatic conversion of all T4 templates at generation time:

<PropertyGroup>
  <TransformOnBuild>true</TransformOnBuild>
  <TransformOutOfDateOnly>false</TransformOutOfDateOnly>
</PropertyGroup>
<Import Project="$(MSBuildToolsPath)\..\..\..\Microsoft\VisualStudio\v$(VisualStudioVersion)\TextTemplating\Microsoft.TextTemplating.targets" />

The aim is to find the Microsoft.TextTemplating.targets file.

There may be an easier way,but it works fine in VS2022.

Misleading answered 24/8, 2024 at 9:34 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.