[UPDATE 3/24/12]  You can find a real code metrics activity here as part of the Community TFS Build Extensions.

Cameron Skinner has announced a new command line tool for generating code metrics.  We’ve long gotten requests to be able to generate code metrics from the build.  Prior to this tool, code metrics could only be generated from within the Visual Studio IDE.

I installed it this morning.  The readme link on the download page tells you where it is installed, which is %programfiles%\Microsoft Visual Studio 10.0\Team Tools\Static Analysis Tools\FxCop.

I wanted to do the simplest possible thing (i.e., quick and dirty!) I could to give it a try this morning as part of a 2010 TFS build.  I grabbed the copy of Professional Application Lifecycle Management I happened to have sitting here on my desk at home (thanks, Martin) and turned to page 504 to follow the ZIP archive example to get me started.  You can get the entire build chapter for free (same with the manual testing chapter).

  1. Open up the default build process template in the WF designer (e.g., open the build definition, click Show Details on the Process tab, click on the hyperlink, and double click the file)
  2. Scroll to the bottom of the workflow.
  3. Drag an InvokeProcess activity into the build process.  Drop it in as the last activity in the Try, Compile, Test sequence (drop it after the symbol activity, just before the catch block).
  4. Right click on the InvokeProcess activity and show parameters
  5. Set the following properties, fixing up the paths for your machine and the hard-coded assembly name (like I said, I went for the quick and dirty just to see it work).
    1. Arguments: String.Format("/f:""{0}"" /o:""{1}""", System.IO.Path.Combine(BinariesDirectory, "ConsoleApplication1.exe"), System.IO.Path.Combine(BinariesDirectory, "out.xml"))
    2. DisplayName: Code Metrics
    3. FileName: "D:\Program Files (x86)\Microsoft Visual Studio 10.0\Team Tools\Static Analysis Tools\FxCop\metrics.exe"
  6. Drop a WriteBuildMessage into the bock for the standard output and a WriteBuildError for the error output.
  7. Set the following for WriteBuildMessage
    1. Importance: Microsoft.TeamFoundation.Build.Client.BuildMessageImportance.High
    2. Message: stdOutput
  8. Set the following for the WriteBuildError
    1. Message: errOutput

Then I checked in my build template changes and ran a build.  The drop folder now contains a file called out.xml with the code metric data in it.

You can find documentation on all of the activities here on MSDN.


[UPDATE 1/30/10]  Martin sent me the links to the build and testing chapters, which I’ve added above.