如何在 .net 标准项目中通过 nuget 获取 stylecop 规则集

joe*_*oey 7 .net c# stylecop nuget .net-standard

我们正试图为我们所有的项目提供一个带有 stylecop 规则集的 nuget 包。我们获取了项目中的文件,但规则集不适用于我们的项目。它仍然使用 minimimumrecomended.ruleset。

我们现在拥有的是:

custom.stylecop.props

<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <RunCodeAnalysis>true</RunCodeAnalysis>
    <CodeAnalysisRuleSet>Custom.StyleCop.ruleset</CodeAnalysisRuleSet>
  </PropertyGroup>
</Project>
Run Code Online (Sandbox Code Playgroud)

custom.stylecop.targets

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <AdditionalFiles Include="$(MSBuildThisFileDirectory)\Content\stylecop.json">
      <Link>stylecop.json</Link>
    </AdditionalFiles>
  </ItemGroup>
</Project>
Run Code Online (Sandbox Code Playgroud)

custom.stylecop.nuspec

<contentFiles>
    <files include="Content/stylecop.json" buildAction="EmbeddedResource" />
</contentFiles>
....
<files>
    <file src="build\**" target="build" />
    <file src="Content/stylecop.json" target="contentFiles" />
</files>
Run Code Online (Sandbox Code Playgroud)

有没有人在 github 上有任何想法或示例,我们可以在那里找到示例,因为我们找不到任何示例。

joe*_*oey 5

我们通过以下方式解决了这个问题:

代码分析.props

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <CodeAnalysisRuleSet>$(MSBuildThisFileDirectory)..\CodeAnalysis.ruleset</CodeAnalysisRuleSet>
  </PropertyGroup>
  <ItemGroup>
    <AdditionalFiles Include="$(MSBuildThisFileDirectory)..\stylecop.json" />
  </ItemGroup>
</Project>
Run Code Online (Sandbox Code Playgroud)

代码分析.nuspec

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
    <metadata>
        <id>CodeAnalysis</id>
        <version>1.0.0</version>
        <description>Roslyn analyzers, rule sets and additional configuration to be used for Code Analysis</description>
        <authors></authors>
        <owners></owners>

        <dependencies>
          <dependency id="Stylecop.Analyzers" version="1.0.2" />
        </dependencies>
    </metadata>
    <files>
      <file src="stylecop.json" />
      <file src="CodeAnalysis.ruleset" />
      <file src="CodeAnalysis.props" target="build" />
    </files>
</package>
Run Code Online (Sandbox Code Playgroud)