关于 c#:How to get a stylecop ruleset trough nuget in a .net standard project | 珊瑚贝

How to get a stylecop ruleset trough nuget in a .net standard project


我们正在尝试为我们所有的项目提供一个带有 stylecop 规则集的 nuget 包。
我们获得了项目中的文件,但规则集并未应用于我们的项目。它仍然使用 minimimumrecomended.ruleset。

我们现在拥有的是:

Custom.stylecop.props

1
2
3
4
5
6
<Project ToolsVersion=“4.0” xmlns=“http://schemas.microsoft.com/developer/msbuild/2003”>
  <PropertyGroup>
    <RunCodeAnalysis>true</RunCodeAnalysis>
    <CodeAnalysisRuleSet>Custom.StyleCop.ruleset</CodeAnalysisRuleSet>
  </PropertyGroup>
</Project>

custom.stylecop.targets

1
2
3
4
5
6
7
8
<?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>

Custom.stylecop.nuspec

1
2
3
4
5
6
7
8
<contentFiles>
    <files include=“Content/stylecop.json” buildAction=“EmbeddedResource” />
</contentFiles>
….
<files>
    <file src=“build\\**” target=“build” />
    <file src=“Content/stylecop.json” target=“contentFiles” />
</files>

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

  • 不幸的是,我们暂时停止了该项目,并计划在 1 到 2 个月内重新投入时间。当我们找到一个工作解决方案时,我会在这里发布。
  • 乔伊,很抱歉删除了我之前的评论。在下面找到了这个现在对我有用的解决方案。谢谢你这么快回来!
  • 乔伊,当你有机会时,你能试试看下面的回复是否能解决你的问题吗?谢谢
  • 我星期四要回去工作我会测试它是否有效


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

代码分析.props

1
2
3
4
5
6
7
8
9
<?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>

代码分析.nuspec

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?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>
        <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>


在我们的 Xamarin.Forms 项目中,修复是手动编辑 .NET Standard .csproj 文件的过程。如果有更好的方法,请告诉我!

  • 首先,在文本编辑器 (_Notepad) 中打开您的 .NET Standard 项目
  • 然后在 <PropertyGroup> 中添加 <CodeAnalysisRuleSet> … </CodeAnalysisRuleSet>。
  • 1
    2
    3
    <PropertyGroup>
        <CodeAnalysisRuleSet>..\\stylecop.ruleset</CodeAnalysisRuleSet>
    </PropertyGroup>
  • (可选)如果你想在你的项目中访问这些文件,你可以使用 <AdditionalFiles … /> 创建一个 ItemGroup。
  • 1
    2
    3
    4
    <ItemGroup>
        <AdditionalFiles Include=“..\\stylecop.json” />
        <AdditionalFiles Include=“..\\stylecop.ruleset” />
    </ItemGroup>

    参考

    https://github.com/dotnet/roslyn/blob/master/docs/compilers/Rule Set Format.md

    • 这是适用于该 1 项目的解决方案。我们希望它在安装我们创建的 nuget 包后自动包含自己。
    • @joey,你和我都是。不幸的是,这些是我们从 StyleCopAnalyzers 组中获得的牌。值得庆幸的是,使用我们的 Xamarin.Forms 产品,我们只有少数项目需要这种手动按摩来达到我们的目标


    来源:https://www.codenong.com/52742473/

    微信公众号
    手机浏览(小程序)

    Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

    Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

    Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_8805.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
    0
    分享到:
    没有账号? 忘记密码?