Integrating Code Metrics in TFS 2010 Build – WF 4.0

07/02/2011

Integrating Code Metrics in TFS 2010 Build – WF 4.0

In my last two posts I talked about What is Code Metrics and about the Code Metrics From The Command Line – Power Tools available, in this post I’ll show how you can integrate Code Metrics as part of your Build Process without writing any custom code activity.
This is not the best solution but because Jakob Ehn – Already did an amazing work to integrate Code Metrics into Team Build 2010, I will show you how to integrate the Code Metrics into the build and find out if there were not Errors during the calculation. – Not the best solution but it’s also a nice way to get inside the Build Work Flow and learn new stuff.

So let’s get started, create new Build Definition called “Build With Metrics”, go to the Process Tab and click on the DefaultTemplate.xaml like (picture below)

Download Code Metrics Build Process Template

image

After the Build Template finish loading click on “Arguments” (Down left corner) and Create new Argument called – “CheckCodeMetrics”.

image

Now, open the Metadata property in order to define a proper display name for our new Argument.

image

You can Check-in the Build Process Template, get back to the Build Definition and hit the refresh button, and you will notice you new argument is now available under the Misc category we defined in the Metadata editor.

image 

Let’s back to our Build Process Template and let defined where to perform the Code Metrics.
There more than one please you can set the Code Metrics check, but it most be after the project or projects finished the compilation.

In this demo I’ll show how to add the Metrics Check after all Projects has complete the MsBuild build.

Navigate by the following activities:

Process-> Sequence –> Run on Agent (AgentScope) –> Type Compile, Test, and Associate Changesets and Work Items –> Sequence –> Compile, Test, and Associate Changesets and Work Items –> Try Compile and Test –> Compile and Test –> For Each Configuration in BuildSettings.PlatformConfigurations –> Compile and Test for Configuration –> If BuildSettings.HasProjectsToBuild

image

After you find the activity search for the end of it and the start of new activity called – “If Not Disable Tests”

image

Open ToolBox and search for “Control Flow” section, drag the “If” activity (Picture Above).
Click on the “IF” activity and define in the condition field – CheckCodeMetrics = True, the display name can be anything you like.

image

From the ToolBox drag new activity called Sequence (This will allow you to add more activities when the condition is true), change the activity Display Name to – “Run Code Mertics for Output Dll’s and Exe’s”.
Under the “Team Foundation Build Activities” section in ToolBox, drag the “InvokeProcess” activity into the new Sequence you just added ,change the the activity Display Name to – “Run Metrics.exe Power Tool”
The propose of this InvokeProcess is to run the Metrics.exe file with out build arguments.

image

Selecting the InvokeProcess activity you need to modify the properties:

As I mention above Jakob Ehn did much more advanced work, but I want to do it in the sample way of – if the Code Metrics passed by Microsoft criterion than my build is ok, In order to do so, I’ll add new argument to the Metrics.exe power tool called – successfile

/successfile -   Create .lastcodemetricsssucceeded file in output report directory if no errors occur during calculation.

FileName = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), "Microsoft Visual Studio 10.0\Team Tools\Static Analysis Tools\FxCop\metrics.exe")

Arguments = String.Format("/f:""{0}\*.dll"" /f:""{0}\*.exe"" /out:""{0}\CodeMetricsResult.xml"" /successfile", BinariesDirectory)

image

After the Build will run the Metrics.exe file we want to make sure the .lastcodemetricsssucceeded file exists in our BinariesDirectory.
In order to do so, drag new IF activity from the ToolBox after the InvokeProcess and add the following Condition:

Directory.GetFiles(BinariesDirectory, "*.lastcodemetricssucceeded").Length = 1

Again, you can find more technics to check if the Code Metrics Passed but this simply will tell you I the Code Metrics checked passed for all files.

Drag an additional activity to the Else road called – “SetBuildProperties”, Changed “PropertiesToSet” to “Status” and in the “Status” define what if the Code Metrics fails, I defined the build status to “Partially Succeeded”.

image

image

Drag another activity called – WriteBuildMessage, define the message you want to see if the Code Metrics check passed and make sure to change the Importance property to High.

image

Now let Check-in the Build Process File and check our new Build.

image

If the Code Metrics Calculation will failed for any reason the Build Status will be “Partially Succeeded”

Download Code Metrics Build Process Template

Enjoy.

Add comment
facebook linkedin twitter email

Leave a Reply

4 comments

  1. Jason25/01/2012 ב 16:11

    The Download link appears to be broken. Any chance you can fix it?

  2. Rajesh02/03/2012 ב 05:59

    Hi,

    I have successfully incorporate the code metrics in TFS build. But its resulted XML file.
    How we can convert the codemetrics xml file to excel?
    is any way to do automatic conversion from TFS build.

    please help me to resolve this issue.

  3. Radhika18/04/2012 ב 17:00

    I have integrated code metrics in TFS build successfully. when I queue the build, the resulted XML is empty. please let me know where I did mistake.

    Thanks in advance.

  4. Radhika23/04/2012 ב 12:37

    I have integrated code metrics in TFS 2010 build and able to generate MetricsResults.xml successfully. But the xml file is empty