Using Gendarme with CruiseControl.Net for code analysis

Gendarme is being developed as a part of the Mono project and is a tool for code analysis. It comes with a wide range of predefined rules and can easily be extended with you own custom rules which you can write in C# or other .Net languages.

Configuring the CruiseControl.Net buidl task

CruiseControl.Net has been delivered with the Gendarme task since version 1.4.3. However, the Gendarme executable must be downloaded and installed separately. The binary can be downloaded from this link: https://github.com/spouliot/gendarme/downloads


Gendarme is designed for processing the build output assemblies in ONE directory. I.e. it does not support recursive search for assemblies, which fits well if you have one CruiseControl.Net build project per service/application, but in my case I wanted to generate a report for an entire product branch with multiple services and applications.

This can be achieved by using the <assemblyListFile> configuration element, which lets you specify a file that contains the full path to each assembly which should be analysed.
In order to generate the file, I execute the following PowerShell command:

Get-ChildItem -Path 'D:\SomeDir\Work' -Recurse -Include MyCompany*.dll -Exclude *.Test*.dll,*Generated.dll | sort -Property Name -Unique | sort -Property FullName | foreach {$_.FullName} | Out-File -FilePath 'D:\SomeDir\Artifact\AssembliesForCodeAnalysis.txt' -Width 255
The PowerShell command above will recursively scan through the directory “D:\SomeDir\Work” and include all DLL files starting with “MyCompany” excluding those which ends with “.Test.dll” or “Generated.dll”. Next it will select distinct files regardless of paths (in order to filter out shared assemblies which are duplicated), before it sorts by full path name and write the output to file.

Using the PowerShell command as an executable step, the project configuration in ccnet.config turns into this:

   1:        </msbuild>
   2:        <exec>
   3:          <executable>powershell</executable>
   4:          <buildArgs>-Command "Get-ChildItem -Path 'D:\SomeDir\Work' -Recurse -Include MyCompany*.dll -Exclude *.Test*.dll,*Generated.dll | sort -Property Name -Unique | sort -Property FullName | foreach {$_.FullName} | Out-File -FilePath 'D:\SomeDir\Artifact\AssembliesForCodeAnalysis.txt' -Width 255"</buildArgs>
   5:        </exec>
   6:        <gendarme>
   7:            <executable>C:\Program Files (x86)\Gendarme\gendarme.exe</executable>
   8:            <assemblyListFile>D:\SomeDir\Artifact\AssembliesForCodeAnalysis.txt</assemblyListFile>
   9:            <baseDirectory>D:\SomeDir\Work</baseDirectory>
  10:            <limit>2000</limit>
  11:            <severity>medium+</severity>
  12:            <confidence>high</confidence>
  13:            <quiet>FALSE</quiet>
  14:            <verbose>TRUE</verbose>
  15:            <failBuildOnFoundDefects>FALSE</failBuildOnFoundDefects>
  16:            <verifyTimeoutSeconds>600</verifyTimeoutSeconds>
  17:        </gendarme>          
  18:      </tasks>
  19:      <publishers> 
  20:        <merge> 
  21:          <files> 
  22:            <file>D:\SomeDir\Artifact\test-results\*.xml</file> 
  23:            <file>D:\SomeDir\Artifact\gendarme-results.xml</file> 
  24:          </files> 
  25:        </merge> 
  26:        <statistics />

Configuring the Dashboard

The stylesheets which are needed for showing the formatted reports in the CruiseControl.Net dasboard are included with the CruiseControl.Net installation, and just need to be referenced in dasboard.config:

   1:       <buildPlugins> 
   2:        <buildReportBuildPlugin> 
   3:          <xslFileNames> 
   4:            <xslFile>xsl\gendarme-summary-ccnet.xsl</xslFile> 
   5:         </xslFileNames> 
   6:         <xslReportBuildPlugin description="Gendarme Report" actionName="GendarmeBuildReport" xslFileName="xsl\gendarme-report-ccnet.xsl"/>
   7:        </buildReportBuildPlugin> 

Resources

Gendarme home page: http://www.mono-project.com/Gendarme

Gendarme CCNet task configuration: http://confluence.public.thoughtworks.org/display/CCNET/Gendarme+Task

Etiketter: ,