How can I configure Roslyn Analyzers in many projects?
Asked Answered
B

4

22

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?

Blinding answered 30/7, 2019 at 14:54 Comment(0)
K
8

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

Kuhlman answered 30/7, 2019 at 20:5 Comment(0)
I
5

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.

Incorporeity answered 2/8, 2019 at 18:57 Comment(1)
This sounds like what I want but, I'd like an example or instructions on how to make that NuGet package.Blinding
P
2

Here's an example of a Directory.Build.props file that uses Microsoft.CodeAnalysis.NetAnalyzers. You can place this file anywhere, and it will have effect on all solutions/projects that can be found in the (sub)directories of the directory where you place the .props file.

<Project>
  <ItemGroup>
    <PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="6.0.0">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
  </ItemGroup>
</Project>
Pickett answered 9/3, 2022 at 15:58 Comment(0)
P
0
  1. For the existing projects you could create a script (powershell) and update all the projects such that the rules added as one time process.
  2. You can setup a Custom Project Template and include these rules as dependency and make it mandatory all the projects should be created using the newly created project template.
  3. Enable Gated Checkin and then like someone suggested above, you could add props / targets to "msbuild" on the build server and include in the default build targets. If the project does not have set standards fail the check in.
Peraea answered 5/8, 2019 at 20:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.