Adding FxCop to a FAKE build script

INFO

This documentation is for FAKE.exe before version 5 (or the non-netcore version). The documentation needs te be updated, please help!

This article is an extension to the getting started guide. You will improve the same Calculator sample with a task for FxCop.

If you need more details please see the API docs for the FxCop task.

Setting up FxCop

Open build.fsx from your Calculator sample folder and add a new target FxCop to the targets section:

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
Target "FxCop" (fun () ->  
    !! (buildDir + @"\**\*.dll") 
    ++ (buildDir + @"\**\*.exe") 
    |> FxCop 
        (fun p -> 
            {p with 
              // override default parameters
              ReportFileName = testDir + "FXCopResults.xml"
              ToolPath = "FxCopCmd.exe"})
)

In the dependencies section modify the build order to:

1: 
2: 
3: 
4: 
5: 
6: 
7: 
"Clean"
  ==> "BuildApp"
  ==> "FxCop"
  ==> "BuildTest"
  ==> "Test"
  ==> "Deploy"
  ==> "Default"

That's it. If you run your build script you will get new .xml file in the ./test* folder:

alt text

Letting the build fail

If you want to let the build fail in the case that FxCop reports any errors or warnings you can use the FailOnError parameter:

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
Target "FxCop" (fun () ->  
    !! (buildDir + @"\**\*.dll") 
    ++ (buildDir + @"\**\*.exe") 
    |> FxCop 
        (fun p -> 
            {p with 
              // override default parameters
              ReportFileName = testDir + "FXCopResults.xml"
              FailOnError = FxCopErrorLevel.CriticalWarning
              ToolPath = "FxCopCmd.exe"})
)

alt text

If you activate this option FxCop errors will cause your build to fail. Possible values are:

  • FxCopErrorLevel.Warning
  • FxCopErrorLevel.CriticalWarning
  • FxCopErrorLevel.Error
  • FxCopErrorLevel.CriticalError
  • FxCopErrorLevel.ToolError
  • FxCopErrorLevel.DontFailBuild

The values are cummulative. If you choose FxCopErrorLevel.CriticalWarning the build will fail for critical warnings, errors, critical errors and FxCop tool errors but not for simple warnings. The default is FxCopErrorLevel.DontFailBuild.