lysergic-acid
lysergic-acid

Reputation: 20050

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).

Upvotes: 19

Views: 10906

Answers (4)

Adam Ralph
Adam Ralph

Reputation: 29956

I'm late to the show here but a very easy way to achieve this is to install the StyleCop.MSBuild NuGet package in any project which you want to analyse with StyleCop.

After installing the package, StyleCop analysis will run on every build you do, regardless of where or how it is invoked, e.g VS, command line, msbuild, psake, rake, fake, bake, nant, build server, etc. No special actions are required.

If you want the build to fail when StyleCop rules are broken you just need to add the following element to your project file under each appropriate build configuration, E.g.

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <StyleCopTreatErrorsAsWarnings>false</StyleCopTreatErrorsAsWarnings>
    ...

Again, this will work on every build, regardless of where and how it is invoked.

Upvotes: 5

Benjol
Benjol

Reputation: 66531

There's a (new?) third-party TeamCity plugin for StyleCop here, (though I haven't tried it yet).

UPDATE: as far as I can tell, the latest version only works with TeamCity 7 (or I did something wrong). Also, I have a very slow (virtual) build server, so even after the services were restarted, it took a while for the StyleCop runner to appear in the web interface.

Another stupid thing I did was not read the readme properly: you have to unzip the downloaded zip, and use the zip inside.

I also originally started with just a list of .cs files in the "Include" option (for the build step), but that didn't work; links to sln or csproj files do work though.

Upvotes: 3

James Woolfenden
James Woolfenden

Reputation: 6661

Did you know that teamcity provides specific properties just from msbuild? No need for the service messages, see: http://confluence.jetbrains.net/display/TCD65/MSBuild+Service+Tasks

So you dont have to add a custom report page. Use the build stats e.g.

<TeamCitySetStatus Status="$(AllPassed)" Text="Violations: $(StyleCopViolations)" />

you can then log the statistic too:

<TeamCityReportStatsValue Key="StyleCopViolations" Value="$(StyleCopViolations)" />

And then create a custom graph to display, and you already have the violations in your msbuild output. edit main-config.xml and add:

<graph title="Style Violations" seriesTitle="Warning">
   <valueType key="StyleCopViolations" title="Violations" buildTypeId="bt20"/>
 </graph>

Where buildTypeId="bt20" bt20 is your style build.

Upvotes: 6

devdigital
devdigital

Reputation: 34349

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.

Upvotes: 16

Related Questions