19

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

lysergic-acid
  • 19,570
  • 21
  • 109
  • 218

4 Answers4

16

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.

devdigital
  • 34,151
  • 9
  • 98
  • 120
  • Can I give as Include, my root path as you showed, and it will iterate recursively under all subfolders to look for .cs files? Also, my original problem was, how this will be displayed in the TeamCity report? – lysergic-acid Jun 16 '11 at 13:45
  • 1
    In the last version of StyleCop (4.7) StyleCopTask moved to 'StyleCop.dll', so the using will now looks like that: – mironych Jun 03 '13 at 13:01
6

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.

James Woolfenden
  • 6,498
  • 33
  • 53
  • Nice, i will chrck it out. Any way to also do this from the TC GUI ? – lysergic-acid Jun 17 '11 at 19:45
  • The first 2 are for your regular msbuild, you need to use a condition on each so that it doesnt break any manual builds -to only work if running inside tc. The graph thing is text edit on the server config folder. – James Woolfenden Jun 17 '11 at 22:34
5

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.

Adam Ralph
  • 29,453
  • 4
  • 60
  • 67
  • cool, but still kind of cumbersome to add to each and every project, dont u think? – lysergic-acid Oct 27 '12 at 17:34
  • You can add the package to all projects in a solution in one go, just like any other NuGet package. If you want the build to fail on a violation then yes, you'll have to add that element to every project manually. However, that step is optional - in the accepted answer there's a mandatory per-project manual step ;-) – Adam Ralph Oct 28 '12 at 18:31
  • 1
    @AdamRalph using this aproach, how do you go about outputting the results of the StyleCop Analysis to within TeamCity for visualizing if there are any errors? Thanks! – Gary Ewan Park Jul 10 '14 at 11:52
  • 1
    I don't do anything special. I just let the messages go into the log and inspect them from there. In my day job we fail the build on violations so we're forced to eyeball the log and fix them. For OSS I let them pass and fix them up when I see them locally in VS. – Adam Ralph Jul 11 '14 at 06:11
  • Gotcha, that makes sense. You would never (i.e. shouldn't) be able to commit anything where the build doesn't work locally on your own machine, so what is the point in having a report of the results in TeamCity. I can live with that. Thanks! – Gary Ewan Park Jul 11 '14 at 06:22
3

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.

Benjol
  • 63,995
  • 54
  • 186
  • 268
  • Doesn't work in TC8 unless like you I did something wrong. Instructions are wrong as it is as there is no stylecop.zip in the download, you have to make it. Anyway, doesn't work ... – Jammer Sep 04 '13 at 11:59