T4 Templates in MonoDevelop

A few week ago, I travelled to GDC and MIX. While in planes, airports and in spare moments in the conference, I implemented a feature I've wanted for some time - integrated T4 templating. This takes the T4 engine that I wrote for ASP.NET MVC templates, and exposed it within the IDE as a "custom tool", like Visual Studio does.

This also meant I had to implement a simple version of VS-style custom tools, or generators. Set the file's "custom tool" property to "TextTemplatingFileGenerator" using the property pad, then whenever you save it, MD will run the generator on the file.

Unlike VS, there is also a file template for the T4 file:
MonoDevelop New File dialog with T4 template

The T4 syntax is very like ASP.NET's render blocks, expressions and directives. Here you can see a simple example that generates a countdown:
T4 template in MonoDevelop generating a countdown

When this is saved, it generates the output file:
T4 output file in MonoDevelop

It's also integrated with MonoDevelop 2.4's new error bubbles, so any errors in T4 code show up in the editor as soon as the file is saved:
Error bubbles in T4 template in MonoDevelop

This is very much a side project for me, so contributions in this area would be very helpful. It would be great to have support for viewing generators' message output and cancelling long-running generators, support for T4 custom directives, debugging T4, code completion in T4 files, and generating template class files that can be compiled into apps.

This is part of the Catchup 2010 series of posts.

Comments

Where's the code for T4 template generation? I'm looking around to try to find a cross-platform T4 template generation solution (without a lot of dependencies). This post from Miguel last year mentions that there's a CLI tool for T4 template generation: http://tirania.org/blog/archive/2009/Mar-10.html

I'd love to help out! I recently added .pp support (that NuGet uses) to a command line tool I've been using for cross-platform .NET development and utilizing something like T4 templates would be ideal!

The T4 engine is Mono.TextTemplating.dll and the commandline tool is TextTransform.exe. You can find them in a MD installation or build them from source.

I'm a bit confused about the way T4 is integrated into the IDE. (I know your work is following the pattern in Visual Studio.)

I don't want an output file per T4 template. I want to write a single T4 template that is used to process a particular type of XML file, running the same way on each XML file. My T4 template will be a sort of IDL generator, and the XML will be the IDL. I see how I can load and access XML data from T4, but what I don't see is how to bind this together into the IDE. I want do have something like

+ MyLibrary
+ ClassGenerator.tt
+ ClassGenerator.XMLDTD
+ MyApplication
+ FooClassDef.XML (of type ClassGenerator)
+ FooClass_Generated.cs (generated by combining ClassGenerator.tt and FooClassDef.XML)

How can I achieve this?

That's not easily possible - you'd have to write a custom MD addin or a build task that would use the T4 to compile the template and use it to process your input files.