Book Image

Instant StyleCop Code Analysis How-to

By : Franck Leveque
Book Image

Instant StyleCop Code Analysis How-to

By: Franck Leveque

Overview of this book

In medium-sized and big projects, coding conventions are defined in order to improve readability and maintainability for all the developers of the team. Stylecop analyzes your code and detects coding rule violations during all the phases of your project lifecycle. Instant StyleCop Code Analysis How-to allows you to take advantage of the features of Stylecop by guiding you through how to configure it, how to integrate it in your project environment, and finally how to personalize it in order to fit your needs. Instant StyleCop Code Analysis How-to teaches you how to configure and integrate Stylecop in your programming environment. The book will do this by showing you how to configure Stylecop on the developers IDE to the continuous integration server. You will also learn how to customize Stylecop to fit your coding style by creating new rules as well as learning how to personalize your headers file. You will also see how to embed it in your own tools, using as an example the creation of a real time analysis add-on for Monodevelop. With Instant StyleCop Code Analysis How-to, you will have an overview of all the required steps to successfully integrate your programming team and enforce your own coding rules.
Table of Contents (7 chapters)

Automating StyleCop using a command-line batch (Simple)


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.

Getting ready

For this recipe, you need to have the following elements:

How to do it...

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.

  1. Open the Visual Studio project of StyleCopCmd.

  2. 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

  3. 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.

  4. Finally, in the file ReportBuilder.cs of the StyleCopCmd project, remove the call to the dispose method in the method created at line 437.

  5. Verify you are able to generate your binaries (right-click on Net.SF.StyleCopCmd.Console and click on Build)

  6. 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"
    
  7. 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.
    
  8. 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.

How it works...

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 parameter

  • The project files with –pf parameter

  • Directory with –d parameter, an optional –r option allows you to force recursion on the given directory

  • And 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.

There's more...

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 for Mono and Linux systems

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

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:

http://sourceforge.net/projects/stylecopcli/

Building your own

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