Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop T4 from executing every time I switch to another tab?

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.

like image 840
Athari Avatar asked Oct 27 '10 15:10

Athari


5 Answers

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

#>
like image 153
James Close Avatar answered Nov 13 '22 23:11

James Close


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.

like image 41
GarethJ Avatar answered Nov 13 '22 23:11

GarethJ


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. :)

like image 22
Carlos Toledo Avatar answered Nov 13 '22 21:11

Carlos Toledo


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!!! 

...

like image 28
gmayer Avatar answered Nov 13 '22 23:11

gmayer


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.

like image 1
ejs Avatar answered Nov 13 '22 22:11

ejs