Wednesday, February 9, 2011

Executing a T4 Template from the command line

A T4 Template is normally added to a project and this gets executed whenever the user adds the template to the project / whenever a user saves the text template. Now i had a requirement when i needed to execute the T4 template programmatically. I had the following options:

Options to execute a T4 text template programmatically:

  • Implement the ITextTemplating interface.
  • Use the TextTransform command line utility.

I opted for the 2nd one as it was simple and solved my problem in a sec. We will look at first calling it from the command line and then calling this utility from .net code.programmatically.

From the Command Line:

TextTransform.exe is located In the “\Program Files\Common Files\Microsoft Shared\TextTemplating\10.0” directory. 

I had a text template located at “D:\T4\test.tt”.

Now i fired up the command line using

Run—> cmd

then changed the directory to the TextTransform exe directory using

cd D:\Program Files\Common Files\Microsoft Shared\TextTemplating\10.0

then executed the tt using the following command

TextTransform D:\T4\Test.tt

image

 

 

And voila the output of the transform, in my case Test.cs appeared at D:\T4\

References

http://msdn.microsoft.com/en-us/library/bb126245.aspx

No comments:

Post a Comment