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 NAnt (Intermediate)


In this recipe, we will see how to use StyleCopCmd to automate our process using NAnt.

Getting ready

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.

How to do it...

Note

StyleCopCmd comes with its own NAnt task. It is contained in the Net.SF.StyleCopCmd.Core.dll file.

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

How it works...

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 the AssemblyInfo.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.

There's more...

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