Monday, 21 May 2012

Running StyleCop for all projects in Visual Web Developer 2010 Express

In my previous post, I hinted at a method of getting StyleCop to run under the free versions of Visual Studio, particularly Visual Studio 2010 Express and Visual Web Developer. Getting this tool to run is remarkably simple, although there are some differences between running under this environment and running under the paid for Visual Studio editions.

Although these differences mean that StyleCop does not seem as embedded in Visual Studio as previous environments that I have worked with, I have become used to working in a certain way, and it no longer bothers me that my work environment is a little different to my home working environment.

What is StyleCop

For those who are not already using this fantastic tool, StyleCop is a Visual Studio plugin which enforces certain rules against how code is formatted, code location, and where and when documentation headers are used. Of course, if you have never used StyleCop before, this doesn't seem like that much of a big deal, and can even seem like a waste of time. I fully admit that, when I first came to using the tool a few years ago, I was a little sceptical at first. Implementing the rules became a little bit of a hassle, especially as I always left the enhancements until last thing, resulting in potentially hundreds of "errors". It wasn't until I moved to a new company that had never used the tool that I realised how much easier code that has followed the rules is to pick up and find your way around. It is true to say that StyleCop will probably not enhance any of your actual code, but if it means that you need to think about what a method does in order to decorate it with a documentation header, then that can't be a bad thing, can it?!

Installing StyleCop

Back to the main purpose of this post, how to go about installing StyleCop on a Visual Studio Express environment. First things first, you will need to download and install the MSI, which can be found on the CodePlex website. Once the installation has finished, you will need to find the .Targets file. This should be located in the MSBuild folder C:\Program Files (x86)\MSBuild\StyleCop\v4.7\StyleCop.Targets. Move this file to the following folder (you may need to create this folder if it doesn't already exist!) C:\Program Files\MSBuild\4.0\Microsoft.CSharp.targets\ImportAfter\.

That's it, it really is that simple. You will need to restart Visual Studio, but from now on, every time you build a project, StyleCop will run.

Happy Coding :0)

1 comment:

  1. After installing StyleCop and copying the file, there is nothing different during the build/debug output. Nothing at all. Any troubleshooting tips?