0

Currently, I have a .Net Standard project that references StyleCop. It builds as a NuGet package and includes custom ruleset's and a custom props file. I would like the props file to apply a different rule set based on the output type of the project that references my NuGet package.

I want to add this NuGet package to a solution that includes different project types such as Class Library or Windows Forms. Different project types need a different ruleset. For example, I don't want to force documentation on a Windows Forms application but I want to force it in a Class Library project.

I am trying to do this using Conditions but the default StyleCop ruleset is always used.

I also do not know of a way to debug the project and props file to make sure the rule set is being included.

Here is my props file.

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="16.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <PropertyGroup>
    <CodeAnalysisRuleSetLocation Condition=" '$(NuGetPackageRoot)' != '' ">$(NuGetPackageRoot)\CustomStyleCop\1.0.0</CodeAnalysisRuleSetLocation>
    <CodeAnalysisRuleSetLocation Condition=" '$(CodeAnalysisRuleSetLocation)' == '' and '$(SolutionDir)' != '' ">$(SolutionDir)\packages\CustomStyleCop.1.0.0</CodeAnalysisRuleSetLocation>
    <CodeAnalysisRuleSetLocation Condition=" '$(CodeAnalysisRuleSetLocation)' == '' ">$([System.IO.Path]::GetDirectoryName($(MSBuildProjectDirectory)))\packages\CustomStyleCop.1.0.0</CodeAnalysisRuleSetLocation>
  </PropertyGroup>

  <PropertyGroup>
    <CodeAnalysisRuleSet>
    <CodeAnalysisRuleSet Condition=" '$(OutputType)' == 'Library' ">$(CodeAnalysisRuleSetLocation)\CustomStyleCopClassLibrary.ruleset</CodeAnalysisRuleSet>
    <CodeAnalysisRuleSet Condition=" '$(OutputType)' == 'Exe' or '$(OutputType)' == 'WinExe' ">$(CodeAnalysisRuleSetLocation)\CustomStyleCopForms.ruleset</CodeAnalysisRuleSet>
  </PropertyGroup>

  <ItemGroup>
    <AdditionalFiles Include="$(CodeAnalysisRuleSetLocation)\stylecop.json" Link="stylecop.json" />
  </ItemGroup>

</Project>

And my nuspec file.

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
  <metadata>
    <id>CustomStyleCop</id>
    <title>CustomStyleCop</title>
    <version>1.0.0</version>
    <developmentDependency>true</developmentDependency>
    <dependencies>
      <dependency id="StyleCop.Analyzers" version="[1.1.1-rc.114]" />
    </dependencies>
  </metadata>
  <files>
    <file src="stylecop.json" target="" />
    <file src="Rulesets\CustomStyleCopClassLibrary.ruleset" target="" />
    <file src="Rulesets\CustomStyleCopForms.ruleset" target="" />
    <file src="CustomStyleCop.props" target="build" />
  </files>
</package>

Is this possible to do? Or do I need different NuGet packages for each project type? Is there currently a way to debug msbuild? (I know it was available in the past but scrapped)

Alex
  • 870
  • 2
  • 9
  • 19
  • It's possible for your situation with winform and classlibrary. But for other project types like .net core mvc, right-click project=> properties you can't find a 'CodeAnalysis' option, so i think your nuget package won't support for some certain types. For your issue, you can get some help from [Nicole's suggestion](https://stackoverflow.com/questions/20267268/add-code-analysis-ruleset-through-nuget-package). – LoLance Apr 15 '19 at 06:25
  • To debug msbuild, it seems you use vs2019, please check this [similar issue](https://stackoverflow.com/questions/42116865/how-to-debug-a-project-file-in-msbuild-14-0-vs2015), the debug msbuild options have been cut since 2015, for now we can set the [build log to details](https://learn.microsoft.com/en-us/visualstudio/ide/how-to-view-save-and-configure-build-log-files?view=vs-2019#to-change-the-amount-of-information-included-in-the-build-log) as an alternative workaround. – LoLance Apr 15 '19 at 06:28

0 Answers0