Geeks With Blogs

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

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

MSBuild is lacking in handy functions. You can check if a path exists and list the contents of a directory, but everything else has to be achieved with a task. You often find yourself choosing between clumsily stringing together tasks or writing a custom task to perform a specific function – even a simple one.

Rather than putting together a function library, I've got one simple task which takes C# code as a parameter and executes it. The code can consist of one or more statements, can read the runtime values of properties or item groups in the build, and returns a string output (currently it expects last the code statement to be like return x.ToString()).

So if you want to prefix text to a property, get the ticks of the current time or get a valid temporary file name, you can do it like this:

<Target Name="PropertyPrefix">

<CodeExecutor Statements='string prefix = ""_old.""; return string.Concat(prefix, ""$(StartDir)"");'>

<Output TaskParameter="Output" PropertyName="ExecutorOutput"/>

</CodeExecutor>

<Message Text="Output: $(ExecutorOutput)"/>

</Target>

 

<Target Name="NowTicks">

<CodeExecutor Statements='return DateTime.Now.Ticks.ToString();'>

<Output TaskParameter="Output" PropertyName="ExecutorOutput"/>

</CodeExecutor>

<Message Text="Output: $(ExecutorOutput)"/>

</Target>

 

<Target Name="GetTempFileName">

<CodeExecutor Statements='return Path.GetTempFileName();' Directives='System.IO'>

<Output TaskParameter="Output" PropertyName="ExecutorOutput"/>

</CodeExecutor>

<Message Text="Output: $(ExecutorOutput)"/>

</Target>

The task doesn't do anything complicated; it builds a class at runtime by injecting the code statements passed to it into a T4 template (as with the T4 task, in its current form it uses the TextTransform.exe tool), compiles it in memory with CodeDom and executes a static method which now contains the code statements.

You can pass it directives if your code uses anything more exotic than System, System.Collections.Generic etc, and give it the path to any existing assemblies the code needs to reference. I'll add it as part of a set of build tasks to CodePlex but until then the code for the task is here: MSBuild CodeExecutor task.

 

Posted on Tuesday, September 2, 2008 10:31 PM MSBuild | Back to top


Comments on this post: An MSBuild Task to run C# code

# re: An MSBuild Task to run C# code
Requesting Gravatar...
Note, this task used the same logic as the ExecuteT4Template task to get hold of the executing property values. The helper class has been updated so this now works under MSBuild 2.0 and 3.5
Left by Elton on Oct 04, 2008 9:19 AM

# re: An MSBuild Task to run C# code
Requesting Gravatar...
Hi, your website's design is first-class and i like it. Your articles are totally amazing. Please continue this great work.
Left by ecommerce-33 on Apr 11, 2010 5:28 AM

# re: An MSBuild Task to run C# code
Requesting Gravatar...
This is something i've been wanting to code for a long time, thanks!
Left by Explode My Brain on Apr 15, 2010 6:15 PM

# re: An MSBuild Task to run C# code
Requesting Gravatar...
Definitely useful. MSBuild Community tasks library includes a Script task that would do just this (plus a few extra things)...
Left by Igor Pashchuk on Apr 29, 2011 4:55 PM

# re: An MSBuild Task to run C# code
Requesting Gravatar...
This simplified task can be used for various use so that I will not struggling on making script for every specific task.
Left by distance learning universities on May 13, 2011 6:43 AM

# re: An MSBuild Task to run C# code
Requesting Gravatar...
There is ALWAYS something unique…it just takes a while to find it sometimes! Keep blogging.
Left by Siebe valves on May 23, 2011 12:31 PM

# re: An MSBuild Task to run C# code
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 ;-)
London Indian escorts
Left by London Escorts on Jun 04, 2011 11:58 PM

# re: An MSBuild Task to run C# code
Requesting Gravatar...
Thanks for you kind information.
I am very happy to see it kindly keep sharing such posts to us
Thanks
Left by half wigs on Jun 21, 2011 12:30 PM

# re: An MSBuild Task to run C# code
Requesting Gravatar...
Doing anything in angle brackets for msbuild was painful for me. I write code for a living, so it just makes sense to write code to build my stuff. Lately I've been doing some work with our builds and TeamCity.
Left by tinnitus miracle scam on Dec 20, 2011 6:08 PM

Your comment:
 (will show your gravatar)
 


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