The Script Task in MSBuild
As part of your build process customization, you may sometimes prefer invoking a simple piece of C# or VB code to relatively elaborate MSBuild script segments while implementing certain functionalities.
As part of your build process customization, you may sometimes prefer invoking a simple piece of C# or VB code to relatively elaborate MSBuild script segments while implementing certain functionalities.
In case you are not interested in writing your own custom tasks and compiling them into a dll for these small customization needs, MSBuild Community Tasks has a Script task for such cases
Sticking to tradition, let's go with a 'hello world' example:
<Import Project="$(MSBuildExtensionsPath)/MSBuildCommunityTasks/MSBuild.Community.Tasks.Targets" />
<PropertyGroup>
<Text1>Hello</Text1>
<GetFullText>
<![CDATA[
public static string ScriptMain() {
string Text2 = "World";
return "$(Text1)" + Text2;
}
]]>
</GetFullText>
</PropertyGroup>
<Target Name="TestScriptTask">
<Script Language="C#" Code="$(GetFullText)">
<Output TaskParameter="ReturnValue" PropertyName="FullText" />
</Script>
<Message Text="My String: $(FullText)" />
</Target>
Sticking to tradition, let's go with a 'hello world' example:
<Import Project="$(MSBuildExtensionsPath)/MSBuildCommunityTasks/MSBuild.Community.Tasks.Targets" />
<PropertyGroup>
<Text1>Hello</Text1>
<GetFullText>
<![CDATA[
public static string ScriptMain() {
string Text2 = "World";
return "$(Text1)" + Text2;
}
]]>
</GetFullText>
</PropertyGroup>
<Target Name="TestScriptTask">
<Script Language="C#" Code="$(GetFullText)">
<Output TaskParameter="ReturnValue" PropertyName="FullText" />
</Script>
<Message Text="My String: $(FullText)" />
</Target>
The return type of ScriptMain() can be void or string.
Now, the auto-generated code and the build output would look something like this:
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
public class msbc96d6fb42e75a487c93c422f4f56a72da {
public static string ScriptMain() {
string Text2 = "World";
return "Hello" + Text2;
}
}
Done executing task "Script".
Task "Message"
My String: HelloWorld
No comments:
Post a Comment