Thursday, September 19, 2013

Visual Studio Online (cloud based TFS): Getting multiple labels using TF GET (without deleting the files associated with the previous TF GET)

This article presents how to pull source code from TFS using the TF utility. Pulling source code in this manner is the first step in automating a build. Rather than simply pulling one label from a collection in TFS, this article presents how to pull multiple labels.

As a developer, I am a big fan of Microsoft's hosted Team Foundation Server (TFS) which is free for up to five users: http://tfs.visualstudio.com. So for a project I work on, I was tasked with automating the build using PowerShell. Before writing any PowerShell script, I wanted to learn how to pull code using TF.

Within TFS, I created two team projects: ATest and BTest. I placed a single text file in each project. I created a label for each project ATest01 and BTest01 respectively. Project is the terminology used by Microsoft's hosted TFS. ATest01 and BTest02 are actually TFS collections.

I did not want to test with the production code from TFS simply because it takes too long to pull the code so two project each with one file seemed like an efficient development environment.

TF for Visual Studio 2012 is documented here: http://msdn.microsoft.com/en-us/library/cc31bk2e.aspx. How to run TF is documented here: http://msdn.microsoft.com/en-us/library/cc31bk2e.aspx#run which explains how to open a Visual Studio console window.

The steps to get source code are;

1) Delete the workspace in case it was previously created by the build. Below we delete WorkSpaceATest and make sure not to prompt since this will be automated process:

tf workspace /delete WorkSpaceATest /noprompt

2) Create the workspace. Below we create the workspace WorkSpaceATest and again insure there is no prompt:

tf workspace /new WorkSpaceATest /noprompt

3) Map the collection to a folder and associate the mapping to a workspace. Below we map collection ATest to folder D:\ATest and associate this mapping with WorkSpaceATest:

tf workfold /map $/ATest D:\ATest /WorkSpace:WorkSpaceATest

4) Get the source code by specifying the label;
tf get /version:LATest01

The /version command-line option is used to specify a version number. By prefixing the label name with an L, the /version command-line option can be used to specific a label. The L prefixes the label ATest01.

Using the previous commands to pull the source code for the ATest and BTest collections is as follows:

tf workspace /delete WorkSpaceATest /noprompt
tf workspace /new WorkSpaceATest /noprompt
tf workfold /map $/ATest D:\ATest /WorkSpace:WorkSpaceATest
tf get /version:LATest01

tf workspace /delete WorkSpaceBTest /noprompt
tf workspace /new WorkSpaceBTest /noprompt
tf workfold /map $/BTest D:\BTest /WorkSpace:WorkSpaceBTest
tf get /version:LBTest01

As it turns out the "tf get /version:LBTest01" command deletes all the files for the ATest collection.



The command "tf workspace /new" created a new workspace and associates the current folder (c:\temp) with this workspace.  There is an error in the previous screenshot. The workspace WorkSpaceBTest was not created because the current directory (C:\Temp) was already associated with the workspace WorkSpaceATest. When the second "tf get" is invoked for label BTest01 all the files associated with label ATesst01 are deleted.

The trick here is to create each workspace while the script is running in a separate folder. Workspace WorkspaceATest will be created while the current directory is D:\ATest.
MKDIR D:\ATest
CD /D D:\ATest

Workspace WorkspaceBTest will be created while the current directory is D:\BTest.
MKDIR D:\BTest
CD /D D:\BTest

The resulting script to pull two different labels from TFS is as follows:
MKDIR D:\ATest
CD /D D:\ATest
tf workspace /delete WorkSpaceATest /noprompt
tf workspace /new WorkSpaceATest /noprompt
tf workfold /map $/ATest D:\ATest /WorkSpace:WorkSpaceATest
tf get /version:LATest01

MKDIR D:\BTest
CD /D D:\BTest
tf workspace /delete WorkSpaceBTest /noprompt
tf workspace /new WorkSpaceBTest /noprompt 
tf workfold /map $/BTest D:\BTest /WorkSpace:WorkSpaceBTest
tf get /version:LBTest01

I'd prefer a cleaner solution such as a way via the command-line to suppress "tf workspace /new" from associating the workspace with the current directory.

No comments :

Post a Comment