Search code examples
visual-studio-2010projects-and-solutionscode-analysistfsbuildfxcop

Automatically select correct CodeAnalysis ruleset


We have a number of large solutions and have a number of ruleset files based on the different project types. For example we have:

  • A Sharepoint Rulesset containing:

    • Microsoft All Rules
    • MSOCAF rules
    • SPDisposeChecker rule
    • A number of custom rules
  • A test project ruleset

    • Microsoft All Rules
    • Disabled a few naming guideline rules (the use of underscores for example)
    • A number of Test specific rules that similar to TypeMock TestLint
    • A number of custom rules
  • A standard ruleset

    • Microsoft All Rules
    • A number of custom rules

I'd love it if Code Analysis were able to automatically select the right set based on the project type.

I haven't found the ideal way to do this. Things I'll consider:

  • Drop a targets file in the msbuild folder of the build server which derives the project type based on the existence of specific build targets (yuk)
  • Specify the CodeAnalayis Rules file in each target file for each project type (yuk)
  • Create a custom Targets file and include that in every project, set a property in each project to select the correct project type. (yuk)
  • Alter the rules themselves to know whether they're in the correct context (possible for custom rules, not feasible for the default rules).

What we're currently doing:

  • Specify the ruleset manually for each project.

I'd love to hear ideas for a better solution...


Solution

  • I found a solution that works for most project types. Many of the default Msbuild targets provided by Microsoft include an option to load BeforeTargets and Aftertargets by placing the targets in question in a specific folder in the Msbuild folder structure.

    I put a targets file into these folders (naming based on the way the project type expects them, these tend to differ from project type to project type) where I specify the default CodeAnalaysis.rules file. The project can still override these, but if it doesn't the default is used.

    Sharepoint for example looks for this file:

    $(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\SharePointTools\Custom.After.Microsoft.VisualStudio.SharePoint.targets

    These references are stored in msbuild properties such as $(CustomAfterSharePointTargets) which you can extend or override if wanted.

    My solution is built upon this feature and this feature.

    I put in a request to standardize this more in Visual Studio 11. Please vote for it.