如何在.net标准项目中获得一个stylecop规则集

问题描述 投票:4回答:2

我们正在尝试使用stylecop规则集为我们的所有项目提供nuget包。我们获取项目中的文件,但规则集不适用于我们的项目。它仍然使用minimimumrecomended.ruleset。

我们现在拥有的是:

Custom.stylecop.props

<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

<?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

<contentFiles>
    <files include="Content/stylecop.json" buildAction="EmbeddedResource" />
</contentFiles>
....
<files>
    <file src="build\**" target="build" />
    <file src="Content/stylecop.json" target="contentFiles" />
</files>

有没有人在github上有任何想法或例子,我们可以找到一个例子,因为我们找不到任何。

c# .net nuget .net-standard stylecop
2个回答
1
投票

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

  1. 首先,在文本编辑器中打开.NET Standard项目(_Notepad ++)
  2. 然后在<CodeAnalysisRuleSet> ... </CodeAnalysisRuleSet>中添加<PropertyGroup>
<PropertyGroup>
    <CodeAnalysisRuleSet>..\stylecop.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
  1. (可选)如果要在项目中访问这些文件,可以使用<AdditionalFiles ... />创建ItemGroup。
<ItemGroup>
    <AdditionalFiles Include="..\stylecop.json" />
    <AdditionalFiles Include="..\stylecop.ruleset" />
</ItemGroup>

Reference

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


0
投票

我们用以下方法解决了这个问题:

CodeAnalysis.props

<?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>

CodeAnalysis.nuspec

<?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></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>
© www.soinside.com 2019 - 2024. All rights reserved.