简体   繁体   English

StyleCop.Analyzers 集成到 GitLab

[英]StyleCop.Analyzers integration into GitLab

I have a C# project (.NET Core 3.1) and I use with it a nuget package StyleCop.Analyzers.我有一个 C# 项目(.NET Core 3.1),我使用了 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?现在我想知道是否可以将其检查集成到 GitLab CI piplene 中? I would like to run this analise after each build in GitLab.我想在 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.如果您通过在项目中引用 NuGet package 来使用来自StyleCop.Analyzers的代码分析,则代码分析将在编译(构建)期间执行。 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. 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:要正确配置代码分析,请将StyleCop.Analyzers package 引用添加到您的项目中:

  <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文件进一步配置StyleCop.Analyzers行为,例如选择退出一些烦人的样式规则:

<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.官方文档中阅读有关规则集的更多信息,或者查看我在 GitLab 上的库中使用的规则集文件以供参考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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