MonoDevelop 2.2 supports T4 templates. T4 templates are code templates used to generate any text based output, usually C#, SQL, or some other type of code. Basically it enables developers to write a single template to generate unlimited amounts of code such as CRUD stored procedures, data access classes, or any other type of repetitive code whose structure lends itself to being generated resulting in fewer late nights.

Visual Studio 2008 (and 2005 w/addin) have built in menu commands supporting code generation with T4 templates. However, MonoDevelop 2.2 requires a very small amount of setup. Fortunately, new menu commands are easily added to MonoDevelop’s interface. These instructions should work for any Linux distro. Something similar should also work on Windows using a batch script, but I don’t use Windows except at work, so I haven’t tried it. I have never used a Mac, but if there is a command line, then it can probably be done there as well. I got the idea here. Here is how to do it.

Be sure to adjust the path to your MonoDevelop installation directory.

Move the script

mv mono-t4 /usr/local/bin

Make the script executable

sudochmod +x /usr/local/bin/mono-t4

Add a menu command to MonoDevelop’s Tools menu

Open Preferences

MonoDevelop: Edit -> Preferences

Add the Execute T4 command to External Tools

MonoDevelop: Execute T4 Command Settings

Click the “+Add” button

Title: Execute T4
whatever you want the menu option to say

Command: /usr/local/bin/mono-t4
the script we created above

Arguments: ${itemPath} ${itemPath}

Check “Use output window”

The arguments need some explanation. Our script accepts two arguments which it passes on to TextTransform.exe. The first is the output file and the second is the input file. Our command configuration uses the same file for both which will cause our template file to be overwritten by its output. Sounds crazy, I know. However, we can change the file extension of our output using the output directive in our template. So, if we are writing a template that outputs SQL code, we can configure the extension of our output file to be .sql and we can configure the output extension of another template to be .cs and so on. This is demonstrated below. If you do forget to include the output directive in your template, MonoDevelop will overwrite the template file on disk, but will notify you that the contents of the file changed and ask you if you wish to reload it. If this happens, just say no and add the output directive to your template, save it, and execute it again.

FYI: TextTemplate.exe accepts a few additional arguments which could be included in our script and added to the arguments list of our command. I just decided to keep it simple for this example. To get a list of all of the options available, enter

TextTransform.exe --help

at the console. If you are any good with bash scripts and come up with something useful, please feel free to post a comment and share it.

And that’s all there is to it. To learn how to use T4 templates, MSDN has some resources under “Generating Artifacts By Using Text Templates”. Be sure to check out http://www.olegsych.com/ as well. Its written for Visual Studio, but much of it is useful in MonoDevelop as well. I am planning to develop something similar to his T4Toolbox for MonoDevelop as soon as I get the time. When I do, I’ll post it along with examples.

I hope you found this useful. Please make me aware of any necessary improvements or corrections you may find. Thank you.