tags:

views:

428

answers:

2

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  A: 

See Can I use T4 programmatically from C# ?

Dan Blanchard
I'm using Visual Studio 2008, not Visual Studio 2010 and besides, I'm not trying to convert my .tt files to executable classes, I'm trying to run what is called the "TextTemplatingFileGenerator" (which is the Custom Tool on .tt files) only on files related to my DSL document. Thanks for your answer, but it's not the one I'm looking for.
Alex
+2  A: 

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.

Oleg Sych
I don't have time to try this out right now, but from skimming that document it looks very promising, and as soon as I've tested, I'll mark your answer as accepted if it works. It would seem a custom tool is the way to go.
Alex