in

Platinum Bay

Peace, Love, and Visual Studio ALM

This Blog

Syndication


.NETicated

Running T4 Templates with MSBuild

If you haven’t explored T4 yet, you’re missing one of Visual Studio’s best, and best kept, secrets. T4, which stands for Text Templating Transformation Toolkit, is code generation built right into Visual Studio 2008. It is also available in Visual Studio 2005 if you install the Visual Studio SDK. It is a great feature that I use quite frequently to automate repetitive tasks.

T4 template execution only occurs though when the template file is open in the editor and saved. In other words, template execution does not occur when you type Ctrl+B to build the project. There are scenarios however where execution of the templates at every build is the desired behavior.

I did find several custom MSBuild tasks to perform this behavior, but I wanted to avoid additional assembly references. Instead, the following custom MSBuild target was created to address this.

First and foremost, a .targets file is created in the MSBuild directory (%systemdrive%:\Windows\Microsoft.NET\Framework\v3.5):

<?xml version="1.0" encoding="utf-8"?>
<
Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<
Target Name="ExecuteT4Templates">
<
ItemGroup>
<
T4Templates Include=".\**\*.tt" />
</
ItemGroup>
<Exec
WorkingDirectory="C:\Program Files\Common Files\microsoft shared\TextTemplating\1.2\"
Command="TextTransform &quot;%(T4Templates.FullPath)&quot;" />
</
Target>
</
Project>

There are a couple important things to note. First, a custom target is created, and an item group is added to the target. In the item group, we create a new item called T4Templates that will recurse the project and hold information for each text template that is found. Finally, an execute task is added which calls TextTransform.exe for each file found.

Once the targets file is create, the project file is edited to import the new targets file using Import. I put it above the first PropertyGroup node so it’s visible when future maintenance is performed, but it can be placed anywhere within the root Project node:

<Import Project="$(MSBuildToolsPath)\T4Execution.targets" />

Finally, the DefaultTargets attribute in the Project node is modified to include the ExecuteT4Templates target: Note: This change is required for any project where this behavior is desired.

DefaultTargets="ExecuteT4Templates;Build"
Now whenever the project is built (Ctrl+B), template execution occurs. To be fair, there are several issues with the above approach, including:
  • Template execution does not occur when Debug (F5) is triggered unless a project file or property has changed.
  • There is no built-in way to get the number of items in an ItemGroup to use conditional execution.

How important is this functionality in your projects? How would you do it differently?

Comments

August 28, 2009 4:14 AM

Hi Steve,

There are two things you can do to address the issues you've listed:

1. You could put the logic in the BeforeBuild target in the project file, this way it will always be executed even if nothing has changed (I think).

If you still want to re-use the target from a central file you could re-define the BeforeBuild target as:

<Target Name="BeforeBuild" DependsOnTargets="ExecuteT4Templates" />

2. You can determine if an ItemGroup contains any elements or not by checking if it's an empty string:

<Exec ... Condition=" '@(T4Templates)' != '' " />

 

August 28, 2009 4:25 AM

Pingback from  Twitted by ajlopez

 

August 28, 2009 6:38 AM

Pingback from  Dew Drop &#8211; August 28, 2009 | Alvin Ashcraft&#039;s Morning Dew

 

August 29, 2009 4:59 AM

Pingback from  Twitter Trackbacks for                 Running T4 Templates with MSBuild - .NETicated         [platinumbay.com]        on Topsy.com

 

August 29, 2009 5:49 PM

Pingback from  Havadis &#8211; 1 | Bulutlararas??

 

November 15, 2009 11:37 AM

This won't work under 64bit without changing your page or using some kind of path or envirionment variable. The TT working folder is C:\Program Files (x86)\Common Files\microsoft shared\TextTemplating\1.2

 

March 8, 2010 9:06 AM

Any ideas on coping with the files being generated being checked in under tfs?

Ed Swindell
 

May 20, 2010 7:18 PM

Pingback from  Patriot Lighting Service Disabled Veteran Owned Small Business, Jeep Patriot Sales

 

May 20, 2010 9:02 PM

Pingback from  Samurai Jack Movie One, Buy Samurai X

 

May 20, 2010 10:42 PM

Pingback from  Chevrolet C10 Oem, C10 Region

 

May 21, 2010 5:00 AM

Pingback from  Clk430 Cel, Clk430 Parts Replacement Headlight Assembly 1999 Mercedes Benz Shock Absorber

 

May 21, 2010 5:38 AM

Pingback from  Casio 1500t Review Paw1500t 7v, 1987 W150 Dodge Truck

 

May 21, 2010 10:34 AM

Pingback from  Omni Careers, Omnifax L621 Toner

 

May 22, 2010 3:53 PM

Pingback from  Ebay Odyssey Butter, Odyssey Irons Golf Balls

 

May 22, 2010 4:33 PM

Pingback from  1986 Oldsmobile Toronado Parts, Buy Toronado For Rent Oldsmobile Bravada

 

May 22, 2010 8:40 PM

Pingback from  300sdl Browse Our 1987 Mercedes, 1987 Mercedes 300sdl Fuel Economy

 

May 23, 2010 7:04 AM

Pingback from  Lw200 Movie Watch, 02 Saturn Lw200 Traction Control - 192.mfbattle.com

 

May 23, 2010 9:04 AM

Pingback from  1996 Bronco Soft Top Classic Roadsters, E 250 Super Duty Aftermarket Crown Victoria Ford Car Bronco Ii - 340.tgrconversions.com

 

May 23, 2010 12:48 PM

Pingback from  Toyota C35 C3500 Pickup Used Yukon Xl, Gmc P3500 Sale - 460.mfbattle.com

 

May 24, 2010 1:51 PM

Pingback from  Metroplex Supreme Like Omega, Light Bulb For Omega B 22 - 18.tgrconversions.com

 

May 25, 2010 9:50 PM

Pingback from  142 Bulb Headlight Volvo S70, 1998 Volvo S70 Parts - 89.rkwrh.com

 

Leave a Comment

(required )  
(optional )
(required )  
Add

About Steve

Steve Andrews is an independent consultant, INETA speaker, and Microsoft MVP for Visual Studio ALM. He has been working in technology for over ten years focusing on custom application development and Application Lifecycle Management. Steve is also Microsoft and IBM certified and a community fanatic having led sessions at nearly 100 events across North America. When he's not developing software solutions or engaging with the community about software technology, Steve is a closet singer and songwriter and plays the guitar and keys. Occasionally, Steve even gets to sleep. Occasionally.
Powered by Community Server (Commercial Edition), by Telligent Systems
© Platinum Bay | Some Rights Reserved Creative Commons License

Disclaimer: The information in this weblog is provided "AS IS" with no warranties, and confers no rights. This weblog does not represent the thoughts, intentions, plans or strategies of my employer. It is solely my opinion. Feel free to challenge me, disagree with me, or tell me I'm completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or annonymous comments) - so keep it polite, please.