There are several blogs on how to do this (http://scottwhite.blogspot.com/2008/11/creating-custom-stylecop-rules-in-c.html, etc). I’ve found a few useful things to point out:
- Debugging is difficult, but here are the steps (thanks to Tintin’s answer).
1) Delete your custom rules
2) Open Visual Studio (for dev), open your custom rule solution
3) Build & Deploy custom rules (a PostBuild action to copy the rules into the StyleCop folder is handy)
4) Open Visual Studio (for test)
5) Use VS (dev) and Attach to process devenv.exe (the test VS instance), set breakpoints in the rules you want to debug
6) Use VS’ (test) and right-click on project, Run StyleCop
~ it worked once, now I’m having problems getting it to work again
~ I also get the message “Cannot evaluate expression because the code of the current method is optimized.” when I try to look at properties.
~ I found a post on writing the rules with unit tests that would make developing these much easier, but the description is incomplete.
~ http://stylecopcontrib.codeplex.com/ has unit tests and more rules than the default implementation ~http://shishkin.wordpress.com/2008/05/30/stylecop-test-driven-rules-development/ ~this guy wrote the stylecopcontrib. Checkout the comments at the bottom of the post. It got me closer, but I’m still not hitting any breakpoints in my rules code (AnalyzeDocument in particular). Anyone know why??
He has a StyleCopContrib 4.3 zip file on his SkyDrive that might be more up-to-date than the codeplex lnk, however with StyleCop 4.7 installed it’s going to take more than changing the usings from Microsoft.StyleCop to just StyleCop.
http://geekswithblogs.net/thomasweller/archive/2009/11/28/unit-testing-stylecop-rules.aspx talks about using the shishkin examples.
Looking at the source code of the StyleCop.CSharp.Rules.dll that comes with the install. I used JustDecompile from Telerik.
- Create one xml file and name it the same as the one cs file (CodingGuildelineRules.cs and CodingGuidelinRules.xml)
The http://stylecopplus.codeplex.com/ project looks interesting, but I haven’t tried it out.
Unit Testing Solution:
I finally got it working!! I had the path incorrect in the AddSourceCode method of SourceAnalysisTest. It was fileName = Path.GetFullPath("Resources/" + fileName); That wasn’t working because it was looking in the bin/debug folder. I now see that his test files (which I had in the Tests/Resources directory) build action is set to content, not compile.
I've uploaded my solution. This was created with StyleCop 4.7. Hopefully, it will save you from all the digging I had to do to figure this out. The rules aren't the best yet (especially the PascalCase checking).
To Deploy (to be able to run in Visual Studio):
1. Build in Visual Studio
2. Close Visual Studio (Style cop is running so you can’t override your dll without closing)
3. Copy the dll from the bin to the C: \Program Files (x86)\StyleCop 4.7\
4. Open the settings file or re-open Visual Studio
To share StyleCop Settings to the rest of the team:
You can edit the settings of StyleCop for everyone, by first right-clicking a project and clicking SyleCop settings. This will create a StyleCop.Settings file in the project directory. Copy and paste this file into the root folder of your solution and check that file into source control. Then when you need to disable a rule or add a known word or spelling, open up Windows Explorer, navigate to the file, check it out from source control, and double-click the file.