Paul Hacker .Net, TFS, Etc…

September 17, 2013

TFS 2012 Build custom versioning build template

Filed under: Team System — phacker @ 7:08 pm

There are quite a few posts out there for creating a custom workflow that increments the version number so I am not walking through all the steps to do it, but rather why did it. I found the following posts as excellent resources to start with:

http://www.ewaldhofman.nl/post/2010/04/20/Customize-Team-Build-2010-e28093-Part-1-Introduction.aspx

http://www.bryanavery.co.uk/post/2012/12/07/Enhancing-the-Build-in-TFS-2012.aspx

I started with Bryan’s blog and went from there. I had some requirements that I had to follow which is why I created a new one.

The requirements were:

1. Must write the version number to a file and not check in stamped assemblyinfo files

2. There must be a way to set a revision increment. Basically they needed to set a value to increase the revision number by. There strategy was even revision number for patches and odd for full installs.

3. I had to set an Environment variable with the new version number

The following is what I came up with for the Execute method:

protected override void Execute(CodeActivityContext context)

{

var sourcesDirectory = context.GetValue(SourcesDirectory);

Int32 major = context.GetValue(Major);

Int32 minor = context.GetValue(Minor);

Int32 build = context.GetValue(Build);

Int32 revision = context.GetValue(Revision);

Int32 revisionIncrement = context.GetValue(RevisionIncremnent);

string versionFileLocation = context.GetValue(VersionFileLocation);

var assemblyInfoFileMask = context.GetValue(AssemblyInfoFileMask);

var oldValue = “”;

//if there is no file then create it and add a default version number that uses the revision number we set in the build

//otherwise just get the current value

if (!File.Exists(versionFileLocation))

{

oldValue = “0.0.0.” + revision.ToString();

File.WriteAllText(versionFileLocation,oldValue);

}

else

{

oldValue = File.ReadAllText(versionFileLocation);

}

 

// since all we need is the revision number lets get it.

var revisionNumber = oldValue.Split(‘.’);

int newRevision = int.Parse(revisionNumber[3].ToString()) + revisionIncrement;

//find all files that match the assembly mask we set

foreach (var file in Directory.EnumerateFiles(sourcesDirectory, assemblyInfoFileMask, SearchOption.AllDirectories))

{

string text = File.ReadAllText(file);

var newVersion = new Version(major, minor, build, newRevision );

// we want to find ‘AssemblyVersion(“1.0.0.0”)’ etc

foreach (var attribute in new[] { “AssemblyVersion”, “AssemblyFileVersion” })

{

var regex = new Regex(attribute + @”\(“”\d+\.\d+\.\d+\.\d+””\)”);

var match = regex.Match(text);

if (match.Success) text = regex.Replace(text, attribute + “(\”” + newVersion + “\”)”);

File.WriteAllText(file,text);

}

System.Environment.SetEnvironmentVariable(“VersionNumber”, newVersion.ToString(), EnvironmentVariableTarget.User);

//update the version text file with new version number

File.WriteAllText(versionFileLocation, newVersion.ToString());

}

}

The code is pretty similar to what you see in the blogs i referenced.

I added the following arguments to the xaml file: RevisionIncrement and VersionFileLocation

RevisionIncrement – The value you want to increase the revision number by in each build

VersionFileLocation  – The path to the file were we will track the version.

When I use the template in a build definition it looks like so:

Image

I also ran into a gotcha that the posts do not talk about.

The issue/solution are posted here. http://www.hugohaggmark.com/2011/09/08/stuck-on-cannot-create-unknown-type-clr-namespace-in-tfs-build/

Basically you have to update the xaml code as outlined in the post. Now you will need to add the line of code every time that you update the xaml workflow as every change you make to the xaml wipes out that line.

Hope this helps anyone looking to create a custom build workflow.

-paul

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: