Reputation: 1687
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.
Upvotes: 6
Views: 9889
Reputation: 1
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.
Upvotes: 0
Reputation: 169
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.
Upvotes: 4
Reputation: 76770
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.
Upvotes: 10