首页 > 解决方案 > 如何使用 Stylecop Analyzer 和自定义规则集创建 nuget 包?

问题描述

我需要使用 stylecop.json 和 something.ruleset 创建一个 nuget 包,并引用 stylecop 分析器。该软件包将在所有团队中使用,以标准化 .net 的规则。我读过这个:

但不幸的是,我没有正确理解如何实现这一目标。我有 stylecop.json 和 .ruleset 文件。我已完成以下步骤:

我可以知道现在需要做什么来创建包含 stylecop.json 和 .ruleset 文件的 nuget 包吗?

在此处输入图像描述

标签: c#.netvisual-studionugetstylecop

解决方案


我可以知道现在需要做什么来创建包含 stylecop.json 和 .ruleset 文件的 nuget 包吗?

您应该在文件中添加一些关于这些文件的节点xxx.nuspec,然后使用带有文件的nuget.exe clixxxx.nuspec来打包您的项目。

由于您已经生成了xxxx.nuspec文件,因此您应该将这些添加到 nuspec 文件中。

解决方案

1)您应该将这些节点添加到xxx.nuspec文件中,并使用它,这些文件将通过 nuget 添加到新项目中。

<?xml version="1.0" encoding="utf-8"?>
<package >
  <metadata>
    .......

<contentFiles>
<files include="any/any/stylecop.json "(the relativepath of the file under the ContentFiles folder in the xxx.nupkg) buildAction="None" copyToOutput="true" flatten="false" />
<files include="any/any/xxxx.Ruleset "(the relativepath of the file under the ContentFiles folder in the xxx.nupkg) buildAction="None" copyToOutput="true" flatten="false" />
</contentFiles>
  </metadata>

<files>
<file src="stylecop.json(the relativePath of the file under the project)" target="contentFiles/any/any" />
<file src="xxxx.Ruleset(the relativePath of the file under the project)" target="contentFiles/any/any" />
<file src="stylecop.json(the relativePath of the file under the project)" target="content" />
<file src="xxxx.Ruleset(the relativePath of the file under the project)" target="content" />
</files>

</package>

2)记得使用这个函数,你应该使用nuget.exe并且你应该从链接下载它,然后将它的路径设置为系统环境变量的PATH。例如,下载路径是C:\nuget\nuget.exe,你应该C:\nugetPATH中设置,然后你可以在CMD中调用nuget 。

另外,我上面说的修改xxxx.nuspec文件的时候,首先要cd到你的项目的路径,并检查该xxxx.csporj路径下是否存在该文件。

在此处输入图像描述

之后,您可以nuget pack在 CMD 中使用来打包您的项目,然后您将获得一个xxx.nupkgnuget 包。

在此处输入图像描述


推荐阅读