Build Customization in Visual Studio 2015 & TFS 2015 using Code Metrics Utility
Posted by: Gouri Sohoni
in Category Visual Studio
Abstract: Execute Code Metrics Utility with the help of PowerShell Script as part of the new Build process in TFS and Visual Studio 2015
In one of my previous articles I explained how to use InvokeProcess activity. In another article I showed you how to execute Code Metrics Utility with the help of InvokeProcess. These articles were in the context of 2013 versions of Visual Studio and TFS where we had XAML build definitions.
In this article, I will discuss how to execute Code Metrics Utility with the help of PowerShell Script as part of the new Build process in TFS 2015. In the same context, you may also want to check how easy it is to customize build using Visual Studio 2015.
Code Metrics is a built-in utility in Visual Studio. As the name suggests, it provides information about the code whether it is maintainable or not. Originally this algorithm was developed by Carnegie-Mellon University. Microsoft adopted this and incorporated it as a part of Visual Studio from Visual Studio version 2008 onwards. It evaluates code based upon 4 criteria - Class Coupling, Depth of Inheritance, Cyclomatic Complexity and Number of Lines of Code.
Class coupling finds number of dependencies on other classes (so higher number is not good). Depth of Inheritance considers chain of inheritance from the ‘Object’ class. Cyclomatic Complexity is based on the decisions and loops. And the number of lines as you may have guessed are executable lines in the code. Code Metrics creates an index that is in between the range of 0 to 100. 0 to 9 is low, 10 to 19 is moderate and 20 onwards is high. The higher the index, more are the chances of maintainable code.
Applying Code Metrics using Visual Studio is very straight forward. Just right click on the solution in Solution Explorer and select Calculate Code Metrics
Execute Code Metrics as part of the Build
Now we need to apply this utility for the binaries created by the build. That is possible by making use of PowerShell script which runs the utility and is called as an activity in the build.
For using the utility in PowerShell script, download it from here. After installing, it is available in the folder “<system drive>:\Program Files (x86)\Microsoft Visual Studio 14.0\Team Tools\Static Analysis Tools\FxCop”. We get Metrics.exe utility in this folder.
We are going to use a very simple method to incorporate this utility in the build and send the resulting report to the same folder where build binaries are dropped.
1. Let us select one project already available to which we will create a build definition and provide Code Metrics Utility.
2. We need to make the utility available on the build machine. For that, we need to add “Metrics.exe” to the source control and later copy it to the folder for execution
3. Adding the file to Source Control is very easy. Right click on the folder in which you want to add the file and select the options. Browse to the folder where Metrics.exe exists and provide appropriate comment.
4. Create a new build definition. It automatically adds 4 steps to the build definition
5. Select the first step for Visual Studio Build and select the solution to be built. For this walkthrough, we are removing the step of Visual Studio Test.
6. Moving ahead, for the step of Publish Artifact:drop, change the path from Server to file share as follows:
7. In order to execute Metrics utility, we need to use a simple PowerShell script which takes 2 parameters: the location of the build drop folder and the executable name on which the utility is to be applied
pathNew= <SET THE VALUE>
The metrics utility takes arguments for the file for which the utility is to be applied and the output file name for report. Eg:
metrics.exe /f:”C:\windowsapp.exe” /o:Results.xml
Make sure to add $ before the complete path for any variable in script.
8. This file needs to be added to the source control so as to provide the path in the custom step we create.
9. Add a new step and select PowerShell Script
10. We need to provide the path for PowerShell script and the arguments to it
11. Once the build definition is created, save it and Queue the build.
12. The Results.xml is also dropped at the same location where all other binaries are dropped.
13. After successful triggering of build, the Results.xml file gets created with all the information about the code.
Customizing build is very easy with Visual Studio 2015. We also saw how Code Metrics utility can be used by adding a custom task to the build definition.