Search code examples
tfsmsbuildtfsbuildchangeset

Capture msbuild tf.exe changeset /latest


Within an MSBuild file I would like to extract the changeset number from the output of the following command into a $(parameter) value;

<Exec Command="c:\path\tf.exe changeset /latest /i" />

In my build script I can see the Changeset number in the first line of the result:

Changeset: 7539
User: John Doe Date: 23 September 2015 17:03:19

Comment:
some check-in comment

Items: [[List of items here]]

Work Items: [[List of linked work items here]]

I feel like I am very close and want the simplest solution. I can use MSBuild.ExtensionPack or the MSBuild.Community tasks if there is a better option.


Solution

  • To only get the changeset number, please try the following steps: (code is quoted from this link):

    1. Download and install MSBuildExtensionPack on the machine
    2. Add the following code to the .csproj file (assume you're working with a C# project). Note that: you need to change the folder path where MSBuild.ExtensionPack.TaskFactory.PowerShell.dll is installed on your machine for the AssemblyFile property.

      <UsingTask TaskFactory="PowershellTaskFactory" TaskName="Changeset" AssemblyFile="C:\Program Files\MSBuild\ExtensionPack\4.0\MSBuild.ExtensionPack.TaskFactory.PowerShell.dll">
        <ParameterGroup>
          <changeset Output="true" />
        </ParameterGroup>
        <Task>
          <![CDATA[  
           $tf = & "C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\tf.exe" history . /r /noprompt /stopafter:1 /version:W 
           $changeset=$tf[2].Split(" ")[0] 
           ]]>
        </Task>
      </UsingTask>
      <Target Name="TestBuild">
        <Changeset>
          <Output TaskParameter="changeset" PropertyName="changeset" />
        </Changeset>
        <Message Importance="High" Text="Changeset:++++++ ::::: $(changeset)" />
      </Target>