Search code examples
msbuildcontinuous-integrationcode-analysisfxcopcmd

How to convert a ruleset file into a FXCop rules dll?


Am trying to configure the static code analysis(FxCop) in my Continuous integration system.But my developers are using a rule set file for static analysis with Visual Studio.

Is there a way that i can re-use the same rule-set file and convert it into a FxCop rule-set dll and perform the static code analysis while build?

Thanks in advance, Ravi


Solution

  • If you have Visual Studio installed on the CI server, then simply specifying /p:RunCodeAnalysis=[True|False|Always|Default|Never] on the MsBuild command line should run Code Analysis as it was configured on the developer's configuration. The rules files are automatically included in the Visual Studio project file, so they should resolve on their own.

    To run FxCop after the build you can specify the ruleset as a commandline parameter:

    C:\Program Files (x86)\Microsoft Visual Studio 12.0\Team Tools\Static Analysis Tools\FxCop>fxcopcmd /?
    Microsoft (R) FxCop Command-Line Tool, Version 12.0 (12.0.21005.1) X86
    Copyright (C) Microsoft Corporation, All Rights Reserved.
    
    /ruleset:<<+|-|=>file>  [Short form: /rs:<<+|-|=>file>]
    Rule set to be used for the analysis. It can be a file path to the rule set
    file or the file name of a built-in rule set. '+' enables all rules in the
    rule set; '-' disables all rules in the rule set; '=' sets rules to match the
    rule set and disables all rules that are not enabled in the rule set.
    
    /rulesetdirectory:<directory>  [Short form: /rsd:<directory>]
    Directory to search for rule set files that are specified by the /ruleset
    switch or are included by one of the specified rule sets.
    

    The difficult part of running FxCop from the commandline is that you will want to pass all references and that it can only process files targeting the same .NET system libraries (it can only hold one of those in memory). You can specify these references using the following parameters:

    /platform:<directory>  [Short form: /plat:<directory>]
    Location of platform assemblies.
    
    /directory:<directory>  [Short form: /d:<directory>]
    Location to search for assembly dependencies.
    
    /reference:<file>  [Short form: /ref:<file>]
    Reference assemblies required for analysis.