HowTo: MSBuild & StyleCop

 

imageCode Quality is a big issue. StyleCop is a tool from Microsoft (an open Source Tool btw.) to analyse the Source Code. In contrast to FxCop or Code Analysis from VSTS it controls the code for observation the Codeing Conventions etc. Here a blogpost to show you the diference. Anyway it´s quite easy to integrate StyleCop into your MSBuild and use it this way in your Build Process.

 

Conditions

StyleCop – I installed Version 4.4.0.14 RTW.

The Installation-directory for StyleCop:

C:\Program Files (x86)\Microsoft StyleCop 4.4.0.14

During the installation the Build Files have to be installed too:

image

You have the opportunity to open Stylecop in MSBuild without any other tools but in my opinion that´s not the classy way and you don´t have so many options. Take a look on this post from the StyleCop Team.

It´s more interesting if you use MSBuild.Extension.Pack

I´ve downloaded MSBuild Extension Pack August 2010 Files. In this Zip directory we only use the “MSBuild.ExtensionPack.Binaries 4.0.1.0″.

Our Demoapplication

Now we need to copy some files from both folders.

That´s what my solution looks like:

image

 

In the folder “Lib” you will find 3 dlls from the StyleCop folder and the other 2 Files are from the ExtensionPack,Binaries folder. Please copy these files into one directory.

 

 

The Settings.StyleCop file, where the “Rules” for StyleCop are included, are able to be generated with Visual Studio:

image

Click on “run StyleCop” and the StyleCop file is passed to the project directory.

Let´s talk about MSBuild

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Measure">
  <!--<Import Project="$(MSBuildStartupDirectory)\Lib\MSBuild.ExtensionPack.tasks"/>-->
  <UsingTask AssemblyFile="$(MSBuildStartupDirectory)\Lib\MSBuild.ExtensionPack.StyleCop.dll" TaskName="MSBuild.ExtensionPack.CodeQuality.StyleCop"/>
  <PropertyGroup>
    <OutDir>$(MSBuildStartupDirectory)</OutDir>
  </PropertyGroup>
  <Target Name="Measure">
    <Message Text="Measure called." />

    <CreateItem Include="$(MSBuildStartupDirectory)\**\*.cs">
      <Output TaskParameter="Include" ItemName="StyleCopFiles"/>
    </CreateItem>

    <MSBuild.ExtensionPack.CodeQuality.StyleCop
          TaskAction="Scan"
          ShowOutput="true"
          ForceFullAnalysis="true"
          CacheResults="false"
          SourceFiles="@(StyleCopFiles)"
          logFile="$(OutDir)\StyleCopLog.txt"
          SettingsFile="$(MSBuildStartupDirectory)\Settings.StyleCop"
          ContinueOnError="false">
          <Output TaskParameter="Succeeded" PropertyName="AllPassed"/>
          <Output TaskParameter="ViolationCount" PropertyName="Violations"/>
          <Output TaskParameter="FailedFiles" ItemName="Failures"/>
    </MSBuild.ExtensionPack.CodeQuality.StyleCop>
    <Message Text="Succeeded: $(AllPassed), Violations: $(Violations)" />
  </Target>

</Project>

There is a Target “Measure” in the MsBuild File and we are going to import the MSBuild Extension Pack File. After this we copy all .cs files from line 10. At least we open the MSBuild Extension Pack Stylecop.

For return there is a bool named “Succeded” and a counter. With the help of a .bat file we open the MSBuild file.

As result we have a logfile+XML file with all the warnings. It´s easy to attend this file afterwards.

Actually I just tested this “local” but there exist integrations in Hudson and TFS.

Two more links about the subject:

Forumlink

Blogpost

 

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

About the author

Written by Code Inside Team

Learn more about our team.

2 Responses

Comment on this post

Recent Posts

  • Automated Security Analyser for ASP.NET websites

    Evil Hackers are lurking everywhere and many Web-applications are delicately and share “too much” with the attacker. A quick (first!) overview offers the Tool “ASafaWeb”. All the website does is making a few requests and writing an Analyses including problem solving’s. There are no permanent disadvantages (bad requests/ DoS attacks and so on). Example: KnowYourStack.com ...

  • image1489-570x194.png
    „Sign in with Twitter“ for your own ASP.NET WebApp

      “Sign in with Twitter” is a popular practice to authenticate the users on your website. One advantage compared to an own registration is the lower inhibition for the user. But on the other hand Twitter doesn’t fess up with all the information’s and you will get into a kind of addiction. At the end ...

  • image1485-570x194_thumb.png
    CodePlex is going to be updated

      CodePlex the Microsoft Open Source Project Hosting Plattform hasn’t changed that much in the last few years and for a few times I thought Microsoft stopped the whole developing process. But now I found out that there is still life in the project. Maybe it is because of the success of GitHub or because ...

  • image1474_thumb.png
    What does Adobe in the flash-free web? Magazine-Style Layouts with CSS Regions!

      Adobe is well known for Photoshop and Flash but of course there is a lot more. According to the “Future Post” from Google Adobe declared one of their big subjects on a Blogpost. I’m talking about the W3C Working Draft to CSS Regions. Adobe cooperates with the WebKit Team and W3C on this. What ...

  • image1471-523x194.png
    HTML 5 Games, Tooling & 3D

      Game Developing is an interesting subject for all kind of software developer. But as a web developer without any Flash-skills there aren’t that much starting points. With HTML5 and the combination between Javascript, CSS3 and fast browsers there are the first “robust” HTML5 games. HTML5 games? Is this real? Neowin created a “Top 10” ...

Support us