The FXCop activity allows you to run the FXCop tools directly. This is as opposed to enabling Code Analysis in your Visual Studio project which runs a version of FXCop behind the scenes as part of the build process. The reason you might choose to use the stand alone version of FXCop is because of it richer set of command line parameters to control what is being checked and how. You can also use this activity to run the command line FxCop tool shipped with Visual Studio without going via a Visual Studio project’s settings.

NOTE: In most cases it is expect you will find it easier to run FXCop via the code analysis setting within a project, thus removing the need to edit your workflow to include this activity.

In this example we aim to show that the basic steps that are required to get the activity integrated into a build.

Before you can make use of any of the TFS community build activities you have to make sure they are available for the build system and on your development PC. Instructions for this process can be found in the ALM Rangers build guide or in the StyleCop page of this wiki. This page assumes the AssemblyInfo activity is available in the developers build process workflow toolbox.

Configuring the build

FXCop does an analysis against compiled assemblies, this means calling the FXCop activity needs to be placed after the compile activities

 

image

image

To add this block of activities:

  1. Add the following variable with a scope of the “Compile and Test for Configuration” sequence
    • FXCopResults of type boolean
  2. Add a FindMatchingFile activity, set the Result to FXCopFiles and the MatchPattern to string.Format("{0}\**\*.dll", BinariesDirectory ). This will recursively find all the .DLL files in the project and add them to the collection.
  3. Add a FXCop activity with the following properties (these are a minimum to get it working))
    • image
    • AnalysisFailed – set to the variable FXcopResult
    • Files – set to the list of files FileToAnalyse
    • FxCopyPath – If you installed the stand alone version of FXCop you can usually leave this property unset as the default is correct. In my case I set to the path for the version of FXCop installed as part of VS2010
    • OutputFile – the results file, this is empty if there are no issues with the code. String.Format("{0}\FXCop.xml",DropLocation)
  4. Add a WriteBuildMessage activity, set the importance to High (so we always see the message) and the Message to String.Format("FXCop analysis failed - returned {0}",FXCopResult). Note that you use this return code, or further processing of the output file to branch within the build process.

As a minimum only the  Project and/or Files and OutputFile properties need to be set.

The other properties you could set are:

Property Direction/Type Comment
Files InArgument<IEnumerable<string>> Item Collection of assemblies to analyse
DependencyDirectoriesary InArgument<IEnumerable<string>> Specifies additional directories to search for assembly dependencies.
Imports InArgument<IEnumerable<string>> Name of an analysis report or project file to import
Rules InArgument<IEnumerable<string>> Location of rule libraries to load (/rule option). Prefix the Rules path with ! to treat warnings as errors
LogResultsToBuildLog InArgument<bool> Set to true to log the results to the build file. Default is false
ShowSummary InArgument<bool> Set to true to display a summary (/summary option). Default is true
SearchGac InArgument<bool> Set to true to search the GAC for missing assembly references (/gac option). Default is false
SuccessFile InArgument<bool> Set to true to create .lastcodeanalysissucceeded file in output report directory if no build-breaking messages occur during analysis. Default is false
OverrideRuleVisibilities InArgument<bool> Set to true to run all overridable rules against all targets. Default is false
Timeout InArgument<int> Set the override timeout for analysis deadlock detection. Analysis will be aborted when analysis of a single item by a single rule exceeds the specified amount of time. Default is 0 to disable deadlock detection
FailOnMissingRules InArgument<bool> Set to true to treat missing rules or rule sets as an error and halt execution. Default is false
IgnoreGeneratedCode InArgument<bool> Set to true to suppress analysis results against generated code. Default is false
AspNetOnly InArgument<bool> Set to true to analyze only ASP.NET-generated binaries and honor global suppressions in App_Code.dll for all assemblies under analysis. Default is false
IgnoreInvalidTargets InArgument<bool> Set to true to silently ignore invalid target files. Default is false
Quiet InArgument<bool> Set to true to suppress all console output other than the reporting implied by /console or /consolexsl. Default is false
ForceOutput InArgument<bool> Set to true to write output XML and project files even in the case where no violations occurred. Default is false
Verbose InArgument<bool> Set to true to output verbose information during analysis (/verbose option)
UpdateProject InArgument<bool> Saves the results of the analysis in the project file. This option is ignored if the /project option is not specified (/update option)
LogToConsole InArgument<bool> Set to true to direct analysis output to the console (/console option). Default is true
Types InArgument<string> Specifies the types to analyze
RulesetDirectory InArgument<string> Specifies the directory to search for rule set files that are specified by the Ruleset switch or are included by one of the specified rule sets.
Ruleset InArgument<string> Specifies the 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
FxCopPath InArgument<string> Sets the path to FxCopCmd.exe. Default is [Program Files]\Microsoft FxCop 1.36\FxCopCmd.exe
ReportXsl InArgument<string> Sets the ReportXsl (/outXsl: option)
OutputFile InArgument<string> REQUIRED Set the name of the file for the analysis report
ConsoleXsl InArgument<string> Sets the ConsoleXsl (/consoleXsl option)
Dictionary InArgument<string> Sets the custom dictionary used by spelling rules.Default is no custom dictionary
Project InArgument<string> Set the name of the .fxcop project to use
AnalysisFailed OutArgument<bool> Gets AnalysisFailed. True if FxCop logged Code Analysis errors to the Output file.
ExitCode OutArgument<int> The exit code returned from FxCop
OutputText OutArgument<string> Gets the OutputText emitted during analysis

Running the build

When the build is run you should see an entry in the log

image

and the FXCop output file will be in the drops location

image

Last edited Mar 31, 2012 at 3:11 PM by rfennell, version 3

Comments

No comments yet.