Code 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.
StyleCop - I installed Version 22.214.171.124 RTW.
The Installation-directory for StyleCop:
C:\Program Files (x86)\Microsoft StyleCop 126.96.36.199
During the installation the Build Files have to be installed too:
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 188.8.131.52".
Now we need to copy some files from both folders.
That´s what my solution looks like:
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:
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.
Two more links about the subject: