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.
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
#>
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.
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!!!
...
I have found it useful when developing a T4 template to use the following code snippet at the top of the T4 file:
<# //throw exception to halt execution during development
throw new Exception();
#>
If there are errors when the T4 is saved, they will be displayed, otherwise, a message displayed:
Error Running transformation: System.Exception: Exception of type 'System.Exception' was thrown.
Then comment out the exception when you're ready to actually generate the T4 output.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With