Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run the TextTemplatingFileGenerator on Build (VS 2017)

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.

like image 570
Mark Avatar asked Feb 01 '18 17:02

Mark


People also ask

How do TT files work?

Template file created by Visual Studio, a software development tool created by Microsoft; contains both text blocks and control logic used for generating new text files; can be written using Visual C# or Visual Basic code; used for both runtime text generation as well as source code generation.

What is transform all T4 templates?

t4 is basically a tool built into VS for doing text transformation, typically for doing code generation. Transform All T4 Templates searches your solution for *. tt files and executes them to create other text, again typically source code, files.


1 Answers

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.

like image 106
Leo Liu-MSFT Avatar answered Oct 27 '22 01:10

Leo Liu-MSFT