home learn tableau about
SDLC VS 2010 Coded UI Test Coded UI Test and Fitnesse MSBuild 4.0 MSBuild and IronPython MSBuild and IronPython - TFS checkins MSBuild and IronPython - Custom SQL Data

previous next

IronPython Class Factory for MSBuild 4.0 Part 2

Example 1: PyChangeCase task, use standard Python code

This example uses only standard, native Python string methods to manipulate the text that is passed in and looks like:

 <!-- Task: PyChangeCase, change case on string via Python -->
  <UsingTask TaskName="PyChangeCase"  TaskFactory="PythonClassFactory" 
        AssemblyFile="..\_compiled\PythonClassFactory.dll" >
      <inString ParameterType="System.String" Required="true" />
      <inCaseChange ParameterType="System.String" Required="true" />
      <outString Output="true" />

if inCaseChange == 'upper':
  outString = inString.upper()
elif inCaseChange == 'lower':
  outString = inString.lower()
elif inCaseChange == 'title':
  outString = inString.title()
elif inCaseChange == 'swapcase':
  outString = inString.swapcase()
else :
  outString = inString

  <Target Name="PyTarget1" >
    <PyChangeCase inString="$(inStringValue)" inCaseChange="$(inCaseChangeValue)" >
      <Output PropertyName="localProperty" TaskParameter="outString"  />
    <Message Text="Input string: $(inStringValue)  Change case value: $(inCaseChangeValue)" 
    <Message Text="Output string: $(localProperty)" Importance="High"/>

There are three main sections to this snippet:

1) UsingTask element

Defines the custom task, see MSDN documentation on UsingTask. The key factors here in terms of the new Python class factory is to set the TaskFactory element = "PythonClassFactory" and the AssemblyFile = a path that will resolve to PythonClassFactory.dll when the Team Build takes place. As noted earlier, the IronPython dll's need to be in this same directory. There are three parameters defined, two going in and one coming out; all are strings. The Task element contains the actual Python code to be executed and is delimited within a CDATA section, meaning it won't be parsed by MSBuild. The simple code here looks at the value of inCaseChange string and uses it to decide how the contents of inString should be transformed. The resulting string is assigned to the outString variable, which becomes an output parameter. Nothing specific to IronPython going on here, standard Python string methods.

2) PropertyGroup element

Not technically needed for this example but it provides a default implementation of the Task. If nothing is passed in on the command line in terms of properties, the string "test" will be input to the Task and (hopefully) "TEST" will be returned. Going back to the CommandLineArguments property of the workflow's MSBuild_PythonTargets Activity, we can see that values (inStringValue & inCaseChangeValue) are indeed being passed in dynamically, via /p: syntax. The string "this is some input text" is passed and should be modified via the Python str.title() method.

3) Target element

Serves as the 'entry' method for a grouping of MSBuild elements and will be called by MSBuild_PythonTargets. An element with the name of the Task, PyChangeCase, sets the input and output parameters. The inString and inCaseChange parameters are assigned to the current value of the internal MSBuild inStringValue and inCaseChangeValue properties. The parameter being returned by the code snippet is named outString and its value will be assigned to a local MSBuild property named localProperty. After PyChangeCase task has run two Message tasks are used to log what values went in and what came out.

Give it a try by kicking off a PythonBuildDef1 build and, assuming success, examine the results by looking in (newBuild)\Logs\myPythonTargetsLog. There the relevant section confirms the original string was correctly formatted to 'title' case, capitalizing the first character of each word:

   Input string: this is some input text  Change case value: title
   Output string: This Is Some Input Text

Next, introducing IronPython...

previous next