Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I configure Roslyn Analyzers in many projects?

I want to enforce code quality and consistent styling in my organization.

To do this I plan to add Roslyn Analyzers and StyleCop to my projects.

In order to meet with our agreed coding standards, these analyzers will need additional configuration. Ideally they will be configured using .editorconfig like this or, failing that, using rule sets.

At my organization, we have many projects, in many solutions, in many repositories. I want to enforce these standards as broadly as possible. I don't want to have to add all the analyzer packages and configuration to every project, is there a better, easier, more easily consistent way to achieve this?

I have an idea that I could make a NuGet Package, for my organization, that incorporates my organization's selected Analyers, any configuration and indeed, any custom Analyzers that may be created. This "bundling" package could be added to every project, avoiding the tedious and error prone repetition. Is this possible, is it even a good idea? Has anybody else done this?

like image 693
Jodrell Avatar asked Jul 30 '19 14:07

Jodrell


2 Answers

You can use Directory.Build.props and Directory.Build.targets to add properties and targets to your projects.

like image 116
Paulo Morgado Avatar answered Oct 24 '22 15:10

Paulo Morgado


There are a few things you can do

  • Add a NuGet package to the project (this is what we do at our workplace), we have added StyleCop and FxCop with our customized setting on top of it. It is working. Distribution is very easy in this case.

  • Add Directory.Build.props and Directory.Build.targets as @Paulo Morgado mentioned - they are configurable at a project level and solution level

  • You can use editorConfig also. It is being widely adopted. Both FxCop and StyleCop work very well with editorConfig. EditorConfig can also be packaged in Nuget and distributed. Integration FXCop with EditorConfig.

For instance, to create a nuget package, create a .netstandard library with .nuspec file, the most basic example is

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
  <metadata>

    <dependencies>
      <dependency id="StyleCop.Analyzers" version=""/>
      <dependency id="Microsoft.CodeAnalysis.FxCopAnalyzers" version=""/>
    </dependencies>
  </metadata>

</package>

And of course, you can include your customised coding rules and include them also.

like image 20
jeevjyot singh chhabda Avatar answered Oct 24 '22 14:10

jeevjyot singh chhabda