Xamarin + StyleCop in Visual Studio for Mac

StyleCop analyses C# source code to enforce a set of style and consistency rules. It is available in two primary forms:

  • The StyleCop Visual Studio extension, which allows StyleCop analysis to be run on any file, project, or solution in Visual Studio without modifying the source code. Visual Studio 2010, 2012, 2013, 2015, and 2017 are supported by this extension.
  • The StyleCop.MSBuild NuGet package, which allows StyleCop analysis to be added to any .NET 4.0+ project without installing anything else on the system.

This article will be focusing on how to add StyleCop.MSBuild into Xamarin project.

Note: The Roslyn-based StyleCopAnalyzers project is recommended for developers who use only Visual Studio 2015 or later. However, Roslyn-based packages is not supported in Visual Studio for Mac. Let’s wait for Xamarin team to support that in future.

The source code that is use for the following guide can be found HERE.

Setup

Current latest version of StyleCop.MSBuild is 5.0.0 which support C# 6 syntax. Add the package with NuGet package manager to the projects.

Install-Package StyleCop.MSBuild -Version 5.0.0

Screen Shot 2017-08-26 at 8.01.24 AM.png

After added the package, your project is now ready with StyleCop. We can test it out by start building the project. After build complete, you should see some warnings show in Errors Pad trigger by StyleCop.

Screen Shot 2017-08-26 at 8.04.23 AM.png

Modify StyleCop Settings

By default, if we did not supply with any settings file, StyleCop will analyse using its own default settings. The default settings file is located in

 packages\StyleCop.MSBuild.{version}\tools\Settings.StyleCop

To edit the Settings file, we can manually open it in text editor and add in our rules. However, this will be too troublesome to remember the syntax and format of the StyleCop settings. Luckily, StyleCop ship together with a Settings Editor application. It is located in:

packages\StyleCop.MSBuild.{version}\tools\StyleCop.SettingsEditor.exe

Unfortunately, we cannot open this editor in Mac, not even with the help of mono. So, we will need to open it in Windows environment. Copy the files to Windows and execute the editor using command prompt with the following command:

StyleCop.SettingsEditor.exe [path-to-stylecop-settings-file]

After that, you can edit the settings in the GUI editor and click OK to save the file after you have done.

stylecop_settings_editor.png

Copy the Settings.StyleCop file back to your project root folder in Mac. Putting the settings file in the root of solutions folder will make the settings inherited by all projects. This file can then kept in Source Control and can be share to all developers in your project.

Cache

After you build the project and performed code analysis, StyleCop will create a cache file in each of the project folder (StyleCop.Cache). The next time StyleCop is run on the same project or file, the tool will determine whether any changes have occurred within the file since the last analysis. If not, the previous results are read from the cache. For larger files and projects, this can improve the overall performance of StyleCop.

Sometimes, you might prefer not storing the cache and run the analysis every time. In this case, you can go to StyleCop settings editor to turn the Cache off.

2017-08-26 23_15_47-StyleCop 5.0 (5.0.6329.1) Project Settings - C__SVN_fbp_trunk_src_StyleCop_Setti.png

Suppress Warnings

Sometimes we might want to suppress some warnings in some part of the code without turn off the rules. The following is example on how to suppress StyleCop warnings:

// Naming rules
[SuppressMessage("StyleCop.CSharp.NamingRules""SA1307:AccessibleFieldsMustBeginWithUpperCaseLetter", Justification = "Reviewed")]

// Documentation rules
[SuppressMessage("StyleCop.CSharp.DocumentationRules""SA1601:PartialElementsMustBeDocumented", Justification = "Reviewed")]

 

 

5 thoughts on “Xamarin + StyleCop in Visual Studio for Mac”

  1. I’ve not managed to be able to add the nuget package using your project. Are you still able to do this in the latest version of visual studio?

    Like

      1. “Could not install package ‘StyleCop 5.0.0’. You are trying to install this package into a project that targets ‘.NETPortable,Version=v4.5,Profile=Profile111’, but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.”

        Like

Leave a comment