Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run T4 text template programmatically

Tags:

c#

.net

t4

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.

like image 952
Alex Avatar asked Dec 04 '09 12:12

Alex


1 Answers

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.

like image 177
Oleg Sych Avatar answered Sep 16 '22 13:09

Oleg Sych