Add code analysis ruleset through nuget package

后端 未结 3 645
南旧
南旧 2020-12-09 12:52

I\'m trying to build a NuGet package that adds our company\'s code analysis dictionary automatically and updatable.

The rule set is added in the content folder and n

相关标签:
3条回答
  • 2020-12-09 13:11

    You can also do it manually by using the following steps.

    • Right click project
    • Go to properties
    • Go to the dropdown [Run this rule set]
    • Select the [Browse..] option
    • Choose your company ruleset which you may place at a predefined location in source code.
    • Click Open on the Open window
    • Save the project file.

    You may repeat the step for other projects in the solution.

    0 讨论(0)
  • 2020-12-09 13:15

    I had the same problem as reported in the comments: the dictionary was added as content, not as a CodeAnalysisDictionary.

    I added this piece of code in the install.ps1 of the nuget package to solve this:

    $dictionary = $project.ProjectItems | Where-Object {$_.Name.EndsWith("CustomDictionary.xml")}
    $dictionary.Properties.Item("Buildaction").Value = [int]5;
    
    0 讨论(0)
  • 2020-12-09 13:21

    There's no need to script this. Both the ruleset and dictionary can be registered via an imported MSBuild .props file, as described here https://docs.microsoft.com/en-us/nuget/create-packages/creating-a-package#include-msbuild-props-and-targets-in-a-package

    For example, your NuGet source folder structure might look like this (assuming "CodeAnalysisSettings" is your package ID):

    • build
      • CodeAnalysisSettings.props
    • content
      • MyCustomDictionary.xml
      • MyRules.ruleset

    where the contents of CodeAnalysisSettings.props are something like the following:

    <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
        <PropertyGroup>
            <RunCodeAnalysis>true</RunCodeAnalysis>
            <CodeAnalysisRuleSet>MyRules.ruleset</CodeAnalysisRuleSet>
        </PropertyGroup>
        <ItemGroup>
            <CodeAnalysisDictionary Include="MyCustomDictionary.xml" />
        </ItemGroup>
    </Project>
    
    0 讨论(0)
提交回复
热议问题