Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

We are trying to provide a nuget package for all our projects with a stylecop ruleset. We get the files in the project but the ruleset is not applied to our projects. It still uses the minimimumrecomended.ruleset.

what we have now is:

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>

Does anyone have any idee or an example on github or so where we can find an example because we could not find any.

like image 212
joey Avatar asked Oct 10 '18 14:10

joey


2 Answers

We solved the issue with the following:

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>
like image 121
joey Avatar answered Nov 02 '22 21:11

joey


In our Xamarin.Forms projects the fix was a manual process of editing the .NET Standard .csproj file. If there's a better way, please let me know!

  1. First, open your .NET Standard project in a text editor (_Notepad++)
  2. Then add the <CodeAnalysisRuleSet> ... </CodeAnalysisRuleSet> inside a <PropertyGroup>.
<PropertyGroup>
    <CodeAnalysisRuleSet>..\stylecop.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
  1. (optional) If you want to access these files inside your project you can create an ItemGroup using <AdditionalFiles ... />.
<ItemGroup>
    <AdditionalFiles Include="..\stylecop.json" />
    <AdditionalFiles Include="..\stylecop.ruleset" />
</ItemGroup>

Reference

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

like image 35
Damian Avatar answered Nov 02 '22 20:11

Damian