Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use StyleCop with TeamCity

Has anyone had any success with running StyleCop from TeamCity?

I know StyleCop supports a command line mode, however i am not sure how this will integrate into the report output by TeamCity.

I've checked out this plugin found here: https://bitbucket.org/metaman/teamcitydotnetcontrib/src/753712db5df7/stylecop/

However could not get it running.

I am using TeamCity 6.5.1 (latest).

like image 909
lysergic-acid Avatar asked Jun 16 '11 10:06

lysergic-acid


1 Answers

I don't know how familiar you are with MSBuild, but you should be able to add a new Build Step in TC 6 and above, and set MSBuild as the build runner, and point it to a .proj file which does something similar to the following:

<Target Name="StyleCop">

  <!-- Create a collection of files to scan -->
  <CreateItem Include="$(SourceFolder)\**\*.cs">
    <Output TaskParameter="Include" ItemName="StyleCopFiles" />
  </CreateItem>

  <StyleCopTask
    ProjectFullPath="$(MSBuildProjectFile)"
    SourceFiles="@(StyleCopFiles)"
    ForceFullAnalysis="true"
    TreatErrorsAsWarnings="true"
    OutputFile="StyleCopReport.xml"
    CacheResults="true" />

  <Xslt Inputs="StyleCopReport.xml"
     RootTag="StyleCopViolations" 
     Xsl="tools\StyleCop\StyleCopReport.xsl"
     Output="StyleCopReport.html" />

  <XmlRead XPath="count(//Violation)" XmlFileName="StyleCopReport.xml">
    <Output TaskParameter="Value" PropertyName="StyleCopViolations" />
  </XmlRead>

  <Error Condition="$(StyleCopViolations) > 0" Text="StyleCop found $(StyleCopViolations) broken rules!" />

</Target>

If you don't want to fail the build on a StyleCop error, then set the Error task to be Warning instead.

You'll also need to add the following to your .proj file:

<UsingTask TaskName="StyleCopTask" AssemblyFile="$(StyleCopTasksPath)\Microsoft.StyleCop.dll" />

Microsoft.StyleCop.dll is included in the StyleCop installation, and you'll need to set your paths appropriately.

To see the outputted StyleCop results in TeamCity, you will need to transform the .xml StyleCop report to HTML using an appropriate .xsl file (called StyleCopReport.xsl in the script above).

To display the HTML file in TeamCity, you'll need to create an artifact from this .html output, and then include that artifact in the build results.

The Continuous Integration in .NET book is a great resource.

like image 195
devdigital Avatar answered Nov 21 '22 00:11

devdigital