How to run the TextTemplatingFileGenerator on Build (VS 2017)
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):
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.
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.