Elton Stoneman

  Home  |   Contact  |   Syndication    |   Login
  120 Posts | 0 Stories | 3602 Comments | 0 Trackbacks

News

Archives

Post Categories

[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.

 

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
posted on Tuesday, September 02, 2008 10:31 PM

Feedback

# re: An MSBuild Task to run C# code 10/4/2008 9:19 AM Elton
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

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

# re: An MSBuild Task to run C# code 4/15/2010 6:15 PM Explode My Brain
This is something i've been wanting to code for a long time, thanks!

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

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

# re: An MSBuild Task to run C# code 5/23/2011 12:31 PM Siebe valves
There is ALWAYS something unique…it just takes a while to find it sometimes! Keep blogging.

# re: An MSBuild Task to run C# code 6/4/2011 11:58 PM London Escorts
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

# re: An MSBuild Task to run C# code 6/21/2011 12:30 PM half wigs
Thanks for you kind information.
I am very happy to see it kindly keep sharing such posts to us
Thanks

# re: An MSBuild Task to run C# code 12/20/2011 6:08 PM tinnitus miracle scam
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.

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