In this recipe, I will show you how to analyze your projects with StyleCop from the command line. For this, I will use a tool named StyleCopCmd, and prepare it to be able to launch the last version of StyleCop.
For this recipe, you need to have the following elements:
StyleCop 4.7 with MSBuild integration
StyleCopCmd 0.2.10 (Sources); they can be downloaded from http://sourceforge.net/projects/stylecopcmd/files/stylecopcmd/stylecopcmd-0.2.1/StyleCopCmd-src-0.2.1.0.zip/download
A sample C# project to analyze
Note
As indicated in the previous recipe, StyleCopCmd is not maintained anymore. However, the tool works correctly and need just a little tweaking to be run with the last version of StyleCop. That's what we will do in this recipe.
Open the Visual Studio project of StyleCopCmd.
First, we have to change the references of StyleCop libraries from 4.3 to 4.7. This is done quite easily in the all projects by removing the references to:
Stylecop
Stylecop.CSharp
Stylecop.CSharp.Rules
Use Visual Studio to replace all
Microsoft.Stylecop
occurrences by StyleCop. When the project was put on CodePlex, one of the first things undertaken was the removal of Microsoft references.Finally, in the file
ReportBuilder.cs
of the StyleCopCmd project, remove the call to the dispose method in the method created at line 437.Verify you are able to generate your binaries (right-click on Net.SF.StyleCopCmd.Console and click on Build)
Now that we have our up-to-date binaries we can use them to launch StyleCop directly from the command line. To do so, open a command console, and then go to your StyleCopCmd directory and type the following command:
Net.SF.StyleCopCmd.Console.exe -ifp "((.)*(Base*)(.)*(\.cs))|((.)*(\.Designer\.cs))|(AssemblyInfo\.cs)" -sf "..\StylecopCustomRule\StylecopCustomRule.sln" -of "stylecop-report.xml"
On the screen, the only information that appears is the total number of violations and the list of files scanned:
Pass 1: StylecopCustomRule.csproj - MyCustomRule.cs 9 violations encountered.
If we look at what has been generated, you will find two files in your directory:
stylecop-report.xml
stylecop-report.violations.xml
Both displays the list of violations generated by StyleCop; the only difference is the XML structure of the files. The first one follows StyleCopCmd internal schema and transformation files whereas the second is a bare output of StyleCop.
StyleCopCmd comes with a lot of options out of the box.
In the previous example, I made you give a solution file. However, StyleCop allows four kinds of entry point:
The solution files with
–sf
parameterThe project files with
–pf
parameterDirectory with
–d
parameter, an optional–r
option allows you to force recursion on the given directoryAnd files with
–f
parameter
The ipf
parameter allows you to remove some of the files from StyleCop scan by providing a regular expression matching their filename.
Finally, the -of
option permits you to specify the name of the output file. This is used with –tf
, which is used to transform the output with the help of an XSLT file. It can give you any kind of human readable report.
To get help, launch StyleCopCmd with the -?
option; this will display the available options as shown in the following screenshot:
I will let you explore the remaining possibilities.
StyleCopCmd is not the only tool available to do StyleCop analysis from the command line. As we will see later, the API of StyleCop is quite easy to understand and though they don't give a command line directly, lots of projects have been made to support this functionality.
StyleCopCmd has been made available on Mono and Linux systems thanks to the work of Ian Norton (https://github.com/inorton/StyleCopCmd).
The original version of StyleCopCmd is still linked to StyleCop 4.3, and you will have to upgrade the project to StyleCop 4.7 if you want to use the latest functionality of StyleCop.
Some problems are known and well documented. For me, the major problem I encountered was the registry key used by StyleCop. It forces the user to execute StyleCop commands with root privilege on the first start.
StyleCop CLI has the same objective as StyleCopCmd. It permits an integration of StyleCop from the command line in a wider automation system.
Compared to StyleCopCmd, this project has less functionality; one of the most important missing functionality is the ability to transform the output of StyleCop violations. However, the tool doesn't need any tweaking to be compatible with StyleCop 4.7 as it already embeds it. The tool is available at the following site:
As I said earlier, lots of people have already started this task for you. However, if you're not satisfied with the existing tools or if you just want to look at how to make one, a good start is the tutorial made by the team of StyleCop+, which gives you advice on how to start building such a tool. The tutorial is available at the following site:
http://stylecopplus.codeplex.com/wikipage?title=Running%20StyleCop%20from%20Your%20Code