Elton Stoneman

  Home  |   Contact  |   Syndication    |   Login
  85 Posts | 0 Stories | 202 Comments | 0 Trackbacks

News

Archives

Post Categories

[Source: http://geekswithblogs.net/EltonStoneman]

I was looking for an MSBuild task which could execute a T4 template, replacing tags in the template with the value of properties or item groups from the build - giving your T4 templates access to all the runtime values used in the build scripts. So you start with a T4 template like this:

<#@ template language="C#" #>

<#@ output extension=".txt" #>

<#@ assembly name="System.dll" #>

***START

Property value: StartDir= $(StartDir)

Item Group value: TextFiles = @(TextFiles)

***END

 

- execute it through a call to ExecuteT4Template in your build target:

<PropertyGroup>

<StartDir>C:</StartDir>

</PropertyGroup>

 

<ItemGroup>

<TextFiles Include="$(BuildOutputDir)\*.txt"/>

</ItemGroup>

 

<Target Name="ExecuteTemplate">

<ExecuteT4Template TemplatePath="T4Sample.tt" OutputPath="c:\T4SampleOutput.txt"/>

</Target>

 

- and it produces the following output:

***START

Property value: StartDir= C:

Item Group value: TextFiles = \zeros.txt

***END


There wasn't one that I could find so I put a simple one together. A couple of fiddly things worth noting if you do something similar:

  1. the project's property values are not exposed to the executing task in an accessible way, so you have to reflect around BuildEngine to get hold of the executing project;
  2. if you don't want to write an ITextTemplatingEngineHost or rely on a GAX or other host, you'll end up shelling out to TextTransform.exe (as I have).

Full source for the task is here: ExecuteT4Template.cs (note this uses version 1.1 of the TextTransform tool).

A couple of things to note: this task will update all instances of property and item tags in the template - $(propertyName) or @(itemGroupName) (it's not clever enough to deal with %(itemGroupName.ValueName))- with the value from MSBuild, if the named property/item group exists in the project. Potentially you may want to leave certain tags in place in the template, in which case you'll need to ensure there are no matching properties in the build, or extend the task to take a list of names to ignore.

Also, if you're using a value in your script which is the output from another task, then the task which produces the output must be in a separate target which completes before the call to ExecuteT4Template. MSBuild seems only to store properties/item groups from task outputs when targets are completed, so if the output is created in the same target as the T4 task, the property won't be registered and the tag in the template won't be replaced.

I have a couple of other tasks which all relate to deploying BizTalk and ESB Service Providers, so I'll add them to the ESBSimpleSamples CodePlex project along with this one.

posted on Friday, July 25, 2008 1:54 PM

Feedback

# re: An MSBuild task to execute T4 templates 7/27/2008 3:04 PM Oleg Sych
Elton,

This is a great idea. Not only does it allow you to reference project properties and items, it also enables using T4 templates in Visual Studio projects that don't support custom tools. Database projects in Visual Studio DBPro Edition could greatly benefit from code generation but don't support custom tools.

Is it possible to access Visual Studio services, i.e. extensibility APIs from a custom MSBuild task when it is running in Visual Studio? I'm asking is because most of my templates rely on extensibility APIs to add output files to the project. How would that work when the template is running in MSBuild?

Oleg

# re: An MSBuild task to execute T4 templates 7/29/2008 2:32 PM Elton
Good question, Oleg. The APIs are all available to GAX actions, so running the template through the GAX host rather than the command line may make VS available to the templates. I'll have a look and post an update.

# re: An MSBuild task to execute T4 templates 10/4/2008 9:17 AM Elton
Note, the task has been updated now to run under MSBuild 2.0 and 3.5: http://geekswithblogs.net/EltonStoneman/archive/2008/10/04/executet4template-msbuild-task-updated.aspx

# re: An MSBuild task to execute T4 templates 12/7/2008 1:20 PM Landing Page Templates
This is indeed a great idea. I just love it.

# re: An MSBuild task to execute T4 templates 1/5/2009 9:56 PM ride on mowers
Thanks for this scripting tutorial.

# re: An MSBuild task to execute T4 templates 4/9/2009 3:17 PM jeremy
Your color scheme on the website makes it almost impossible to read your code snippets.

# re: An MSBuild task to execute T4 templates 4/9/2009 3:53 PM Elton
Fair point Jeremy, I was experimenting with a new skin but the classic version is easier on the eyes. Back to the way it was now.

# re: An MSBuild task to execute T4 templates 5/15/2009 8:16 PM Bowling Balls for Sale
Great work. It is exactly what i was looking for. it gets right to the poing. Excellent! Cheap Bowling Balls

# re: An MSBuild task to execute T4 templates 5/15/2009 8:19 PM Youth Baseball Bats For Sale
That is a really great work around to a bit of a sticky problem. And such a simple solution as well. Thank you very much for the information. Great work putting this together. Cheap Youth Baseball Bats

# re: An MSBuild task to execute T4 templates 6/26/2009 8:06 PM Ogre
Seems that I'm unable to call this task from a .targets file?

D:\Dev\MediaBrowser\OgreBranch\MediaBrowser.targets(235,9): error MSB4018: System.Exception: ProjectHelper: exception retrieving executing project: Object reference not set to an instance of an object.
D:\Dev\MediaBrowser\OgreBranch\MediaBrowser.targets(235,9): error MSB4018: at Sixeyed.MSBuild.Project.ProjectFactory.GetProject(ITask executingTask) in D:\Dev\MediaBrowser\OgreBranch\BuildTasks\Sixeyed.MSBuild\Project\ProjectFactory.cs:line 40

# re: An MSBuild task to execute T4 templates 8/16/2009 6:37 AM registry cleaners station
I didn't realize how simple the code for the template is. You did a good job with this.

# re: An MSBuild task to execute T4 templates 8/21/2009 3:13 AM folding chairs
I'm developing a new skin for my main site. I think I could get some use for your algorithm in mine. You are a very skilled programmer. Kudo.

# re: An MSBuild task to execute T4 templates 8/26/2009 3:00 AM kids loft beds
I agree with you. It was so easy to follow.

# re: An MSBuild task to execute T4 templates 8/26/2009 3:04 AM typy bukmacherskie
Such an helpful resource, thanks for that kind of info, now i can fix my bug ! Really great share !

I enjoy T4 templates !

# re: An MSBuild task to execute T4 templates 9/4/2009 4:46 AM Deresen
I'm using this very usefull msbuild task. But have one question.
I would like to use the properties inside my template, something like this:

<# var myProp = $(StartDir) #>
MyProperty: <#= myProp #>

Is this possible? Because I can't get it working.

# re: An MSBuild task to execute T4 templates 9/26/2009 2:51 AM external hard drive
Wow, If I only understood this code.

# re: An MSBuild task to execute T4 templates 10/20/2009 11:40 AM closet organizers sytems
The build target series of codes is what I'm missing before. I never thought it could be this simple.

# re: An MSBuild task to execute T4 templates 10/31/2009 5:29 AM Baby Modeling
Haha, this code is really sneaky - I love it. Thanks for the idea, I'm definitely going to try it out.

# re: An MSBuild task to execute T4 templates 11/10/2009 7:19 AM Toine
Nice job !
Here are my changes on TextTransform to bypass the x86/x64 and TextTemplating version problems:

private class TextTransform
{
static TextTransform()
{
string toolPath = Path.Combine(
Environment.GetEnvironmentVariable("CommonProgramFiles"),
@"Microsoft Shared\TextTemplating");

if (Directory.Exists(toolPath))
{
foreach (var directory in Directory.GetDirectories(toolPath, "1.*"))
{
ToolPath = directory;
}
}
}

public static readonly string ToolPath;
public const string ExeName = "TextTransform.exe";
public const string ArgumentFormat = " -out \"{0}\" \"{1}\"";
}

# re: An MSBuild task to execute T4 templates 11/11/2009 7:57 PM christmas presents
Thanks for the latest update.

# re: An MSBuild task to execute T4 templates 11/25/2009 12:59 AM rrod fix
Well well folks, I like these updates.

# re: An MSBuild task to execute T4 templates 11/30/2009 7:33 AM Buy High PR Links
Great post! an informative one, i think the idea shared here will enable us using T4 templates in Visual Studio projects.

# re: An MSBuild task to execute T4 templates 11/30/2009 7:46 AM Teeth Whitening Products
Nice post! I enjoyed reading all the information shared here.

# re: An MSBuild task to execute T4 templates 11/30/2009 7:59 AM Best Acne Treatments
Great updates man! nice to have places like this, learned a lot from it.

# re: An MSBuild task to execute T4 templates 12/31/2009 6:01 AM Dave
Thanks for updates!!

# re: An MSBuild task to execute T4 templates 1/11/2010 5:31 AM hilarious quotes
this templated saved my 14 hrs, thx. I appreciate your hard work and share with us.

# re: An MSBuild task to execute T4 templates 1/11/2010 5:33 AM dental implants
you are a gem to share this with us. Thank you

# re: An MSBuild task to execute T4 templates 1/15/2010 11:52 AM Seo Articles
Thanks a ton. I've spent the last 30 mins searching for the exact same thing and i stumbled upon this site by luck.

# re: An MSBuild task to execute T4 templates 1/15/2010 6:59 PM Freed
Excellent updade.
I have been looking for something like this untill i found this.
Thankyou

Bookmarked.

# re: An MSBuild task to execute T4 templates 1/16/2010 12:28 AM baby sweaters
Thanks for the coding, Also, if you're using a value in your script which is the output from another task, then the task which produces the output must be in a separate target which completes before the call to ExecuteT4Template.

# re: An MSBuild task to execute T4 templates 1/17/2010 1:33 PM Hospedagem
I am delighted to find this MSBuild for executing T4 templates.
I think this code will be helpful as indicated earlier:
<# var myProp = $(StartDir) #>
MyProperty: <#= myProp #>

Thanks for sharing.

# re: An MSBuild task to execute T4 templates 1/19/2010 5:24 PM acai berry scams
Great article fellow nerds.

# Good post 1/20/2010 1:14 AM affordablehosting
Thanks fro sharing.

# Free Guide 1/20/2010 10:42 AM Yasin
I found good post here , inspiring post here ....

# re: An MSBuild task to execute T4 templates 1/20/2010 10:44 AM Free Guide
Here is the interesting post , thank you for posting....

# re: An MSBuild task to execute T4 templates 1/20/2010 11:35 AM Anna Thucker
This post is mainly a developer/website related..


Forex Bonzen

# re: An MSBuild task to execute T4 templates 1/21/2010 9:01 AM wordpress theme
Very helpful .I have boomared

# re: An MSBuild task to execute T4 templates 1/22/2010 12:13 PM Fix rrod
Thanks for this script it will be very helpful.

# re: An MSBuild task to execute T4 templates 1/23/2010 10:05 AM viswiss
i think this information is really awesome and i will definitely gonna use it. thanks for sharing such a nice list

# re: An MSBuild task to execute T4 templates 1/24/2010 8:35 AM Learn Forex
Great post! Have been looking for it!

# re: An MSBuild task to execute T4 templates 1/24/2010 8:36 AM Latest in electronics
Very detailed programming! Thumbs up!

# re: An MSBuild task to execute T4 templates 1/25/2010 3:01 AM Firma Budowlana
Haha, this code is really sneaky - I love it. Thanks for the idea, I'm definitely going to try it out.

# re: An MSBuild task to execute T4 templates 1/25/2010 12:23 PM pellet stove insert
Thanks for this code. It will be very helpful!

# re: An MSBuild task to execute T4 templates 1/25/2010 6:05 PM Kompetisi website kompas muda
Well well folks, I like these updates.

# re: An MSBuild task to execute T4 templates 1/26/2010 4:33 AM Colon cleanser
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>

# re: An MSBuild task to execute T4 templates 1/26/2010 12:04 PM Lifestyle on The Net
Simple coding for design but very useful :)

# re: An MSBuild task to execute T4 templates 1/26/2010 8:06 PM nick
thanks for the code very interesting!

# re: An MSBuild task to execute T4 templates 1/27/2010 10:10 AM helen
Very helpful resource at all. Our team want to know more things like that.You are doing very dormant job as well.
Thanks !

# re: An MSBuild task to execute T4 templates 1/28/2010 5:41 AM hosting
thank you for this article. Ive looked at the end.
hosting

# vps 1/28/2010 6:35 AM sunucu
www.turkiyeknightonline.com
www.turkiyereseller.com
thank you for this article. Ive looked at the end.

# re: An MSBuild task to execute T4 templates 1/28/2010 2:33 PM Nardy Online
Thank youuu! This is what I was searching for!


# re: An MSBuild task to execute T4 templates 1/28/2010 5:05 PM Jon Sims
This is a great article, and oddly enough, it reminds me of this sort of unrelated site about Car Warranty Reviews. Now here, the big issue is whether or not the company is pulling some sort of Auto Warranty Scam. The article you have above seems to walk the line as well - are the people involved totally committed to what they are talking about?

# re: An MSBuild task to execute T4 templates 1/29/2010 3:51 AM GPS tracker
Thanks for such a great informative article,your templates are really nice..

# re: An MSBuild task to execute T4 templates 1/29/2010 4:39 AM United States
Thanks For the code and it is getting worked without any bugs and so

# re: An MSBuild task to execute T4 templates 1/30/2010 8:36 AM traghetti Albania
Very good post. It really helped me a lot, will be referring a lot of friends about this. I keep seeing articles like these.


# re: An MSBuild task to execute T4 templates 1/30/2010 10:10 AM Fantasia
Great updates man! nice to have places like this, learned a lot from it.

# re: An MSBuild task to execute T4 templates 1/30/2010 5:22 PM Brazil
These post will bring the rapid content and processind the idea to one anather

# re: An MSBuild task to execute T4 templates 1/31/2010 5:04 AM Closet Doors
Yes. Very Great in article. I accept Code.

Closet Doors

# re: An MSBuild task to execute T4 templates 1/31/2010 5:43 AM Pest Control Las Vegas
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:

# re: An MSBuild task to execute T4 templates 1/31/2010 4:05 PM Chicago
Updated information here , nice sharing of post making lot of sense to me

# re: An MSBuild task to execute T4 templates 2/1/2010 1:52 PM Antarctica
I need the t4 templates , while iam in search of that i got this good post and it is helpfull

# re: An MSBuild task to execute T4 templates 2/1/2010 2:09 PM London ontario real estate
That was a fascinating read! I always wanted to learn more about T4 templates, so these insights were very informative. I'll be sure to test it and leave you some comments!

Best regards,
Sara, London ontario real estate

# re: An MSBuild task to execute T4 templates 2/2/2010 12:43 PM Juice Fountain
Very Clear !!!!!! Thank you

# re: An MSBuild task to execute T4 templates 2/3/2010 5:16 PM Phoenix, Arizona
Well, T4 is a code generator built right into Visual Studio. To be clear, you HAVE THIS NOW on your system…go play. Now's the time to introduce code generation to your company. If you're doing something twice or more, manually, in your company, generate it.

Phoenix, Arizona
Phoenix


However, it's not deep-deep built in, because there's no item templates in File | New Item and there's no intellisense or syntax highlighting.

You don't need this, but if you want really get the most out of T4, first, head over to Clarius Consulting and get their "T4 Editor Community Edition." That'll get you some basic coloring. They have a pay version that gets you more if you want.

Now, go into Visual Studio and make a Console App (or any app) and add a Text File, but name it something with a .tt extension. You'll get a warning since it's a generator, that someone could generate evil. Click OK if you are cool with potential evil. ;)

# re: An MSBuild task to execute T4 templates 2/4/2010 4:39 AM Best Wrinkle Cream
I never knew T4 template authoring can be so easy. Thanks for this very informative guide.

# re: An MSBuild task to execute T4 templates 2/4/2010 11:45 AM Life In Florida
T4 templetes are going well now a days , so the people are interested to know about this one , any how thanks for posting...

# re: An MSBuild task to execute T4 templates 2/4/2010 10:32 PM Tunsori
I learn mor about T4 templets with this article tunsori

# re: An MSBuild task to execute T4 templates 2/5/2010 10:21 PM Learn Spanish in Mexico
Very detailed programming, Thank youuu! This is what I was searching for!

# re: An MSBuild task to execute T4 templates 2/6/2010 12:23 AM drugzer
You don't need this, but if you want really get the most out of T4, first, head over to Clarius Consulting and get their "T4 Editor Community Edition."
<p>stock market for beginners</p>

# latest news update 2/6/2010 4:03 AM India News
Thanks for sharing with us Great post !india news for latest news update

# re: An MSBuild task to execute T4 templates 2/6/2010 8:43 PM Expat Social Network
That was a fascinating read! I always wanted to learn more about T4 templates, so these insights were very informative. I'll be sure to test it and leave you some comments!


<p>Expat Social Network</p>


# re: An MSBuild task to execute T4 templates 2/7/2010 7:56 AM Straight Razor
Thanks for this MSBuild task tutorial. People who are not very good at coding, like me, will find this tutorial very helpful

# re: An MSBuild task to execute T4 templates 2/7/2010 9:11 PM steve
I wanted to avoid additional assembly references. Instead, the following custom MSBuild target was created to address this.
<p>roof tile cleaning/a></p>

# re: An MSBuild task to execute T4 templates 2/7/2010 10:29 PM Expat Social Network
That was a fascinating read! I always wanted to learn more about T4 templates, so these insights were very informative. I'll be sure to test it and leave you some comments!

Expat Social Network


# re: An MSBuild task to execute T4 templates 2/8/2010 12:56 AM Freeware Data Recovery
Thanks for this script it will be very helpful. keep posting

# re: An MSBuild task to execute T4 templates 2/8/2010 8:53 AM somchai jobs
All codes above are very useful, Thank you for sharing. หางาน

# re: An MSBuild task to execute T4 templates 2/8/2010 10:37 AM jumakhan
you want really get the most out of T4, first, head over to Clarius Consulting and get their "T4 Editor Community Edition." That'll get you some basic coloring.
<p>Free Online Games</p>


# re: An MSBuild task to execute T4 templates 2/8/2010 11:04 AM datingsites
Thank you for sharing this with the rest of the world

# re: An MSBuild task to execute T4 templates 2/8/2010 3:03 PM Life In India
Thanks for usefull script , making good sense about the process and all , thanks for posting...

# re: An MSBuild task to execute T4 templates 2/8/2010 8:34 PM gunda
we create a new item called T4Templates that will recurse the project and hold information for each text template that is found.
<p>Houston Limousine</p>


# re: An MSBuild task to execute T4 templates 2/8/2010 10:56 PM Ghazanfar
nice info about ms build template i found here .
thanks a lot for this nice info
Download software games and movies

# re: An MSBuild task to execute T4 templates 2/8/2010 10:57 PM Ghazanfar
about mi found here .due to excessive knowledge iam sure it is googd info
thanks a lot for this nice info
Download software games and movies

# # re: An MSBuild task to execute T4 templates 2/9/2010 10:01 AM Timmy Hugend
Great post! Thanks for sharing!
Lenovo Computers
Lenovo Australia


# re: An MSBuild task to execute T4 templates 2/9/2010 10:11 AM Felicity Merriman American Girl
yes thats right 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.Thanks

# re: An MSBuild task to execute T4 templates 2/9/2010 12:25 PM king
I always wanted to learn more about T4 templates, so these insights were very informative. I'll be sure to test it and leave you some comments!
<p>Free of cost Download</p>

# re: An MSBuild task to execute T4 templates 2/9/2010 1:48 PM gang
you want really get the most out of T4, first, head over to Clarius Consulting and get their "T4 Editor Community Edition." That'll get you some basic coloring.<p>Modern Furniture</p>


# re: An MSBuild task to execute T4 templates 2/9/2010 2:26 PM ganay
I always wanted to learn more about T4 templates, so these insights were very informative. I'll be sure to test it and leave you some comments!
<p>br domains</p>


# re: An MSBuild task to execute T4 templates 2/9/2010 4:44 PM 300MB Movies Blast
Thanks for the coding, Also, if you're using a value in your script which is the output from another task, then the task which produces the output must be in a separate target which completes before the call to ExecuteT4Template.

Postal Gold


# re: An MSBuild task to execute T4 templates 2/9/2010 9:34 PM babu
It really helped me a lot, will be referring a lot of friends about this. I keep seeing articles like these.
<p>Quit Smoking</p>

# re: An MSBuild task to execute T4 templates 2/10/2010 12:05 AM Tech3D
T4 templetes are going well now a days , so the people are interested to know about this one , any how thanks for posting...

<p>mens swimwear</p>

# re: An MSBuild task to execute T4 templates 2/10/2010 12:08 AM Tech3D
Thanks For the code and it is getting worked without any bugs and so

marriage affair

# re: An MSBuild task to execute T4 templates 2/10/2010 2:31 AM exzant
I need more time to understand this code, but i'll never give up. Thanks.
exzant

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: