How do I choose a different Rule Set based on OutputType in Visual Studio?

171 Views Asked by At

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)

0

There are 0 best solutions below