- | rssFeed | My book on MSBuild and Team Build | Archives and Categories Saturday, July 31, 2010

MSBuild 4 : Inline Tasks Part 3, tasks in JavaScript

A while back I talked about the new feature available in MSBuild 4 Inline Tasks in.

In the previous examples I have use C# as my language of choice. C# is not your only choice you can also use VB.Net but you can also use JavaScript! When MSBuild creates the class for the inline task it uses CodeDom to do so, and JavaScript is one of its supported languages. Perhaps its actually called JScript. In any case take a look at the project file below which shows this in action.

<Project ToolsVersion="4.0" DefaultTargets="PrintValues" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <UsingTask
    TaskName="Jsex01"
    TaskFactory="CodeTaskFactory"
    AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
    <ParameterGroup>
      <Files ParameterType="Microsoft.Build.Framework.ITaskItem[]"/>
    </ParameterGroup>
    <Task>
      <Code Type="Fragment" Language="javascript">
        <![CDATA[
          function printMessageFor(item : Microsoft.Build.Framework.ITaskItem) {
            Log.LogMessage(item.ItemSpec + "fullpath: " + item.GetMetadata("FullPath"));
          }
          
          for(var i = 0; i<Files.length; i++) {
            printMessageFor(Files[0]);
          }
          
        ]]>
      </Code>
    </Task>
  </UsingTask>

  <ItemGroup>
    <Source Include="one.cs"/>
    <Source Include="two.cs"/>
    <Source Include="three.cs"/>
    <Source Include="four.cs"/>
  </ItemGroup>
  
  <Target Name="PrintValues">
    <Jsex01 Files="@(Source)" />
  </Target>

</Project>

Here you can see that I created a new inline task called, Jsex01 and its written in Javascript. Then inside of the PrintValues target this task is called. If you execute the PrintValues target the result will be what you see below.

image

So if you prefer JavaScript to C# or VB.Net then you should try this out!

Sayed Ibrahim Hashimi

inline task | msbuild | MSBuild 4.0 Saturday, July 31, 2010 5:22:28 AM (GMT Daylight Time, UTC+01:00)  #     | 
Friday, July 23, 2010

Visual Studio Ideas

Do you have a great idea for the next version of Visual Studio and you want to get your voice heard? Now is your chance! Go to twitter and post a message stating your request with the hash tag #vswish and we will take a look at it. We are listening to that channel so please do submit your feedback.

Sayed Ibrahim Hashimi

feature-request | Visual Studio Friday, July 23, 2010 8:24:45 AM (GMT Daylight Time, UTC+01:00)  #     |