通过 NuGet 为 .net 核心项目提供代码分析规则集 [英] Providing a code analysis ruleset to a .net core project through NuGet

查看:28
本文介绍了通过 NuGet 为 .net 核心项目提供代码分析规则集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've run into a problem when upgrading a .NET 4.6 project to .NET Core 2.0. All our projects use a custom StyleCop ruleset which is provided by a NuGet package. The ruleset is in a file called custom.ruleset and lives in the content folder inside the package. All our projects consume this package and so get a copy of custom.ruleset.

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

However, in Core 2.0 and Standard 2.0 projects this doesn't work. Files are no longer copied from the content folder of a package, and we're told to use the contentFiles folder instead.

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

I have a nuspec that now looks like this:

<?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 中,但尝试从项目的 .csproj 文件中使用 custom.ruleset</CodeAnalysisRuleSet> 引用它,却静默失败并恢复使用默认规则集.我可以通过添加 <CodeAnalysisRuleSet>$(NuGetPackageRoot)CustomRuleset1.0.11contentFilesanyanycustom.ruleset</CodeAnalysisRuleSet> 来强制它工作,但这意味着 csproj 需要更新每当规则集发生变化时,它也可能是一个手动过程.任何想法如何解决这个问题?

With this structure the ruleset appears in Visual Studio under the project, but trying to reference it from the project's .csproj file with <CodeAnalysisRuleSet>custom.ruleset</CodeAnalysisRuleSet> silently fails and reverts to using the default ruleset. I can force it to work by adding <CodeAnalysisRuleSet>$(NuGetPackageRoot)CustomRuleset1.0.11contentFilesanyanycustom.ruleset</CodeAnalysisRuleSet> but this means the csproj will need updating whenever the ruleset changes, so it may as well be a manual process. Any ideas how to fix this?

推荐答案

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

The idea is to not try to deploy the file as content but add build logic to the NuGet package.

确保包的结构如下:

  • 构建
  • buildcustom.ruleset
  • build{YourPackageName}.targets(例如 CustomRuleset.targets)

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

This structure causes the .targets file to be automatically imported into the consuming project by convention.

.targets 文件应该包含:

The .targets file should then contain:

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

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

This will cause the project's ruleset property to be overwritten to the location relative to the .targets file.

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

Note that this also applies to .net framework projects using the new PackageReference style of NuGet packages (replacement of packages.config) which is opt-in in VS 2017 (15.2+).

这篇关于通过 NuGet 为 .net 核心项目提供代码分析规则集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆