Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

I had the exact same issue. I followed the steps in this article http://msdn.microsoft.com/en-us/library/ee789839.aspx about splitting off the templates into another project and sharing the output files.

It details how to switch off the TextTemplatingFileGenerator tool attached to the template by right clicking the template and clearing the CustomTool property. This stops the template generating code when saved ... but it STILL RUNS when switching tabs!

I think the only way to get round this would be to move all your template code into a new file with a different suffix (like ttinclude or t4 or something) and then include this file in your actual T4 template file using the include directive. That way you will never need to open that file to edit the template so it wont run by accident.

So in one file called MyTemplate.tt:

<#@ template language="VB" debug="false" hostspecific="true"#>
<#@ include file="Include\MyTemplateCodeBehind.t4" #>
<#@ output extension=".vb"#>
<# ' Nothing to see here! #>

Whilst in the other file called MyTemplateCodeBehind.t4:

<#@ template language="VB" debug="false" hostspecific="true"#>
<#
   For Each something In somecollection
#>
   <#= something.PrintMyCode() #>
<#
   Next

#>

What I'm doing (probably a bad method) is writing at the beginning of the tt file an exception line like:

<# throw new Exception(); #>

Because I throw an Exception the process stop and when I finish all the work I just have to remove this line. :)

Try right after the compile directives, add a return to exit method

<#@ template language="C#" debug="false" hostspecific="true"#>
<#@ include file="EF6.Utility.CS.ttinclude"#><#@ 
 output extension="Repository.cs"#><#
return string.Empty;     //<-- add this line!!! 

...

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top