Monday, 27 August 2012

OS X - Running StyleCop in MonoDevelop on C# code

When developing on Windows I find StyleCop to be a really useful tool for keeping code consistent throughout projects and I sorely miss it when developing C# projects on OS X with MonoDevelop.

I knew about a StyleCop addin for MonoDevelop here but at the time of writing it's not compatible with the latest version of MonoDevelop (3.0.3.5).

Time to get my hands dirty then! I forked the project on GitHub and modified it so that it works with MonoDevelop 3.0.3.5, the repository is here.

Upgrading the project was relatively easy, just tweaking a few version numbers in the addin manifest and reset the references. I also adjusted the StyleCop violations to output as Warnings rather that Errors, this suited the way I like to see the violations.

Getting the addin to run is another matter though - StyleCop uses the registry. Mono has a registry implementation but I found there were security issues involved with non admin processed reading and writing to it so in order to install the addin a little tweak to the registry permissions was required:

Navigate to the registry file:

On my machine it was here:

/Library/Frameworks/Mono.framework/Versions/2.10.9/etc/mono/registry/

Then adjust the security:

sudo chmod 777 last-btime

Here is a screen shot of it running:




Download and build from here:

https://github.com/mattlaver/Stylecop-Monodevelop-Addin

7 comments:

  1. Hi,

    I've tried to get this working on my system but everytime I try to run StyleCop on a file I get an error.

    I copied the compiled MonoDevelop.StyleCop.dll with the files in dll directory into the same directory and named it MonoDevelop.StyleCop

    Monodevelop always throws the following error:
    System.Reflection.ReflectionTypeLoadException: The classes in the module cannot be loaded.

    I guess I'm missing something.. I first guessed, that it has something to do with the registry file but I set the rights on it as you adviced but I still get the same error.

    ReplyDelete
  2. Ahh, I shouldn't have checked in the bin directory as I've not checked in all the binaries. You need to build the project yourself, then copy across all the dlls that are in the bin directory.

    You may need to correct the references to the mono dlls, let me know if you have issues and I'll update the blog post with some pointers.

    ReplyDelete
  3. I think I found my problem, I always copied all the dlls of /dll into the MonoDevelop.StyleCop Addin directory as MonoDevelop has always thrown a "System.IO.FileNotFoundException: Could not load file or assembly" exception.
    I had to remove all files except the following:
    Microsoft.StyleCop.CSharp.dll
    Microsoft.StyleCop.CSharp.Rules.dll
    Microsoft.StyleCop.dll
    MonoDevelop.StyleCop.dll

    I guess the Reflection error came from the Microsoft.StyleCop.VSPackage.dll cause as soon as I removed that dll I had no problems anymore. I just reduced the necessary file to the ones listed above.

    I also had to start MonoDevelop once as admin and execute StyleCop so Mono creates the registry file.

    Now I can run the command without an error. Thanks :)

    ReplyDelete
  4. Hi,

    I'm not sure if you're still interested in this but I've forked the plugin and completely rewrote it.
    You can download it from the Downloads section here:
    https://github.com/DarkCloud14/MonoDevelop.StyleCop

    Please take a look at the Remarks in the README as the plugin won't work otherwise and you'll always get errors and can't use MonoDevelop anymore.

    ReplyDelete
    Replies
    1. I'm definitely interested, especially if you've integrated the latest version of StyleCop! Not being able to use MonoDevelop is not great thought so once I've figured out how to set the paths correctly I'll give it a try. Thanks for letting me know about this.

      Delete
    2. No problem :) What do you mean with "once I've figured out how to set the paths correctly" ? Can I help you with something or is there something I didn't mention on the Installation remarks for operating systems other than Windows?

      Delete
    3. Oh you don't have to build it yourself. I've finally managed to get it build on addins.monodevelop.com and you can get that package here https://github.com/DarkCloud14/MonoDevelop.StyleCop/downloads
      Later official releases will go over addins.monodevelop.com but at the moment it's not possible to download it from there. They need to approve the package first I guess.

      Delete