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

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

问题描述

将.NET 4.6项目升级到.NET Core 2.0时遇到问题。我们所有的项目都使用NuGet包提供的自定义StyleCop规则集。规则集位于一个名为custom.ruleset的文件中,并且位于包内的content文件夹中。我们所有的项目都使用此包,因此获得了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>
<元数据>
...
< 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中,但是尝试从项目的引用中引用它。带有< CodeAnalysisRuleSet> custom.ruleset< / CodeAnalysisRuleSet> 的.csproj文件会自动失败并恢复为使用默认规则集。我可以通过添加< CodeAnalysisRuleSet> $(NuGetPackageRoot)CustomRuleset\1.0.11\contentFiles\any\any\custom.ruleset< / CodeAnalysisRuleSet> ,但这意味着每当规则集更改时,csproj都需要更新,因此也可能是手动过程。有什么想法可以解决此问题吗?

解决方案

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



请确保该软件包的结构如下:




  • build\

  • build\custom.ruleset

  • build\ {YourPackageName} .targets(例如 CustomRuleset .targets



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



.targets文件应包含:

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

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

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


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.

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.

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>

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)CustomRuleset\1.0.11\contentFiles\any\any\custom.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?

解决方案

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

Make sure that the package is structured in the following way:

  • build\
  • build\custom.ruleset
  • build\{YourPackageName}.targets (e.g. CustomRuleset.targets)

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

The .targets file should then contain:

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

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

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天全站免登陆