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)MSBuildStyleCopv4.7StyleCop.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>
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset