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

6

18

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.

Phospholipide answered 27/10, 2010 at 15:24 Comment(0)
J
7

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.

Jargonize answered 28/10, 2010 at 6:0 Comment(2)
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.Phospholipide
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.Jargonize
A
14

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

#>
Annihilator answered 19/10, 2011 at 13:4 Comment(0)
J
7

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.

Jargonize answered 28/10, 2010 at 6:0 Comment(2)
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.Phospholipide
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.Jargonize
T
5

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

<# throw new Exception(); #>

Because I throw an exception the process stops and when I finish all the work I just have to remove this line. It works.

Tertian answered 5/6, 2016 at 2:54 Comment(0)
T
2

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

...

Thiourea answered 15/7, 2014 at 18:56 Comment(0)
A
2

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.

Afforest answered 20/8, 2020 at 17:28 Comment(0)
S
-1

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.

Sholom answered 27/10, 2010 at 15:37 Comment(2)
The question is, where is this setting?Phospholipide
-1 This is incorrect information. When you switch tabs, the T4 template is executed automatically, even though the tab still has "*" in the name, indicating the file is not saved.Tanberg

© 2022 - 2024 — McMap. All rights reserved.