How to set stylecop.json creating Stylecop Anayzer

2019-09-14 20:09发布

问题:

I want to use the rules of the StyleCop analyzer for Roslyn in SonarQube, In order to do that and following the documentation founded, I downloaded the SonarQube Roslyn SDK, and I generate the plugin.

In my development environment I have a JSON file (stylecop.json) that add configuration:

{
  "$schema": "https://raw.githubusercontent.com/DotNetAnalyzers/StyleCopAnalyzers/master/StyleCop.Analyzers/StyleCop.Analyzers/Settings/stylecop.schema.json",
  "settings": {
    "documentationRules": {
      "companyName": "XXXXXX",
      "copyrightText": " My Copyright (c) ",
      "xmlHeader": true,
      "fileNamingConvention": "metadata"
    },
    "namingRules": {
      "allowedHungarianPrefixes": ["as", "do", "id", "if", "in", "is", "my", "no", "on", "to", "ui"]
    }
  }
}

This configuration works perfectly with VS2015, but when I run the analysis, the plugin does not get it, I am aware that I do not setup that file when I create the plugin. I am able to recreate the plugin, but how do I setup that JSON file for the plugin creation process?

回答1:

Roslyn analyzers can load parameters from files. This API is not too strict, meaning you can easily write an analyzer that requires a single file, multiple files, or whatever parameter loading that you can come up with. We haven't yet generalized this in the SonarQube Roslyn API, so there's no way to define additional files.

However, you can set up your projects to use your stylecop.json the same way as you'd do without SonarQube being in the picture. Then the SonarQube Scanner for MsBuild will pull down the analyzer DLLs from the SonarQube server, add them to your project during build, and with the latest version it will not clear the AdditionalFiles property, so your stylecop.json will be passed to the analyzers.