以编程方式运行 T4 文本模板 [英] Run T4 text template programmatically

查看:27
本文介绍了以编程方式运行 T4 文本模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法从代码中以编程方式运行 T4 文本模板?我正在制作自定义域特定语言,并且我希望每次用户保存时运行相关的文本模板.目前,这就是我在 DSL 模型中所做的:

Is there a way to programmatically run T4 text templates from code? I'm making a custom domain specific language and I would like the related text templates to run every time the user saves. Currently, this is what I do in the DSL model:

protected override void OnDocumentSaved(EventArgs e)
{
    IVsCommandWindow commandWindow = (IVsCommandWindow)this.ServiceProvider.GetService(typeof(IVsCommandWindow));
    if (commandWindow != null)
    {
        commandWindow.ExecuteCommand("TextTransformation.TransformAllTemplates");
    }
    base.OnDocumentSaved(e);
}

这有效,但它有一个非常烦人的副作用.如果项目有多个 DSL 文档,每个文档都有相关的文本模板,它们将全部运行,而不仅仅是受给定 DSL 文档更改影响的那些.这可能看起来没什么大不了的,但它会导致源代码控制检查所有生成的文件,如果您有很多文档,转换实际上可能需要很长时间.感谢您的帮助.

This works, but it has a really annoying side-effect. If the project has multiple DSL-documents, each with their related text templates, they will all be run, not just the ones that are affected by changes to the given DSL-document. This may not seem like such a big deal, but it causes source control to check out all the generated files, and if you have a lot of the documents, the transformation might actually take quite a while. Thanks for any help.

推荐答案

Jean-Mark Prieur 来自DSL 团队在 DSL 工具实验室的第 4 部分中解释了如何使用自定义工具执行此操作.您也可以使用 直接从 DSL 模型执行此操作ITextTemplating 服务.在此处详细了解模板转换的工作原理.

Jean-Mark Prieur from the DSL team explains how to do this with a custom tool in Part 4 of the DSL Tools Lab. You can also do this directly from the DSL model using ITextTemplating service. More on how template transformation works here.

这篇关于以编程方式运行 T4 文本模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆