Saturday, December 25, 2010

Continuous Integration Step 2 – FxCop Integration with MsBuild

An FxCop project specifies the set of assemblies to be analyzed, the rules used to analyze the assemblies, and the most recent analysis results reported by FxCop.
Local Path of FxCopCmd.exe on my build server is for example:
C:\Program Files (x86)\Microsoft Visual Studio 10.0\Team Tools\Static Analysis Tools\FxCop\FxCopCmd.exe
Challenge is to write a custom target for code analysis, plug that target in build process to perform analysis as last task and then produce the report in xml file.  So below the After Build target in previous post, I wrote following script:
  <Target Name="Bash_CodeAnalysis">
      <Message Text ="Static Code Analysis Starts Here"  Importance="high"> </Message>

Build and verify that message appear at the end of “After Build” target execution. The code above is very simple that is, run Bash_CodeAnaysis target after standard build. Now I can write Exec Command afterwards.

<Target Name="Bash_CodeAnalysis">
      <Message Text ="Static Code Analysis Starts Here"  Importance="high"> </Message>
   <Exec Command="&quot;C:\Program Files (x86)\Microsoft Visual Studio 10.0\Team Tools\Static Analysis Tools\FxCop\FxCopCmd.exe&quot; /searchgac /rule:&quot;C:\Program Files (x86)\Microsoft Visual Studio 10.0\Team Tools\Static Analysis Tools\FxCop\Rules&quot; /file:$(ProjectDir)bin /out:C:\fxCop.xml" 

Note: Prefer to write Command value in single line otherwise command line tool will consider it separate commands and throws an error.
Parameters details that are passed in:
/searchgac – Tells FxCop to search the gac for any referenced dll's not found in say the bin folder.  If you forget this FxCop will error out.
/rule:Rull.dll – You can specify as many rules as you like to check against your compiled dll just repeat this command with each of the Rules you like.  Unless you have a specific FxCop project you must specify at least one rule. You can specify Rules folder as well
Note: As we are calling it from an MsBuild file which is XML we have to encode the quotes as “&quot;” otherwise famous 9009 error will be encountered.
/file:YourCompiled.dll – Much like the rule parameter you can specify as many .dll’s as you would like to check but need at least one. I specified project’s bin folder means all dlls to be analyzed in it.
/out:someXml.xml – You are going to want to specify an XML file to output the results to so that you can integrate the results in your build log. uses XSL to transform the generated XML into HTML for viewing in its Web Dashboard and auto generated Emails.
ContinueOnError – Unless you have written your own FxCop rules you are most likely going to want to set this value to true so that your build doesn’t fail each time you break one of the many rules that FxCop specifies.

Here is another example analyzing single NamingRules.dll on single dlls only.
<Target Name="FxCop" DependsOnTargets="BuildProject> 
<Exec Command="&quot;C:\Program Files\Microsoft FxCop 1.36\FxCopCmd.exe&quot;
 /searchgac /rule:&quot;C:\Program Files\Microsoft FxCop 1.36\Rules\NamingRules.dll&quot;
 /file:C:\Project\bin\My.Framework.dll /out:fxCop.xml" ContinueOnError="true">

Now save the project file.
Open VS2010 command line tool again and browse to the folder where csproj file exists. Execute  this command:
 >>> MSbuild GD.AutoDist.MainService.csproj /fl
After build, static code analysis is performed by FxCop as follows.

Now next is to dig into CCNet  configurations :)
Provide your comments

No comments:

Post a Comment