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!
Check out C:\Program Files (x86)\Common Files\Microsoft Shared\TextTemplating there is a command line transformation exe in there. Alternatively write a MSBuild task with a custom host and do the transform yourself.
There is a great NuGet package that does just this:
Details about the package can be found here
The pre-build can be reduced to a single line:
This transforms all
.tt
files in the project and lists them to the build output.If you don't want the build output then you have to work around some "interesting behaviour":
Of course, you can pull this out into a batch file to which you pass the project directory path if you wish.
NB The path may require some tweaking. The path above is where VS 2008 installed it on my machine; but you might find that the version number between
TextTemplating
andTextTransform.exe
is different.I used JoelFan's answer to come up w/ this. I like it better because you don't have to remember to modify the pre-build event every time you add a new .tt file to the project.
%PATH%
transform_all ..\..
"transform_all.bat
Please see mhutch's answer https://stackoverflow.com/a/1395377/9587
IMHO, this is the best build server and dev environment friendly option.
Expanding on Seth Reno and JoelFan's answers, I came up with this. With this solution don't need to remember to modify the pre-build event every time you add a new .tt file to the project.
Implementation Procedure
transform_all.bat "$(ProjectDir)" $(ProjectExt)
for each project with a .tt you want to buildtransform_all.bat
NOTES
The text transformation assumes the code in the T4 template is the same language as your project type. If this case does not apply to you, then you will have to replace the
$(ProjectExt)
argument with the extension of the files you want the code generate..TT
files must be in the project directory else they won't build. You can build TT files outside the project directory by specifying a different path as the first argument (i.e. replace"$(ProjectDir)"
with the path containing the TT files.)Remember also to set the correct path to the
transform_all.bat
batch file.For example, I placed it in my solution directory so the pre-build event was as follows
"$(SolutionDir)transform_all.bat" "$(ProjectDir)" $(ProjectExt)