MSbuild tasks to integrate/customize Team Foundation Version Control functionality with Team Build

 

You can use MSbuild exec task to execute any system command. The task also gives you option to specify the working folder from where to execute the command. This is useful for the scenarios where you want to run a tool from specific location. Do note that this task creates a new process and there are performance implications associated with the use.

 

Simple example on how to run any external command :-

 

<Project xmlns="https://schemas.microsoft.com/developer/msbuild/2003">

 

   <Target Name="Dummy">

      <Exec

         WorkingDirectory="c:\"

         Command="dir"/>

   </Target>

</Project>

 

Simple example on

  1. How to create new workspace?

  2. How to modify the folder mappings of the workspace?

  3. How to get files from the repository?

  4. How to apply label to files in the workspace?

  5. How to checkout specific file? 

  6. How to delete the workspace?

     

<Project xmlns="https://schemas.microsoft.com/developer/msbuild/2003">

 

   <PropertyGroup>

 

      <!-- operating folder -->

      <SolutionRoot>c:\temp</SolutionRoot>
<WorkingDirectories>$(SolutionRoot)\..\Reusable Code</WorkingDirectories>

 

      <!-- Path to command line tool -->
<TfCommand>&quot;$(_GiveYourPathToTool_)\tf.exe&quot;</TfCommand>

      <ServerName>https://MyDummyVSTSServer:8080</ServerName>

      <WorkspaceName>DummyWorkspace</WorkspaceName>

</PropertyGroup>

    

   

   <Target Name="Dummy">

 

      <! -- Create dummy workspace -->
<Exec Command="$(TfCommand) workspace /new $(WorkSpaceName) /server:$(ServerName) /noprompt" WorkingDirectory="$(WorkingDirectory)" ContinueOnError="false"/>

 

      <! -- Add mappings to the workspace -->
<Exec Command="$(TfCommand) workfold /map /workspace:$(WorkSpaceName) /server: $(ServerName) &quot;$/Reusable Code&quot; &quot;$(SolutionRoot)\..\Reusable Code &quot;" WorkingDirectory="$(WorkingDirectory)" ContinueOnError="false"/>

    

     <! -- Sync files on the local disk -->

     <Exec Command="$(TfCommand) get &quot;$/Reusable Code&quot; /recursive /version:T /force" WorkingDirectory="$(WorkingDirectory)" ContinueOnError="false" />

 

     <! --Apply Label on workspace -->
<Exec Command="$(TfCommand) label /server:$(ServerName) $(_GiveYourLabelName_) &quot;$/Reusable Code&quot; /version:W$(WorkSpaceName) /recursive" WorkingDirectory="$(WorkingDirectory)" ContinueOnError="false"/>

 

     <!— Checkout the file $/Reusable Code/dummyfile.txt -->

     <!— Note that you can also specify the relative (w.r.t workingdirectory) local path of the file -->
<Exec Command="$(TfCommand) edit &quot;$/Reusable Code/dummyfile.txt&quot;" WorkingDirectory="$(WorkSpaceName)" ContinueOnError="false"/>

 

      <!-- Delete dummy workspace -->
<Exec Command="$(TfCommand) workspace /delete $(WorkSpaceName) /server:$(ServerName) /noprompt" WorkingDirectory="$(WorkingDirectory)" ContinueOnError="false" />

 

   </Target>

 

</Project>