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 MSBuild (Simple)


In this recipe, we will see how to automate our build process using MSBuild. We will describe which lines need to be added to the MSBuild project in order to enable StyleCop analysis and how to cap the number of violations encountered before the build breaks.

Getting ready

For this recipe, you will need to have:

  • StyleCop 4.7 installed with the option MSBuild integration checked

  • A sample C# project to modify

How to do it...

  1. Open your project file with the text editor, and locate the following line:

    <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
  2. After this, add the following line:

    <Import Project="$(ProgramFiles)\MSBuild\StyleCop\v4.7\StyleCop.targets" />

    This enables StyleCop analysis on the project.

  3. Now let's modify the behavior of the StyleCop task to brake after 100 violations are encountered. Locate the first PropertyGroup section within the project file, and then add a new XML element StyleCopMaxViolationCount with a value of 100. For example:

    <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
      <PropertyGroup>
        <Configuration Condition=" '$(Configuration)' == '' ">
            Debug
        </Configuration>
        <Platform Condition=" '$(Platform)' == '' ">
            AnyCPU
        </Platform>
        <ProductVersion>8.0.50727</ProductVersion>
        <SchemaVersion>2.0</SchemaVersion>
        <ProjectGuid>
            {F029E8D9-743F-4C6F-95F3-6FBDA6477165}
        </ProjectGuid>
        <OutputType>Exe</OutputType>
        <AppDesignerFolder>Properties</AppDesignerFolder>
        <RootNamespace>VanillaProject</RootNamespace>
        <AssemblyName>VanillaProject</AssemblyName>
        <StyleCopMaxViolationCount>
            100
        </StyleCopMaxViolationCount>
      </PropertyGroup>

How it works...

The first element we added imports the StyleCop task in the project. That's all that is really required to enable StyleCop analysis via MSBuild. The element is located just under the project root node. It can be placed anywhere as long as it's a direct child of the root node. As you can see, the path used to locate the StyleCop.Targets file is dependant of the version you installed on your computer.

In the second part, I showed you how to modify the behavior of StyleCop by adding properties in the project.

There are 10 properties that can be modified that way; I will present the three most important to me:

  • StyleCopAdditionalAddinPaths: This allows you to specify other paths for your custom rules

  • StyleCopTreatErrorsAsWarnings: This allow you to turn your StyleCop violations into build errors

  • StyleCopMaxViolationCount: This allows you to specify the maximum number of violations we accept in the project before breaking the build

There's more...

Here is some other information that might come in handy in certain scenarios.

Setting up properties of the task in a more global way

In this recipe, we have seen how to modify the StyleCop task behavior on a project basis. However, we can set the behavior properties as environment variables on the machine, or within the build environment command window. Setting the properties in this way will cause StyleCop to behave in the same way for all projects where StyleCop build integration is enabled.

Excluding files from StyleCop analysis

Excluding files from analysis can be helpful in certain scenarios (such as in legacy projects, or when you add third-party Mono.Options files, for example). To do such a thing, you need to open your project file and change the compile node of the file:

<Compile Include="File.cs"/>

Should become:

<Compile Include="File.cs"> 
  <ExcludeFromStyleCop>true</ExcludeFromStyleCop> 
</Compile>