How To Execute T4 Text Templates In MonoDevelop

This is now obsolete. MonoDevelop 2.4 now has T4 Templates integrated. SUH-WEEEET!!!

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.

  1. Make sure you have the Text Templating add-in installed
    • Open the Add-in Manager
      MonoDevelop: Tools -> Add-in Manager

      MonoDevelop: Tools -> Add-in Manager

    • Look for the Text Templating Add-in
      MonoDevelop: Add-in Manager

      MonoDevelop: Add-in Manager

      If its not there, click the “Install Add-ins” button and install it.
  2. Create a bash script to execute TextTransform.exe with arguments
    • Write the script
      cat > mono-t4
      x=$(mono /PATH/TO/monodevelop/AddIns/MonoDevelop.TextTemplating/TextTransform.exe -o $1 $2)
      echo $x
      Be sure to adjust the path to your MonoDevelop installation directory.
    • Move the script
      mv mono-t4 /usr/local/bin
    • Make the script executable
      sudo chmod +x /usr/local/bin/mono-t4
  3. Add a menu command to MonoDevelop’s Tools menu
    • Open Preferences
      MonoDevelop: Edit -> Preferences

      MonoDevelop: Edit -> Preferences

    • Add the Execute T4 command to External Tools
      MonoDevelop: Execute T4 Command Settings

      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 now, for a demonstration, we have the infamous “Hello World”.

  1. Add a new text file to your project
    MonoDevelop: Add New Text File

    MonoDevelop: Add New Text File

  2. Enter the following template code and save it
    <#@ template language="C#" #>
    <#@ output extension="txt" #>
    Hello <# Write("World!"); #>
  3. Run the Execute T4 command
    MonoDevelop: Tools -> Execute T4

    MonoDevelop: Tools -> Execute T4

  4. Check out your newly generated file. (It should be [template].txt in your project directory.)

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 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.

<>< Peace ><>

2 Responses to How To Execute T4 Text Templates In MonoDevelop

  1. Thanks for blogging this! The MD T4 engine and addin have stirred up a fair bit of interest, and it’s great to see people using it and making it easier for others to get started.

    I’d love to have more time to spend on the T4 addin myself, but I’ve been hugely busy with other things, though there was a brief discussion on the MD mailing list a couple of weeks ago where I detailed how to progress in properly integrating T4 into MD. You’re more than welcome to contribute your T4Toolbox equivalent (and anything else) to MD directly, so that we can get it out to all our users. Keep us posted so we don’t duplicate efforts 🙂

    • Dan says:

      Hey, thanks for stopping by. I’m pretty sure I got the idea from one of your answers on the mailing list that ended up on a discussion forum somewhere or something like that. I really enjoy working with MD. Unfortunately, I have to work for a living, so I don’t get to spend as much time on it as I’d like to. But, I just subscribed to the mailing list, so you’ll be hearing from me.