StyleCop: Adding style cop to the .NET project.

StyleCop helps to consistent coding guide lines across the team and enforces the coding guide lines as configured in the configuration.

We can always customize the rules or rule set using configuration files.

Steps:

  • Create any .NET project and install the following nuget projects.

  • Add the following in the project file
  • Create the folder "StyleCopConfig" under project root folder and create the files - "stylecop.json" and "StyleCop.ruleset"
  • Add the following content in the stylecop.json

  {
  "$schema": "https://raw.githubusercontent.com/DotNetAnalyzers/StyleCopAnalyzers/master/StyleCop.Analyzers/StyleCop.Analyzers/Settings/stylecop.schema.json",
  "settings": {
    "documentationRules": {
      "companyName": "VajraTechMinds.com",
      "copyrightText": "Copyright (c) {companyName}. All Rights Reserved.\r\nLicensed under the MIT License. See LICENSE in the project root for license information.",
      "xmlHeader": false,
      "fileNamingConvention": "metadata"
    },
    "layoutRules": {
      "newlineAtEndOfFile": "require"
    },
    "namingRules": {
      "tupleElementNameCasing": "camelCase"
    }
  }
}
Add the following to the StyleCop.ruleset
<RuleSet Name="Rules for Hello World project" Description="These rules focus on critical issues for the Hello World app." ToolsVersion="10.0">
  <Localization ResourceAssembly="Microsoft.VisualStudio.CodeAnalysis.RuleSets.Strings.dll" ResourceBaseName="Microsoft.VisualStudio.CodeAnalysis.RuleSets.Strings.Localized">
    <Name Resource="HelloWorldRules_Name" />
    <Description Resource="HelloWorldRules_Description" />
  </Localization>
  <Rules AnalyzerId="Microsoft.Analyzers.ManagedCodeAnalysis" RuleNamespace="Microsoft.Rules.Managed">
    <Rule Id="CA1001" Action="Warning" />
    <Rule Id="CA1009" Action="Warning" />
    <Rule Id="CA1016" Action="Warning" />
    <Rule Id="CA1033" Action="Warning" />
  </Rules>
  <Rules AnalyzerId="Microsoft.CodeQuality.Analyzers" RuleNamespace="Microsoft.CodeQuality.Analyzers">
    <Rule Id="CA1802" Action="Error" />
    <Rule Id="CA1814" Action="Info" />
    <Rule Id="CA1823" Action="None" />
    <Rule Id="CA2217" Action="Warning" />
  </Rules>
</RuleSet>
Add the following to the project file.
    <itemgroup>
	<additionalfiles include="..\StyleCopConfig\stylecop.json" link="stylecop.json">
	</additionalfiles>
    </itemgroup>
    <PropertyGroup>
        <CodeAnalysisRuleSet>..\StyleCopConfig\StyleCop.ruleset</CodeAnalysisRuleSet>
    </PropertyGroup>

Comments

Popular posts from this blog

Data science : Data types - Foundation

Github - Building C#.net application using github actions.