【问题标题】:Providing a code analysis ruleset to a .net core project through NuGet通过 NuGet 向 .net 核心项目提供代码分析规则集
【发布时间】:2018-02-17 07:03:50
【问题描述】:

我在将 .NET 4.6 项目升级到 .NET Core 2.0 时遇到了问题。我们所有的项目都使用由 NuGet 包提供的自定义 StyleCop 规则集。规则集位于名为 custom.ruleset 的文件中,位于包内的内容文件夹中。我们所有的项目都使用这个包,因此得到一个 custom.ruleset 的副本。

但是,在 Core 2.0 和 Standard 2.0 项目中,这不起作用。文件不再从包的内容文件夹中复制,而是被告知使用 contentFiles 文件夹。

我有一个 nuspec,现在看起来像这样:

<?xml version="1.0" encoding="utf-8" ?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
  <version>1.0.11</version>      
  <metadata>
    ...
    <contentFiles>
      <files include="content\*.ruleset" buildAction="None" copyToOutput="false" flatten="true"/>
    </contentFiles>
  </metadata>
  <files>
    <file src="content\**" target="contentFiles/any/any" />
  </files>
</package>

使用此结构,规则集出现在项目下的 Visual Studio 中,但尝试使用 &lt;CodeAnalysisRuleSet&gt;custom.ruleset&lt;/CodeAnalysisRuleSet&gt; 从项目的 .csproj 文件中引用它会静默失败并恢复为使用默认规则集。我可以通过添加&lt;CodeAnalysisRuleSet&gt;$(NuGetPackageRoot)CustomRuleset\1.0.11\contentFiles\any\any\custom.ruleset&lt;/CodeAnalysisRuleSet&gt; 来强制它工作,但这意味着只要规则集发生变化,csproj 就需要更新,所以它也可能是一个手动过程。任何想法如何解决这个问题?

【问题讨论】:

    标签: c# nuget .net-core stylecop


    【解决方案1】:

    我们的想法是不要尝试将文件部署为内容,而是将构建逻辑添加到 NuGet 包。

    确保包的结构如下:

    • 构建\
    • build\custom.ruleset
    • build\{YourPackageName}.targets(例如CustomRuleset.targets

    这种结构会导致 .targets 文件按照约定自动导入到使用项目中。

    .targets 文件应包含:

    <Project>
      <PropertyGroup>
        <CodeAnalysisRuleSet>$(MSBuildThisFileDirectory)custom.ruleset</CodeAnalysisRuleSet>
      </PropertyGroup>
    </Project>
    

    这将导致项目的规则集属性被覆盖到相对于 .targets 文件的位置。

    请注意,这也适用于使用新的 PackageReference 样式的 NuGet 包(替换 packages.config)的 .net 框架项目,它在 VS 2017 (15.2+) 中是可选的。

    【讨论】:

    • 对我来说也很有效,但我很好奇在build 文件夹中添加不是.targets.props 的文件的能力是否记录在任何地方?
    猜你喜欢
    • 2013-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-13
    • 1970-01-01
    • 2021-10-25
    • 2018-06-02
    • 2017-08-18
    相关资源
    最近更新 更多