简体   繁体   中英

StyleCop.Analyzers integration into GitLab

I have a C# project (.NET Core 3.1) and I use with it a nuget package StyleCop.Analyzers. It analises my code during builds and shows various warnings if finds any problems with my code. Now I wonder is it possible to integrate its checks into GitLab CI piplene? I would like to run this analise after each build in GitLab. How do I do it?

"run this analise after each build"

If you use code analysis from StyleCop.Analyzers by referencing the NuGet package in your projects, then code analysis is performed during compilation (build) time. There is no need for analysis after each build, because at that moment the analysis already has been done - along with the build. Any errors caused by deviations from the styling rules that you can see in Visual Studio error list or CLI will also be present in GitLab CI pipeline output, as in the end they all are compiled by the same .NET SDK.

To properly configure code analysis add StyleCop.Analyzers package reference to your project/s:

  <ItemGroup>
    <PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
  </ItemGroup>

Additionally, you can further configure StyleCop.Analyzers behavior with .ruleset files to eg opt-out some annoying styling rules:

<RuleSet Name="Rules for ClassLibrary21" Description="" ToolsVersion="15.0">
    <Rules AnalyzerId="StyleCop.Analyzers" RuleNamespace="StyleCop.Analyzers">
        <Rule Id="SA0001" Action="None" />
    </Rules>
</RuleSet>

Rule set files have to be explicitly specified in project file settings:

  <PropertyGroup>
    <CodeAnalysisRuleSet>..\..\StyleCop.ruleset</CodeAnalysisRuleSet>
  </PropertyGroup>

Read more about rule sets in official documentation or have a look at the rule set file I use in my library on GitLab for reference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM