The ideas in this post were inspired by the work done by Robin Curry in his post Integrating SourceMonitor Code Metrics Into An NAnt / CruiseControl.NET Build. He did some great work and I would like to express my gratitude.
Unfortunately I found that those CruiseControl stylesheets gave me far too much information. The summary data that SourceMonitor produces also is not as useful as it could be. What I wanted on my build server was a way to quickly glance at the state of the code on a project and to identify code that needed to be reviewed or refactored. At the end of the day the only information I was really interested in was the code complexity and how deeply nested the code blocks were.
The other problem with the SourceMonitor output is that, depending on the size of the project, it could be between 100K and 300K and if you multiply that by the number of projects and checkins on your build server it quickly adds up. At my company it would amount to about 20MB a day. I therefore decided to take Grant Drake’s NCoverExplorer approach and summarise the output before integrating it with CruiseControl. To do this I created a simple stylesheet that is run after creating a detailed SourceMonitor report.
The end result of all that work is a report that looks like this:
To get all this to work you need to do the following:
- Copy the file SourceMonitor.xsl (2.88 KB) into your CruiseControl.Net\webdashboard\xsl folder.
- Add the following line to the dashboard.config (in CruiseControl.Net\webdashboard).
<buildPlugins>
<!-- Summary reports -->
<buildLogBuildPlugin />
<!-- Other detail reports -->
<xslReportBuildPlugin description="SourceMonitor Report" actionName="SourceMonitorReport" xslFileName="xsl\SourceMonitor.xsl" />
</buildPlugins>
- Copy the file SourceMonitorSummaryGeneration.xsl (2.18 KB) to your build script folder or a common location.
- Ensure that you have installed MsBuild Community Tasks available at http://msbuildtasks.tigris.org/. You also need to ensure that you have imported the Community Task targets in your build file:
<Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets" />
- Add the following to your MsBuild script and call the RunSourceMonitor target. Substitute the paths depending on your project directory structure and the location of the xsl file above.
<!-- Source Monitor Command File -->
<PropertyGroup>
<SourceMonitorCommand>
<![CDATA[
<?xml version='1.0' encoding='UTF-8' ?>
<sourcemonitor_commands>
<write_log>false</write_log>
<command>
<project_file>TempProject.smp</project_file>
<project_language>CSharp</project_language>
<source_directory>..\</source_directory>
<include_subdirectories>true</include_subdirectories>
<export>
<export_file>..\SourceMonitorDetailedOutput.xml</export_file>
<export_type>2</export_type>
</export>
</command>
</sourcemonitor_commands>
]]>
</SourceMonitorCommand>
</PropertyGroup>
<!-- Runs the source monitor metrics program and creates a summarised report for CruiseControl-->
<Target Name="RunSourceMonitor">
<!-- Create the command file -->
<WriteLinesToFile
File="SourceMonitorCommands.xml"
Lines="$(SourceMonitorCommand)"
Overwrite="true" />
<!-- SourceMonitor Metrics -->
<Exec
Command=""e;C:\Program Files\SourceMonitor\SourceMonitor.exe"e; /C SourceMonitorCommands.xml" />
<!-- Convert the SourceMonitor Output into a more usable summarised version -->
<Xslt
Inputs="..\SourceMonitorDetailedOutput.xml"
Xsl="SourceMonitorSummaryGeneration.xsl"
Output="..\SourceMonitorResults.xml" />
<!-- Remove the intermediate files -->
<Delete Files="..\SourceMonitorDetailedOutput.xml;TempProject.smp;SourceMonitorCommands.xml" />
</Target>
- Make sure you are integrating the XML output into your CruiseControl report. I set it up so that all files ending with Results.xml are included.
<publishers>
<merge>
<files>
<file>*Results.xml</file>
</files>
</merge>
</publishers>
And viola you should have your summarised SourceMonitor view of the 15 most complex and deeply nested pieces of code for your current system.
I hope that you find this useful.