Geeks With Blogs

News
Elton Stoneman (@EltonStoneman) IT Consultant, integration specialist, @Microsoft MVP and @Pluralsight author.

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

As Cory pointed out, my original task to execute T4 templates under MSBuild (replacing property markers in the T4 script with evaluated values from the build, see: An MSBuild task to execute T4 templates), fails under MSBuild 3.5. This is down to a change in the object model in the framework. All the useful classes are internal and all the useful properties are private, so they have to be reflected over - which makes my code brittle.

I've updated the task as part of a set here: Sixeyed MSBuild tasks, and it now works with either MSBuild 2.0 or 3.5.

Another update will be coming soon, to allow you to use named item group values, as well as property values and whole item groups in the script. A script can be marked with %(Collection.Identity) and when the task is called iteratively over a collection, the script will be populated with the current collection value each time. I'm also planning to change it to use the T4 engine directly rather than invoking the TextTransform console app, to hopefully speed up the execution time.

I'm using this task increasingly in builds now. It's a lot neater than having multiple XML or Regex file updates, and the template can be used to isolate logic so the builds stay clean. Currently it's used to dynamically generate Wix scripts, generate BizTalk bindings file for a set of environments, generate documentation projects for Sandcastle Help File Builder and produce config files for running DevPartner code coverage. Some of those templates may be generically useful, so I'll add them to MSDN when I get to the next update.

Posted on Saturday, October 4, 2008 9:14 AM Scripting & Automation , CodeGen , MSBuild | Back to top


Comments on this post: ExecuteT4Template MSBuild task: Updated

# re: ExecuteT4Template MSBuild task: Updated
Requesting Gravatar...
Great! Your sample is very useful and instructive, but I've found one weak point...

I've been playing with ExecuteT4Template and the sample project builds "successfuly" even in case of TextTransform.exe errors.

At the minimum you can make
ProcessHelper.Run return int:
-> return process.ExitCode
and then
ExecuteT4Template.Execute
-> if (0 == ProcessHelper.Run...
success = true;

Then build report 'FAILED' in case of transformation errors.
It would be useful to get those by intercepting OutStream messages and return them to build engine to show up ;-)

Best regards
Left by Woj on Oct 15, 2008 8:27 AM

# re: ExecuteT4Template MSBuild task: Updated
Requesting Gravatar...
Hi Woj, glad you've found it useful.

You're right though - when the template engine fails and you get a text file that just says "ErrorGeneratingOutput", the task does not return the failure to MSBuild.

Thanks for your suggestion - I'm looking at removing the use of TextTemplate.exe altogether though, and running the engine direct. That should be quicker and will let me log errors and warnings to the build, and return the correct status.

When I do the next update I'll let you know - I'm also planning to move it to the MSDN Code Gallery and post some proper sample templates.

Elton.
Left by Elton on Oct 15, 2008 1:02 PM

# re: ExecuteT4Template MSBuild task: Updated
Requesting Gravatar...
Firstly thx for the code just waht I was looking for.

I have found a weird error, I build and run the examples from Sixeyed MSBuild tasks link above, but when I try and incorporate the task in my own project I run into null object references trying to resolve the parent project.

I ran the code in debug and discovered the reason is that the contexts dictionary line ProjectFactory line 35 returns a single entry

[1, xxxxxx] not [0,xxxxx]

I have changed my code to obtain the one value in the dict and all seems well so far ...

You can probably shed some more light on this.
Left by tricky on Mar 25, 2009 6:01 PM

# re: ExecuteT4Template MSBuild task: Updated
Requesting Gravatar...
The path "C:\Program Files\Common Files\Microsoft Shared\TextTemplating\1.1" inside the task is valid only for x86. On an x64 the correct path is "C:\Program Files (x86)\Common Files\Microsoft Shared\TextTemplating\1.1".
Moreover "1.1" is for VS08 RTM; VS08 SP1 has "1.2".
HTH
Left by Giulio Vian on Apr 01, 2009 10:41 AM

# re: ExecuteT4Template MSBuild task: Updated
Requesting Gravatar...
Thanks Giulio - the task defaults the path as you say, but you can override it in your MSBuild script by specifying the ToolPath property.
Left by Elton on Apr 01, 2009 10:57 AM

# re: ExecuteT4Template MSBuild task: Updated
Requesting Gravatar...
I've found the same problem that tricky had. In my case instead of having 0 or 1 I had 38.
I've a target that is something like:
<Target ...>
<ExecuteT4Template...>
<...>
</Target>

if I change it to
<Target ...>
<ExecuteT4Template...>
<...>
</Target>

the key number becomes 39.

The other issue I found is that assemblies referred in the template cannot be found (such as System.Core.dll). I added few lines to add the -P parameters to the command line:

string incldirs="";
foreach (string refpath in helper.GetItemGroupValue("ReferencePath").Split(new char[] { ';' }))
{
incldirs += "-P \"" + Path.GetDirectoryName(refpath)+ "\" ";
}
Left by Tommaso on Aug 03, 2009 11:23 AM

# re: ExecuteT4Template MSBuild task: Updated
Requesting Gravatar...
Regarding the ProjectFactory wrong index problem. I realized that in some case there can be more than one value in the accessed data structure (contexts). In my case a Solution and a Project.
As workaround in Project Factory I check for the project that has among the usingTasks a task with the same name of the type of the executingTask passed as argument. But I am quite sure that there could be some case where two project could be listed and possibly both having conflicting UsingTasks (both uses ExecuteT4Template, for instance).
Here is the code for my workaround:

project = null;
string currentTaskType = executingTask.GetType().Name;
foreach (var cnt in contexts.Values) {
object parentProject = ReflectionHelper.GetPropertyValue(cnt, ReflectedMember.ParentProject);
ms.UsingTaskCollection utc = ReflectionHelper.GetPropertyValue(parentProject, ReflectedMember.UsingTasks) as ms.UsingTaskCollection;
foreach (ms.UsingTask ut in utc) {
if (ut.TaskName == currentTaskType) {
project = new ReflectedProject(parentProject);
break;
}
}
if (project != null)
break;
}
Left by Tommaso on Aug 18, 2009 5:08 AM

# re: ExecuteT4Template MSBuild task: Updated
Requesting Gravatar...
For the following where there is more than one template item the code is also failing.

<Target Name="ExecuteT4Template">
<ExecuteT4Template ... />
<ExecuteT4Template />
</Target>

When it gets to the second template the line in ProjectFactory which gets the contexts (Hastable contexts = ReflectionHelper.GetFieldValue(engineCallback, ReflectedMember.ExecutionContexts) as Hashtable;) has a problem. It returns one context as item zero in the Hashtable, but it's value is null)

Any ideas on why this would be occuring?
Left by Nick Zdunic on Jan 10, 2010 7:27 PM

# re: ExecuteT4Template MSBuild task: Updated
Requesting Gravatar...
Useful info, have sent the link to this blog to my friends. I think people will get a lot better results following this post. I am hopefully going to be one of them. Thanks for putting this information up. This is EXACTLY what I've been looking for.
Left by Hotel isola d'Elba on Mar 05, 2010 5:04 AM

# re: ExecuteT4Template MSBuild task: Updated
Requesting Gravatar...
Admiring the time and effort you put into your blog and detailed information you offer! I didn't know that!
Left by vacanze isola d'Elba on Mar 05, 2010 5:17 AM

# re: ExecuteT4Template MSBuild task: Updated
Requesting Gravatar...
Your blog provided us with valuable information to work with.Each & every tips of your post are awesome.Thanks a lot for sharing.Keep blogging.
Left by imergent_69 on Apr 10, 2010 8:11 AM

# re: ExecuteT4Template MSBuild task: Updated
Requesting Gravatar...
First You got a great blog .I will be interested in more similar topics. i see you got really very useful topics , i will be always checking your blog thanks.
Left by content filter-57 on Apr 26, 2010 6:39 AM

# re: ExecuteT4Template MSBuild task: Updated
Requesting Gravatar...
It is very interesting article. I enjoyed your distinguished way of writing this post. You have made it easy for me to understand.Thanks a lot for sharing.Keep blogging.
Left by neways87 on Apr 28, 2010 9:02 AM

# re: ExecuteT4Template MSBuild task: Updated
Requesting Gravatar...
Totally informative post.I appreciate the tips and the recommendation.Thanks for the encouraging words.I have bookmarked it already.Keep blogging.
Left by alberghi toscana mare on May 30, 2010 5:13 AM

# re: ExecuteT4Template MSBuild task: Updated
Requesting Gravatar...
Its really one of the fabulous post which i like a lot.Each & every tips of your post are awesome.Keep up the good works.
Left by home security systems11 on Jun 12, 2010 1:41 AM

# re: ExecuteT4Template MSBuild task: Updated
Requesting Gravatar...
Really a superb entry, definitely useful stuff. Never ever considered I'd find the facts I need right here.Thanks.Keep up the good works.
Left by Hotel Südtirol on Jul 29, 2010 8:13 AM

# re: ExecuteT4Template MSBuild task: Updated
Requesting Gravatar...
It’s really good and important information’s am really thankful to u as well as I would like to tell you this is really a good article. Looking forward to reading more about this.
Left by civitavecchia private taxi on Aug 05, 2010 12:02 PM

# re: ExecuteT4Template MSBuild task: Updated
Requesting Gravatar...
Hello there, I’m quite impressed by your blogging ability. I’m a blogger myself.
Left by Uffizi tickets on Aug 05, 2010 12:07 PM

# re: ExecuteT4Template MSBuild task: Updated
Requesting Gravatar...
It's so refreshing to find articles like the ones you post on your site. This is an enlightening blog post that has opened my eyes to new avenues that need to be explored.
Left by Cauzione on Aug 05, 2010 12:23 PM

# re: ExecuteT4Template MSBuild task: Updated
Requesting Gravatar...
I am really enjoying reading your well written articles. It looks like you spend a lot of effort and time on your blog. I have bookmarked it and I am looking forward to reading new articles.
Left by tamara lowe on Oct 11, 2010 7:36 AM

# re: ExecuteT4Template MSBuild task: Updated
Requesting Gravatar...
I really love how it is easy on my eyes and the details are well written. I am wondering how I might be notified whenever a new post has been made. I have subscribed to your rss feed which should do the trick! Have a nice day!
Left by phlebotomy certification on Nov 27, 2010 9:46 AM

# re: ExecuteT4Template MSBuild task: Updated
Requesting Gravatar...
Strouds shows are super entertaining and often shows a wonderful unspoiled landscape. His new show, "Off the Grid" has this stingy Al Gore nay-sayer seriously considering building a place for my own family. Thanks Les! Please do more episodes from your Off The Grid Estate!! I'm hooked!
Office 2010 Product Key
Left by Office 2010 Product Key on Dec 09, 2010 8:23 AM

# re: ExecuteT4Template MSBuild task: Updated
Requesting Gravatar...
It's so refreshing to find articles like the ones you post on your site. This is an enlightening blog post that has opened my eyes to new avenues that need to be explored.
Left by Parenting on Dec 10, 2010 8:47 AM

# re: ExecuteT4Template MSBuild task: Updated
Requesting Gravatar...
Any way I'll be subscribing to your feed and I hope you post again soon
Left by Become Radiology Technician on Dec 14, 2010 3:04 PM

# re: ExecuteT4Template MSBuild task: Updated
Requesting Gravatar...
good stuff with marvelous information.
Left by IGCSE schools in Bangalore on Jan 04, 2011 8:55 AM

# re: ExecuteT4Template MSBuild task: Updated
Requesting Gravatar...
T4 means Text Templating Transformation Toolkit which is available in Visual Studio 2005. MSBuild extensions are the part of the Visualization and Modeling SDK. The main benefits of design-time code generation are its simplicity and automatic addition of the generated files in Visual Studio.
Left by disk backup solutions on Jan 08, 2011 9:14 AM

# re: ExecuteT4Template MSBuild task: Updated
Requesting Gravatar...
Very rarely do I come across a blog thats both informative and entertaining, and let me tell you, youve hit the nail on the head. Your blog is important; the issue is something that not enough people are talking intelligently about.
Left by persönliche assistenz on Jan 29, 2011 8:45 AM

# re: ExecuteT4Template MSBuild task: Updated
Requesting Gravatar...
You are best i really like your post keep it up
Left by flowers to australia on Mar 09, 2011 9:32 AM

# re: ExecuteT4Template MSBuild task: Updated
Requesting Gravatar...
I will read time to time that!!!
Left by evitamins on Mar 29, 2011 12:31 PM

# re: ExecuteT4Template MSBuild task: Updated
Requesting Gravatar...
Thank you for a very clear and helpful post. I am definitely enjoyed to read this post.
Left by Planet Shoes Coupons on Apr 18, 2011 1:41 PM

# re: ExecuteT4Template MSBuild task: Updated
Requesting Gravatar...
I feel very glad for this quality content, you did such a fabulous work in this blog.
Left by Rocawear on Apr 19, 2011 7:57 AM

# re: ExecuteT4Template MSBuild task: Updated
Requesting Gravatar...
Thanks for sharing this wonderful nice thought to us. this is full of adventures and very interesting.
Left by Telecom Technologys on Apr 30, 2011 11:08 AM

# re: ExecuteT4Template MSBuild task: Updated
Requesting Gravatar...
What the result you have when you use T4 engine directly?
Left by online ged programs on May 13, 2011 7:02 AM

# Cheap Christian Louboutins Platforms
Requesting Gravatar...
Christian Louboutin Outlet,Christian Louboutins Cheap,Christian Louboutin Boots Sale,Christian Louboutins For Cheap
Left by Cheap Christian Louboutins on May 30, 2011 6:38 AM

# re: ExecuteT4Template MSBuild task: Updated
Requesting Gravatar...
We are a group of independents opening a new scheme in our community, you have done a good job here giving us a whole new way of looking at certain issues, great topic ;-)
Indian escorts London
Left by London Escorts on Jun 04, 2011 11:59 PM

# re: ExecuteT4Template MSBuild task: Updated
Requesting Gravatar...
Great post!! I love your content and Thankyou very much, I have found this info extremely good!

picaboo coupons
Left by picaboo coupons on Jun 24, 2011 12:00 PM

# re: ExecuteT4Template MSBuild task: Updated
Requesting Gravatar...
I Like your posting keepo it up!!!

Love Shayari
Left by Love Shayari on Jun 24, 2011 2:30 PM

# re: ExecuteT4Template MSBuild task: Updated
Requesting Gravatar...
this is really what i need thanks for this info.
Left by janitorial cleaning supplies on Jun 25, 2011 7:36 AM

# re: ExecuteT4Template MSBuild task: Updated
Requesting Gravatar...
I can’t imagine the community without your sharings. this is the best place for sharing the topics over here.
Left by A2B Lancaster Locksmiths on Jul 14, 2011 6:24 AM

# re: ExecuteT4Template MSBuild task: Updated
Requesting Gravatar...
Hello there, I’m quite impressed by your blogging ability info Impressive I like it very much
Left by Mectaler on Aug 02, 2011 10:52 AM

# re: ExecuteT4Template MSBuild task: Updated
Requesting Gravatar...
thanks for share this great thread.
Left by forex currency trading system on Aug 20, 2011 11:51 AM

# re: ExecuteT4Template MSBuild task: Updated
Requesting Gravatar...
I'm a regular visitor of this site. Keep it up. Thanks for sharing information that is actually helpful. Good day!
Left by way2sms login on Sep 24, 2011 5:06 PM

# re: ExecuteT4Template MSBuild task: Updated
Requesting Gravatar...
I highly appreciate your effort and the points you just raised were worth mentioning. I will recommend you in my blog as well.

nokia mobiles
Left by nokia mobiles on Oct 10, 2011 7:43 AM

# re: ExecuteT4Template MSBuild task: Updated
Requesting Gravatar...
I can't wait for the next update to come soon. I'll probably try to implement T4 templates later. Thanks for this update! Have a good day!
Left by Keyword Research Tool on Nov 20, 2011 2:18 PM

# re: ExecuteT4Template MSBuild task: Updated
Requesting Gravatar...
"I ever welcome to pen in my position something equal that but I work you'r faster
Left by web design rajasthan on Jan 04, 2012 5:00 AM

# re: ExecuteT4Template MSBuild task: Updated
Requesting Gravatar...
interesting, thanks for all the info.
Left by bowling tips on Jan 30, 2012 12:45 PM

# re: ExecuteT4Template MSBuild task: Updated
Requesting Gravatar...
I have read the whole article, It is quite interesting.....

Ramadan Kareem Wishes
Left by Ramadan Kareem Wishes on Jun 20, 2014 6:44 PM

# re: ExecuteT4Template MSBuild task: Updated
Requesting Gravatar...
Thanks for sharing such a wonderful topic Read Quotes wishes Greetings
Left by Anand Srivastava on Jun 21, 2014 9:48 PM

# re: ExecuteT4Template MSBuild task: Updated
Requesting Gravatar...
This is a lovely article...thanks !!


friendship day 2014

friendship day 2014 greetings

friendship day 2014 wishes

friendship day quotes

friendship day 2014 quotes

friendship day jokes
Left by Alok on Aug 03, 2014 2:04 PM

Your comment:
 (will show your gravatar)
 


Copyright © Elton Stoneman | Powered by: GeeksWithBlogs.net | Join free