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?
You can use Directory.Build.props
and Directory.Build.targets
to add properties and targets to your projects.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With