Using StyleCop in TFS Team Build

The recent release of the MSBuild Extensions includes a task for StyleCop 4.3. I have been trying to get this integrated into our TFS TeamBuild, I think it is a far more preferable way to do it than editing the various project files in our solution to link in StyleCop as you had to do in 4.2.

There are a good few steps I had to follow to get it doing:

  • Install the StyleCop 4.3 Msi

  • Install the MSBuild Extensions Msi

  • Now we have to do some fixed/changes. First copy the MSBuild.ExtensionPack.StyleCop.dll from the C:Program FilesMSBuildExtensionPack to C:Program FilesMSBuildMicrosoftStyleCopv4.3. We need to do this as the StyleCop DLLs are not automagically found (you could fix this using a search path I suppose)

  • Next we need to modify the C:Program FilesMSBuildExtensionPackMSBuild.ExtensionPack.tasks file to fix a typo that is a known issue. The StyleCop line at the end of the file should read

  • Now edit your Team Build tfsbuild.proj file; import the extension tasks

     

  • Now you need to edit or add the AfterCompile target, something like as shown below. I have added comments for each block.

   <!— Put up the start processing message – we clear it later -->
   <BuildStep TeamFoundationServerUrl="$(TeamFoundationServerUrl)"
      BuildUri="$(BuildUri)"
           Name="StyleCopStep"
            Message="StyleCop step is executing.">
   
 

    
   
     
   

  
  <MSBuild.ExtensionPack.CodeQuality.StyleCop
      TaskAction="Scan"
      SourceFiles="@(StyleCopFiles)"
      ShowOutput="true"
      ForceFullAnalysis="true"
      CacheResults="false"
      logFile="$(DropLocation)$(BuildNumber)StyleCopLog.txt"
      SettingsFile="$(SolutionRoot)MyTeamProjectMySolutionSettings.StyleCop"
      ContinueOnError="false">
   
   
   
  </MSBuild.ExtensionPack.CodeQuality.StyleCop>

<!—Log the summary of the results -->

  <!-- FailedFile format is:
     
         
              SA Rule Number
              Rule Description
              Rule Name
              Line the violation appears on
              SA violation message
         

     
—>

  <Error Text="StyleCop analysis warnings occured" Condition="'$(AllPassed)' == 'False'"  />

 
  <BuildStep TeamFoundationServerUrl="$(TeamFoundationServerUrl)"
          BuildUri="$(BuildUri)"
          Message="%(Failures.Identity) - Failed on Line %(Failures.LineNumber). %(Failures.CheckId): %(Failures.Message)"/>

  <!—Complete the stylecop step, we get here if we have no thrown an error  -->
<BuildStep TeamFoundationServerUrl="$(TeamFoundationServerUrl)"
                BuildUri="$(BuildUri)"
                Id="$(StyleCopStep)"
                Status="Succeeded"
                Message="StyleCop Succeeded: $(AllPassed), Violations: $(Violations)"/>

 
 

   

 
  <BuildStep TeamFoundationServerUrl="$(TeamFoundationServerUrl)"
          BuildUri="$(BuildUri)"
          Message="%(Failures.Identity) - Failed on Line %(Failures.LineNumber). %(Failures.CheckId): %(Failures.Message)"/>

So this will run StyleCop as a separate build step and you have the option to fail the build or not depending on your view of StyleCop violations by commenting out one line. Either way violations will be listed as rows in the list of build steps.

I had really wanted to get the number of violations added to either the total errors or warnings as listed in the Results Details at the end of the build, also I had wanted a simple way to access the StyleCopLog.txt created in the drops directory. However, I have not worked out how this final step yet. If I manage to work it out I will blog on the solution – or if you know to do please post a comment.

Updated –17 & 21 Oct 08 – Added some extra details to the comments in the XML sample and removed hard coded paths, now use team build parameters so everything is referenced via build agents root location.