Run T4 text template programmatically

血红的双手。 提交于 2019-12-22 04:55:10

问题


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);
}

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.


回答1:


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.



来源:https://stackoverflow.com/questions/1846581/run-t4-text-template-programmatically

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!