StyleCop analysis

Exclusive offer: get 50% off this eBook here
Instant StyleCop Code Analysis How-to [Instant]

Instant StyleCop Code Analysis How-to [Instant] — Save 50%

Learn how to analyze and maintain code for your projects using StyleCop with this book and ebook

$12.99    $6.50
by Franck Leveque | September 2013 | Open Source

This article, StyleCop analysis, talks about setting up a job project, launching it, and analyzing it using Visual Studio. The article StyleCop analysis, by Frank LEVEQUE, author of the book Instant StyleCop Code Analysis How-to, describes about building a job for project, which will also help to find out errors.

(For more resources related to this topic, see here.)

Integrating StyleCop analysis results in Jenkins/Hudson (Intermediate)

In this article we will see how to build and display StyleCop errors in Jenkins/Hudson jobs. To do so, we will need to see how to configure the Jenkins job with a full analysis of the C# files in order to display the technical debt of the project. As we want it to diminish, we will also set in the job an automatic recording of the last number of violations. Finally, we will return an error if we add any violations when compared to the previous build.

Getting ready

For this article you will need to have:

  • StyleCop 4.7 installed with the option MSBuild integration checked
  • A Subversion server
  • A working Jenkins server including:
    • The MSBuild plug in for Jenkins
    • The Violation plug in for Jenkins
  • A C# project followed in a subversion repository.

How to do it...

  1. The first step is to build a working build script for your project. All solutions have their advantages and drawbacks. I will use MSBuild in this article. The only difference here will be that I won't separate files on a project basis but take the "whole" solution:

    <?xml version="1.0" encoding="utf-8" ?>
    <Project DefaultTargets="StyleCop" xmlns="http://schemas.
    microsoft.com/developer/msbuild/2003">
    <UsingTask TaskName="StyleCopTask" AssemblyFile="$(MSBuildExtens
    ionsPath)\..\StyleCop 4.7\StyleCop.dll" />

    <PropertyGroup>
    <!-- Set a default value of 1000000 as maximum Stylecop
    violations found -->
    <StyleCopMaxViolationCount>1000000</StyleCopMaxViolationCount>
    </PropertyGroup>
    <Target Name="StyleCop">
    <!-- Get last violation count from file if exists -->
    <ReadLinesFromFile Condition="Exists('violationCount.txt')"
    File="violationCount.txt">
    <Output TaskParameter="Lines" PropertyName="StyleCopMaxViola
    tionCount" />
    </ReadLinesFromFile>

    <!-- Create a collection of files to scan -->
    <CreateItem Include=".\**\*.cs">
    <Output TaskParameter="Include" ItemName="StyleCopFiles" />
    </CreateItem>
    <!-- Launch Stylecop task itself -->
    <StyleCopTask
    ProjectFullPath="$(MSBuildProjectFile)"
    SourceFiles="@(StyleCopFiles)"
    ForceFullAnalysis="true"
    TreatErrorsAsWarnings="true"
    OutputFile="StyleCopReport.xml"
    CacheResults="true"
    OverrideSettingsFile= "StylecopCustomRule\Settings.Stylecop"
    MaxViolationCount="$(StyleCopMaxViolationCount)">
    <!-- Set the returned number of violation -->
    <Output TaskParameter="ViolationCount" PropertyName="StyleCo
    pViolationCount" />

    </StyleCopTask>
    <!-- Write number of violation founds in last build -->
    <WriteLinesToFile File="violationCount.txt" Lines="$(StyleCopV
    iolationCount)" Overwrite="true" />

    </Target>
    </Project>

  2. After that, we prepare the files that will be scanned by the StyleCop engine and we launch the StyleCop task on it. We redirect the current number of violations to the StyleCopViolationCount property.
  3. Finally, we write the result in the violationsCount.txt file to find out the level of technical debt remaining. This is done with the WriteLinesToFile element.
  4. Now that we have our build script for our job, let's see how to use it with Jenkins. First, we have to create the Jenkins job itself. We will create a Build a free-style software project. After that, we have to set how the subversion repository will be accessed, as shown in the following screenshot:

    We also set it to check for changes on the subversion repository every 15 minutes.

    Then, we have to launch our MSBuild script using the MSBuild task. The task is quite simple to configure and lets you fill in three fields:

    • MSBuild Version: You need to select one of the MSBuild versions you configured in Jenkins (Jenkins | Manage Jenkins | Configure System)
    • MSBuild Build File: Here we will provide the Stylecop.proj file we previously made
    • Command Line Arguments: In our case, we don't have any to provide, but it might be useful when you have multiple targets in your MSBuild file
  5. Finally we have to configure the display of StyleCop errors. This were we will use the violation plugin of Jenkins. It permits the display of multiple quality tools' results on the same graphic. In order to make it work, you have to provide an XML file containing the violations.

    As you can see in the preceding screenshot, Jenkins is again quite simple to configure. After providing the XML filename for StyleCop, you have to fix thresholds to build health and the maximum number of violations you want to display in the detail screen of each file in violation.

How it works...

In the first part of the How to do it…section, we presented a build script. Let's explain what it does:

First, as we don't use the premade MSBuild integration, we have to declare in which assembly the StyleCop task is defined and how we will call it. This is achieved through the use of the UsingTask element.

Then we try to retrieve the previous count of violations and set the maximum number of violations that are acceptable at this stage of our project. This is the role of the ReadLinesFromFile element, which reads the content of a file. As we added a condition to ascertain the existence of the violationsCount.txt file, it will only be executed if the file exists. We redirect the output to the property StyleCopMaxViolationCount.

After that we have configured the Jenkins job to follow our project with StyleCop. We have configured some strict rules to ensure nobody will add new violations over time, and with the violation plugin and the way we addressed StyleCop, we are able to follow the technical debt of the project regarding StyleCop violations in the Violations page.

A summary of each file is also present and if we click on one of them, we will be able to follow the violations of the file.

How to address multiple projects with their own StyleCop settings

As far as I know, this is the limit of the MSBuild StyleCop task. When I need to address multiple projects with their own settings, I generally switch to StyleCopCmd using NAnt or a simple batch script and process the stylecop-report.violations.xml file with an XSLT to get the number of violations.

Summary

This article talked about integrating StyleCop analysis in Jensons/Hudkins. This article helped in building a job analysis for our project.

Resources for Article :


Further resources on this subject:


Instant StyleCop Code Analysis How-to [Instant] Learn how to analyze and maintain code for your projects using StyleCop with this book and ebook
Published: May 2013
eBook Price: $12.99
See more
Select your format and quantity:

About the Author :


Franck Leveque

Franck LEVEQUE has studied computer science in Reims, France and has been in the software industry for more than 12 years. During this period he has worked on different projects using a variety of technology and development languages. Curious by nature, he follows a wide range of open source projects and has proposed bug fixes and features for some of them.

He currently works as the lead architect of the SamFM product for Planon Site Alpha. SamFM is a CMMS used for building management.

Books From Packt


C# 2008 and 2005 Threaded Programming: Beginner's Guide
C# 2008 and 2005 Threaded Programming: Beginner's Guide

C# 5 First Look
C# 5 First Look

GDI+ Application Custom Controls with Visual C# 2005
GDI+ Application Custom Controls with Visual C# 2005

 Mastering Windows 8 C++ App Development
Mastering Windows 8 C++ App Development

Boost.Asio C++ Network Programming
Boost.Asio C++ Network Programming

Reporting with Visual Studio and Crystal Reports
Reporting with Visual Studio and Crystal Reports

Refactoring with Microsoft Visual Studio 2010
Refactoring with Microsoft Visual Studio 2010

Visual Studio 2010 Best Practices
Visual Studio 2010 Best Practices


Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software