How do I get a T4 template to generate its output on every build? As it is now, it only regenerates it when I make a change to the template.
I have found other questions similar to this:
T4 transformation and build order in Visual Studio (unanswered)
How to get t4 files to build in visual studio? (answers are not detailed enough [while still being plenty complicated] and don't even make total sense)
There has got to be a simpler way to do this!
If you're using Visual Studio 2010, you can use the Visual Studio Modeling and Visualization SDK: http://code.msdn.microsoft.com/vsvmsdk
This contains msbuild tasks for executing T4 templates at build time.
Have a look at Oleg's blog for more explanation: http://www.olegsych.com/2010/04/understanding-t4-msbuild-integration
Another good article on this: Code Generation in a Build Process
2012 Modeling and Visualization SDK download link :
https://www.microsoft.com/en-us/download/details.aspx?id=30680
Hey, my script can also parse output extension
Just create
transform_all.bat $(SolutionDir)
pre-build event, and all *.tt files in your solution will be transformed automaticaly.You just need to add this command to the pre-build event of the project:
The check on configuration = debug, makes sure that you don't regenerate the code in the release mode, when you do the build on the TFS build server for instance.
In visual studio 2013, right click the T4 template and set the transform on build property to true.
I used MarkGr's answer and developed this solution. First, create a batch file called RunTemplate.bat in a separate tools folder above the main solution folder. The batch file just has the line:
This batch file takes 2 parameters... %1 is the path to the .tt file without the .tt extension. %2 is the path to any DLLs referred to by Assembly directives in the template.
Next, go into the Project Properties of the project containing the T4 template. Go into Build Events and add the following Pre-build event command line:
replacing MyTemplate with filename of your .tt file (i.e. MyTemplate.tt) without the .tt extension. This will have the result of expanding the template to produce MyTemplate.cs before building the project. Then the actual build will compile MyTemplate.cs