每次切换到另一个选项卡时如何阻止 T4 执行?

发布于 2024-09-29 13:45:59 字数 116 浏览 13 评论 0原文

当我编辑T4时,每次切换到另一个文件时都会执行该脚本。对于快速简单的脚本来说是可以的,但是有些脚本需要很长时间才能执行。有没有办法禁用这种行为?我希望脚本仅在保存 T4 文件或从菜单中手动选择“运行自定义工具”时运行。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

紫罗兰の梦幻 2024-10-06 13:45:59

我有完全相同的问题。我按照本文中的步骤 http://msdn.microsoft.com/en- us/library/ee789839.aspx 关于将模板拆分到另一个项目并共享输出文件。

它详细介绍了如何通过右键单击模板并清除 CustomTool 属性来关闭附加到模板的 TextTemplatedFileGenerator 工具。这会在保存时停止模板生成代码...但在切换选项卡时它仍然运行!

我认为解决这个问题的唯一方法是将所有模板代码移动到具有不同后缀(例如 ttinclude 或 t4 等)的新文件中,然后使用 include 指令将此文件包含在实际的 T4 模板文件中。这样,您将永远不需要打开该文件来编辑模板,因此它不会意外运行。

因此,在一个名为 MyTemplate.tt 的文件中:

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

而在另一个名为 MyTemplateCodeBehind.t4 的文件中:

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

#>

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

#>
过期以后 2024-10-06 13:45:59

T4 连接到自定义工具机制 (IVsSingleFileGenerator)在 C#/VB 项目系统中,这使其能够在保存时运行、运行自定义工具菜单以及在选项卡切换行为上运行 - 所有这些都是以实现简单界面为代价的。

不幸的是,这意味着 T4 基本上也无法控制这些行为,而这些行为是自定义工具的标准。

另一种方法可能是使用 VS 建模和可视化 SDK 中的 T4 MsBuild 支持在构建时执行 T4,然后禁用自定义工具。我将询问构建 msbuild 支持的同事是否使用自定义工具来识别模板集并回发到线程。

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.

笛声青案梦长安 2024-10-06 13:45:59

我正在做的(可能是一个糟糕的解决方案)是在 tt 文件的开头编写一个异常行,例如:

<# throw new Exception(); #>

因为我抛出异常,所以进程停止,当我完成所有工作时,我只需删除此行。
有用。

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.

半城柳色半声笛 2024-10-06 13:45:59

尝试在编译指令之后添加一个返回退出方法

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

...

青衫负雪 2024-10-06 13:45:59

我发现在开发 T4 模板时在 T4 文件顶部使用以下代码片段很有用:

<# //throw exception to halt execution during development
    throw new Exception();
#>

如果保存 T4 时出现错误,它们将被显示,否则将显示一条消息:

运行错误conversion: System.Exception: 抛出了“System.Exception”类型的异常。

然后,当您准备好实际生成 T4 输出时,注释掉该异常。

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.

夜血缘 2024-10-06 13:45:59

保存文件时执行 T4 模板。如果您将 VS 设置为在您离开文件时自动保存,则可以解释该行为。检查您的 VS 配置以确定当您按 Tab 键离开时 VS 是否正在保存文件。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文