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
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.
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...
- 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.
<UsingTask TaskName="StyleCopTask" AssemblyFile="$(MSBuildExtens
ionsPath)\..\StyleCop 4.7\StyleCop.dll" />
<!-- Set a default value of 1000000 as maximum Stylecop
violations found -->
<!-- Get last violation count from file if exists -->
<Output TaskParameter="Lines" PropertyName="StyleCopMaxViola
<!-- Create a collection of files to scan -->
<Output TaskParameter="Include" ItemName="StyleCopFiles" />
<!-- Launch Stylecop task itself -->
<!-- Set the returned number of violation -->
<Output TaskParameter="ViolationCount" PropertyName="StyleCo
<!-- Write number of violation founds in last build -->
<WriteLinesToFile File="violationCount.txt" Lines="$(StyleCopV
iolationCount)" Overwrite="true" />
- 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.
- 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.
- 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
- 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.
This article talked about integrating StyleCop analysis in Jensons/Hudkins. This article helped in building a job analysis for our project.
Resources for Article :
- Organizing, Clarifying and Communicating the R Data Analyses [Article]
- Generating Reports in Notebooks in RStudio [Article]
- Data Profiling with IBM Information Analyzer [Article]
|Learn how to analyze and maintain code for your projects using StyleCop with this book and ebook|
eBook Price: $12.99
About the Author :
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.