In this recipe, we will see how to use StyleCopCmd to automate our process using NAnt.
For this recipe, you will need to have:
StyleCop 4.7 installed
NAnt Version 0.89 or higher
The sample C# project used in the previous recipe
I will assume you have already used NAnt, and I will concentrate on describing the two methods to achieve integration of StyleCop task in to your NAnt script.
Note
StyleCopCmd comes with its own NAnt task. It is contained in the Net.SF.StyleCopCmd.Core.dll
file.
To include it in your NAnt script you will need to add a reference to this dll and add the following XML element in your project or target element:
<styleCopCmd outputXmlFile="stylecop-report.xml" transformFile="" recursionEnabled="true" ignorePatterns="AssemblyInfo\.cs" processorSymbols="" styleCopSettingsFile="Stylecop.Settings" > <solutionFiles> <include name="StylecopCustomRule.sln" /> </solutionFiles> <projectFiles /> <directories /> <files /> </styleCopCmd>
Once our build file is ready, we can execute it in a console, and we obtain the following output:
NAnt 0.92 (Build 0.92.4543.0; release; 09/06/2012) Copyright (C) 2001-2012 Gerry Shaw http://nant.sourceforge.net Buildfile: file:///C:/dev/StylecopCustomRule/bin/test.build Target framework: Microsoft .NET Framework 4.0 [styleCopCmd] Pass 1: StylecopCustomRule.csproj - MyCustomRule.cs [styleCopCmd] 9 violations encountered. BUILD SUCCEEDED Total time: 1.6 seconds.
As with the command-line version, we obtain two files in the directory that can be exploited in a CI to show the violations results:
stylecop-report.xml
stylecop-report.violations.xml
In the previous sample, I tried to give you the full NAnt command. In the StyleCopCmd
element we can configure six attributes:
outputXmlFile
: This attribute is used to specify the result file we want.transformFile
: This attribute is used to specify the transformation (XSLT) file we want to apply to the result file.recursionEnabled
: This attribute is used to enable recursion in the directories to check.ignorePatterns
: This attribute contains a regular expression pattern to exclude filenames from scanning; in the sample I removed theAssemblyInfo.cs
file from the scan.processorSymbols
: This attribute is used to specify a list of processor symbols (for example:DEBUG
,CODE_ANALYSIS
) to be used by StyleCop. Generally, it is not used in most scenarios.styleCopSettingsFile
: This attribute is used to specify a common settings file for all the files being scanned. If no common settings files exist, then it should be removed from the task.
The elements solutionFiles
, projectFiles
, directories
, and files
are used to specify the different kinds of element to analyze.
The explained method is not the only one usable to launch StyleCopCmd task. Another way to do it is to rely to the exec
element of the NAnt framework. It allows you to use the command line executable of StyleCopCmd (or your own if you made it). The tool can be found at the following site:
http://nant.sourceforge.net/release/0.92/help/tasks/exec.html