How to setup TFS builds to validate stylecop code standards? One way to implement using alteration to TFS build templates. Below few posts give good explanation on how to do this with TFS build template modification
Is there a way to do this without build template alteration? Of course yes, thanks to StyleCop.MSBuild by Adam Ralph this is possible now (Brief introduction on Stylecop.msbuild here).
Let’s have a look at step by step how to setup stylecop with TFS builds.
Right click on the Visual Studio solution Go to “Manage NuGet Packages for Solution”.
Search for stylecop. Stylecop.MSBuild will be in the results.
Click on Install and it will download StyleCop.MSBuild.
Select available projects in the solution and click OK. If new projects added to solution they can be selected by going to “Manage NuGet Packages for Solution”. Click on Manage installed package and select the new projects available.
SyleCop.MSBuild will be installed for selected projects.
This will add packages folder to the solution.
Right click on packages folder and undo pending changes. Undo changes will not remove the package folder from local folder and will be available to for the solution in this machine.
This will prevent packages folder and contents being checked in to TFS. It is possible to set this package to automatically downloaded when the solution built.
A file packages.config will be added to each project in the solution and make sure to check this in.
Project file (.csproj) will be modified to add required configurations for StyleCop.
Right click on the solution and enable nuget package restore for the solution. This will allow other team members to automatically download NuGet packages of the solution when it is built.
This will add few files to solution and change the .csproj files.
When the project is built the StyleCop violations are shown.
After fixing all violations in code still three violations shown based on temporary files generated when building .Net framework 4.5 projects.
Below link suggests few ways to fix this issue.
Out of them best solution is adding option to ignore the temporary files in the .csproj file. Since .csproj file will be checked in this fix will be available for other team members and build agents.
Unload project and edit to change the .csproj file.
<ExcludeFromStyleCop Include="$(IntermediateOutputPath)\**\*.cs" >
as shown below inside the StyleCop section added by the StyleCop.MSBuild installation.
Now all violations are fixed.
To verify StyleCop violations still validated after the above fix for temporary files, purposefully create a violation and verify.
Fix the violation and check in the solution to TFS. A CI build using default build template runs successfully.
To verify TFS build gives warning when there is a StyleCop violation purposefully create a violation and check in.
Local build in VS show violation.
CI build shows SA1515 violation as a warning.
To make this violation to fails the build, add below to .csproj StyeCop added property group.
This will make the violation to fail the build locally.
CI Build also fails with StyleCop violation.
SuppressMessage can be added to code (Method or Class level) and works fine with local builds and TFS builds.
Using StyleCop.MSBuild it is possible to run StyleCop with TFS builds without altering build templates and without installing StyleCop in TFS Build Agents.