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!
Dynamo.AutoTT will do what you need. You can configure it to watch files via a regex or generate on build. It also allows you to specify which T4 templates you want it to trigger.
You can download it from here : https://github.com/MartinF/Dynamo.AutoTT
Just build it, copy the dll and AddIn files into
C:\Users\Documents\Visual Studio 2012\Addins\
and away you go.
If you want to get it going in VS2012 you will need to modify the a Dynamo.AutoTT.AddIn file and set the Version to 11.0 inside the AddIn file;
Here is how I tacked it. Link. Basically building on top of a great blog( blogs.clariusconsulting.net/kzu/how-to-transform-t4-templates-on-build-without-installing-a-visual-studio-sdk/ can't post more that 2 links :( ) I came up with This .targets file for use with visual studio proj files.
It's useful when you are using other dll-s inside of your .tt and you want the result to change as the dll-s are changing.
How it works:
Remove the assembly references from .tt
Inside the proj file use this code to set up transform on build:
First part Locates TextTransform.exe
$(IncludeForTransform)
will be equal toc:\path\to\dll\foo.dll' -r c:\path\to\dll\bar.dll
because that's the way to add references for the TextTransform on the command line<_TextTransform Include="$(ProjectDir)**\*.tt" />
this creates a list of all tt files inside the project and subdirectories<Exec Command="...
produces a line for each of the found .tt files that looks like"C:\path\to\Transform.exe" "c:\path\to\my\proj\TransformFile.tt" -r"c:\path\to\foo.dll" -r "c:\path\to\bar.dll"
The only thing left to do is add the paths to the dlls inside of:
Here
<InProject>False</InProject>
hides these items from the Solution ViewSo now you should be able to generate your code on build and on change of dll-s.
You can remove the custom tool (from properties inside of Visual Studio) so the VS does not try to transform and fail miserably every time. Because we removed the assembly references in step 2
Some guy built a nuget package for this.
Side note: I get compile errors from both TextTemplate.exe and that package (because that package calls TextTemplate.exe) but not from Visual Studio. So apparently the behavior is not the same; heads up.
EDIT: This ended up being my problem.
Recently found this great VS plugin, Chirpy.
Not only does it generate your T4 on a build, but it allows T4-based approach to minification of javascript, CSS, and even lets you use LESS syntax for your CSS!
Here is my solution - similar to the accepted answer. We had a problem with our source control. The target .cs files are read-only and the T4 was failing. Here is the code, that runs T4 in temp folder, compares target files, and copies it only in case of same change. It does not fix the problem with read.only files, but at least it does not occur very often:
Transform.bat
You can try to add your check-out command on a line (:: You can try ....)
In your project set this as a prebuild action:
I agree with GarethJ - in VS2010 it is much easier to regenerate tt templates on each build. Oleg Sych's blog describes how to do it. In short:
</Project>
That's it. Open your project. On each build all *.tt templates will be reprocessed