views:

40

answers:

2

When I edit T4, the script is executed every time I switch to another file. It is OK for quick simple scripts, but some scripts take long time to execute. Is there a way to disable this behavior? I want the script to run only when I save T4 file or manually choose "Run Custom Tool" from the menu.

A: 

T4 templates are executed when the file is saved. If you have VS setup to auto-save when you tab away from the file that could explain the behavior. Review your VS configuration to determine if VS is saving the file when you tab away.

Dave Swersky
@DaveSwersky The question is, where is this setting?
Athari
+1  A: 

T4 is connected to the custom tool mechanism (IVsSingleFileGenerator) in the C#/VB project systems, which gives it the run on save, run custom tool menu and also the run on tab switching behavior - all for the price of implementing a simple interface.

Unfortunately this means that T4 also has essentially no control over those behaviors, which are the standard for custom tools.

An alternative may be to use the T4 MsBuild support in the VS Modeling and Visualization SDK to do T4 at build time and then disable the custom tool. I'll enquire with my colleague who built the msbuild support if it uses the custom tool to identify the set of templates or not and post back to the thread.

GarethJ
I use T4 Toolbox which generates multiple files and thus modifies the solution. I'm not sure it'll work in MsBuild. Running T4 only at build time is not an option for me anyway, I want to see results immidiately, when I save T4 file. But you're welcome to provide information on using MsBuild for others those who find this question. It may be useful for them.
Athari
I see - we've not tried integrating the two - Oleg may well have done. I did just confirm that the custom tool is no longer required for msbuild, but instead you'd need to change the item type in your csproj/vbproj file from None/Compile to T4Transform or T4Preprocess.The output files would then no longer be part of your project. There's metadata that can redirect them to the intermediate obj folder or somewhere similar.
GarethJ