Geeks With Blogs

News profile for Aligned at Stack Overflow, Q&A for professional and enthusiast programmers

Donate Bitcoins

Check out Elapser from T3rse!
"free in Christ Jesus from the law of sin and death." Romans 8:2 (ESV) Check out the Falling Plates video on YouTube.
more about the Gospel
And then listen to Francis Chan speaking at LifeLight in SD.

Programming and Learning from SD

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).

    “One way:

    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
    7) Debug”
    ~ 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.

Posted on Wednesday, September 26, 2012 2:51 PM StyleCop | Back to top

Related Posts on Geeks With Blogs Matching Categories

Comments on this post: StyleCop Custom Rules

# re: StyleCop Custom Rules
Requesting Gravatar...
Thanks for this article, it was very helpful. How do you deploy your custom created StyleCop rules to a team? I wouldn't want to just send them the DLL and hope they follow through and place it in their C: \Program Files (x86)\StyleCop 4.7\
Left by Greg on May 22, 2014 10:31 AM

# re: StyleCop Custom Rules
Requesting Gravatar...
Greg, that was the best way I could find to deploy it. We only had 5 of us on that project. You could create a bat file for them to run or run on build in Visual Studio that could copy the dll from a network drive. That'd be a little easier.
Left by Kevin on May 22, 2014 1:11 PM

Your comment:
 (will show your gravatar)
 


Copyright © Aligned | Powered by: GeeksWithBlogs.net | Join free