Click here to Skip to main content
15,881,715 members
Articles / DevOps / TFS

TFS Rev-Up, A Utility to Increment Build Revisions

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
25 Aug 2013CPOL4 min read 20.4K   96   8   2
A utility to increment build numbers for TFS

Introduction

My team uses TFS (Microsoft Team Foundation Server 2010) to do nightly builds. With TFS, it is very easy to set up nightly builds. However, TFS does not auto-increment the build number or contain any mechanism to do this. This means that every build has the same number, unless a developer changes it. This project is a utility that will increment the build number, every time it is run.

The process for incrementing the build number is:

  1. Check-out your AssemblyInfo.cs/.vb file
  2. Increment the lowest digit (1.1.1.1 becomes 1.1.1.2)
  3. Check-in the file

My team scheduled this utility to run each night before the scheduled build. However, we could have chosen to include it into the build template or workflow. If we ever switch to a “Continuous Integration” process, we will move this utility into the build template (or workflow).

Prerequisites

  1. To run this, you need to have the TFS client files installed.
  2. You need permissions to TFS and any TFS path that you will change/increment. If you are running this project (EXE) as a scheduled process, then the scheduler (or task) needs to use an account with those permissions.

Setting Things Up

Project

I created a command line project (with an optional GUI). To do this, I created a “Visual C#“, Windows, “Windows Forms Application”. After the project was created, I went into the Project Properties, under the Application tab, I changed the Output type to “Console Application”. Finally, I changed the Main() function to detect command-line mode versus UI mode, by testing for command-line arguments:

C#
static void Main(string[] args)
{
    //if no command line args then start with UI
    if (args == null || args.Length < 1)
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new frmMain());
    }
    else //run in silent mode
    {
        if (args[0].Contains("silent"))
            Console.WriteLine("Version Revved up to " + TfsRevUp.DoRevUp(
               TfsAutoRevUp.Properties.Settings.Default.TfsServerUrl,
               TfsAutoRevUp.Properties.Settings.Default.AssemblyInfoFilePath));
        else
            Console.WriteLine("Use the /silent arg to run in silent mode.\n" +
               "Example: TfsRevUp.exe /silent");
    }
}

Reference the TFS Assemblies

In the C# project, the following DLLs (TFS assemblies) are needed:

  • Microsoft.TeamFoundation.dll
  • Microsoft.TeamFoundation.Client.dll
  • Microsoft.TeamFoundation.Common.dll
  • Microsoft.TeamFoundation.VersionControl.Client.dll
  • Microsoft.TeamFoundation.VersionControl.Common.dll

These files can usually be found in the folder: c:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\ReferenceAssemblies\v2.0\

Make a Configuration Setting that Points to TFS

I set up this utility to work for one project at a time. The settings went into the app.config file (TfsRevUp.exe.config) so I can change it without recompiling.

Image 1

Using the Code

All of the work for this utility is in one class, named TfsRevUp(.cs).

C#
namespace TfsRevUp
{
    class TfsRevUp
    {
       public static string DoRevUp(string TfsServerUrl, string AssemblyInfoFilePath)
       {

The values for these two arguments will come from the app.config file (Project Settings, above)

Includes

To accomplish my goals, I need to use two different TFS libraries, at the same time. One is for the TFS server and one is for the TFS client. The names are long, so I used an alias for the client namespace:

C#
using TFS=Microsoft.TeamFoundation.VersionControl.Client;

Screen shot (UI or command line)

Image 2

- or -

Image 3

Connect to TFS

TFS is designed to host several services. Source control is only one of them. So I need two connections:

C#
Microsoft.TeamFoundation.Client.TeamFoundationServer tfServer;
TFS.VersionControlServer verServer;
 
//connect to the TFS server
tfServer = new Microsoft.TeamFoundation.Client.TeamFoundationServer(TfsServerUrl);
 
//connect to the Source Control service
verServer = tfServer.GetService<TFS.VersionControlServer>();

Create a Workspace

C#
//TFS needs a workspace to do any check-in/out stuff 
const string cWorkspaceName = "RevUp-TempWorkspace";
TFS.Workspace[] workspaces = verServer.QueryWorkspaces
(cWorkspaceName, verServer.AuthorizedUser, System.Environment.MachineName);
TFS.Workspace wsp;
 
if (workspaces == null || workspaces.Length < 1)
   wsp = verServer.CreateWorkspace(cWorkspaceName, verServer.AuthorizedUser);
else
   wsp = workspaces[0];

Map the Workspace to a Local HDD Path

C#
string strLocalPath = System.IO.Directory.GetCurrentDirectory() + "\\Temp\\";
 
//if the local path (temp) doesn’t exist, create it now
if (!System.IO.Directory.Exists(strLocalPath)) 
   System.IO.Directory.CreateDirectory(strLocalPath);
//map to a local path for editing (unless it already is mapped)
if (!wsp.IsServerPathMapped(AssemblyInfoFilePath))
   wsp.Map(AssemblyInfoFilePath, strLocalPath);

Check-out the File

C#
//make sure I have permissions to check-out the file
if (!wsp.HasCheckInPermission)
{
   return "You do not have permissions to rev-up the project";
}
string strLocalFileName = strLocalPath + "AssemblyInfo.cs";
 
//make sure no one else has the assemblyinfo.cs file checked-out exclusively
TFS.PendingSet[] sets = wsp.QueryPendingSets(new string[] { strLocalFileName }, 
TFS.RecursionType.None, cWorkspaceName, verServer.AuthorizedUser, false);
if (sets.Length < 1)
{
   //get the file
   wsp.Get();
 
   //check-out the file for edit
   wsp.PendEdit(new string[] { strLocalFileName }, 
   TFS.RecursionType.None, null, TFS.LockLevel.CheckOut);
 
   //update the contents of the file
   strNewRev = UpdateAssemblyInfoFile(strLocalFileName);

Parse and Up the Rev

Once I’ve gotten a local copy of the Assembly info file, I find the version number, add 1, update the file and save it.

C#
private static string UpdateAssemblyInfoFile(string filename)
{
   System.Text.RegularExpressions.MatchCollection matches;
   const string cRxMask = "\\\"[0-9]+.[0-9]+.[0-9]+.[0-9]+\\\"";
   string newVer = "";
 
   //read-in the whole AssemblyInfo file
   string strFileContent = System.IO.File.ReadAllText(filename);
 
   //use a regular expression to find the version text(s)
   matches=System.Text.RegularExpressions.Regex.Matches(strFileContent, cRxMask);
   foreach (System.Text.RegularExpressions.Match match in matches)
   {
      //increment the lowest segment (in the format: 001)
      newVer = IncrementVer(match.Value);
 
      //replace the old ver with the newer ver
      strFileContent = System.Text.RegularExpressions.Regex.Replace(
      strFileContent, match.Value, newVer);
   }
 
   //write the new content to the AssemblyInfo file
   System.IO.File.WriteAllText(filename, strFileContent);
 
   return newVer;
}
private static string IncrementVer(string oldVer)
{
   string newVer = oldVer.Replace("\"", ""); //strip string delimiters
   char[] dot = { '.' };
 
   string[] segments = newVer.Split(dot);
 
   //increment the lowest segment (in the format: 001)
   segments[3] = (int.Parse(segments[3]) + 1).ToString("000"); //3 digits
   newVer = String.Join(".", segments);
 
   return "\"" + newVer + "\""; //restore string delimiters
}

Check In

C#
//...function DoRevUp() continued…
   //check-in the file
   wsp.CheckIn(changes, "Daily rev up");
}

Remove the Workspace

C#
   //remove the workspace so it doesn't honk-up TFS
   verServer.DeleteWorkspace(cWorkspaceName, verServer.AuthorizedUser);
 
   return strNewRev;
}

Plan B

Occasionally, somebody from my team would check-out the AssemblyInfo file and forget to check it in, or this utility would encounter an error, etc. So I added the “if” block (near the top) to “QueryPendingSets” and if it found some, it would attempt to re-commit or respond with an error message, that somebody has this file checked-out.

C#
else
{
   //if something goes wrong, replace re
   string re="Previous rev-up is being re-tried, successful"; 
   //if there was an error last time RevUp ran, then check-in the change now
   for (int x = 0; x < sets.Length; x++)
   {
      if (sets[x].Name == cWorkspaceName)
      {
         wsp.CheckIn(sets[x].PendingChanges, "Daily rev up retry");
         break;
      }
      else //if someone else has pending changes on the file, we should be concerned
      {
         re= "Cannot rev-up because someone else has the project checked-out " +
         "exclusively";
      }
   }
 
   return re;
}

Installing/Running

My team’s builds kicked-off at 1 am every day, so I used the Task Scheduler (on the TFS server) to run TfsRevUp a half-hour before, at 12:30 am. In the project, in the file Program.cs I checked for a command-line switch /silent. It will run the program as a command-line app which will “rev-up” and quit.

Image 4

Conclusion

That is all it takes to check a file in/out of TFS and increment the build number(s).

Advanced Options

This article has covered a very simple way of incrementing project build numbers for projects in TFS. However, you may want to integrate this with a TFS build or a TFS build workflow/template. Here are a few resources that you can continue reading about to cover more advanced topics that add onto this concept:

Credit

I borrowed some of this code from other projects. These guys did some excellent work and deserve credit for some of the tricky parts of my project.

History

  • Version 1.0 (original)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United States United States
I have been programming for 30 years(22 professionally). I mostly work with the Microsoft product stack (.NET, SQL Server, IIS, ASP.NET, Dynamics, TFS, etc) however, I have worked with a wide variety of others too (IBM DB2, Oracle, Java, PHP, ColdFusion, Perl, etc). I have taught classes at community colleges, and other speaking engagements. During the day, I am a consultant, programming and fixing programs.

Comments and Discussions

 
QuestionZip file missing "TfsRevUp\TfsAutoRevUp.csproj" file? Pin
JerryK6-Jul-15 15:06
JerryK6-Jul-15 15:06 
GeneralMy vote of 5 Pin
Jeff Tennessen1-Apr-14 13:00
Jeff Tennessen1-Apr-14 13:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.