Gendarme Integration for VS2008

Ever since setting up our CruiseControl.NET server for our internal projects, I wanted to integrate a Gendarme run to catch all those nasty little things that slip through, like not checking arguments for null. Now that I finally found some tuits to do it, the next problem became obvious: doing a quick commit often lead to a quick build fail. Gendarme would have no use if it didn’t find anything, no? So the missing piece was integration into Visual Studio. Arthur hacked together a little XSLT+PowerShell and voila, Gendarme now runs on every build right from the studio and populates the “Error List” window with properly linked entries. Yay! Read on for the details of the implementation. The complete source is downloadable at the end of the article. First, the XSLT (“gendarme.xslt”), transforming Gendarme’s XML to properly formatted text.

<?xml version="1.0"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:output method="text"/>

<xsl:template match="/">
<xsl:call-template name="rules">
<xsl:with-param name="detailnodes" select="//results/rule"/>
</xsl:call-template>
</xsl:template>

<xsl:template name="rules">
<xsl:param name="detailnodes"/>
<xsl:for-each select="$detailnodes">
<xsl:call-template name="defects">
<xsl:with-param name="detailnodes" select="target/defect"/>
</xsl:call-template>
</xsl:for-each>
</xsl:template>

<xsl:template name="defects">
<xsl:param name="detailnodes"/>
<xsl:for-each select="$detailnodes">
<xsl:value-of select="@Source"/>: error :[gendarme] <xsl:value-of select="../../problem"/> Target: <xsl:value-of select="../@Name"/>.
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>

Second, the powershell fragment to execute gendarme and transform the output (“gendarme.ps1″).

param($SolutionDir)

function Convert-WithXslt($xmlFilePath, $xsltFilePath, $outputFilePath)
{
$xsltFilePath = resolve-path $xsltFilePath
$xmlFilePath = resolve-path $xmlFilePath
$outputFilePath = resolve-path $outputFilePath

$xslt = new-object system.xml.xsl.xslcompiledtransform
$xslt.load( $xsltFilePath )
$xslt.Transform( $xmlFilePath, $outputFilePath )
}

if(-not $SolutionDir)
{
$SolutionDir = ".";
}

"." > $SolutionDir\bin\gendarme.txt
gendarme.exe -config $SolutionDir\gendarmerules.xml --xml $SolutionDir\bin\gendarme.xml $SolutionDir\bin\Debug\blahblah.exe
convert-withxslt $SolutionDir\bin\gendarme.xml $SolutionDir\gendarme.xslt $SolutionDir\bin\gendarme.txt
(get-content $SolutionDir\bin\gendarme.txt) -replace '\(\D?(\d+)\)', ' ($1,1)' | set-content $SolutionDir\bin\gendarme.txt
exit 0

Instead of blahblah.exe, put your own assemblies you want to check. And last but not least, the post build step to actually run the whole thing. powershell -command $(SolutionDir)gendarme.ps1 -SolutionDir:$(SolutionDir) type $(SolutionDir)bin\gendarme.txt This can be added in your project properties on the “Build Events” tab, as “Post-build event command line”. In our project this is executed in one of the unit-test projects which has dependencies on all other projects and thus builds last. Don’t forget to enable the RemoteSigned execution policy of the powershell; either by running set-executionpolicy -executionPolicy RemoteSigned from a Administrator’s shell or by adding one or both of the powershell.executionpolicy.reg and powershell.executionpolicy.wow6432.reg files to your registry. You can find all files in this ZIP archive.

dasZ.at

dasZ.at - the people behind ZBox.
dasZ.at Logo

Downloads

The lateset download will be available here - soon

Lastet blog

In our blog we talk about the latest developments around our tool ZBox.
>> Blog

Latest blog entries
>> Reference project: Implementing WordPress-based Website

Based on the reference design provided by Sabine herself, we implemented the zartbitter Website. The site is powered by the PHP-based WordPress blog and CMS engine, some additional plugins and a bit of yarn to hold it all together.


>> Setting a permanent search_path, the Right Way

Others recommend setting the search_path in the postgresql.conf. Current versions of PostgreSQL can set the search_path permanently on a per-database basis without having to touch system configuration files


>> Porting Puppet to Windows

In the course of the PuppetCamp Europe, I met with many people I only knew from IRC and email and it was a great time with interesting and inspiring discussions.

Today I want to write about the work I’m currently commisioned for by puppetlabs, porting puppet to Microsoft Windows.


>> Puppetcamp Europe 2010

I’m going to Puppetcamp Europe 2010!


>> Using gendarme with Code Contracts from .NET 4.0

When using gendarme on post-processed assemblies with code contracts and /throwonfailure set, a few things have to be ignored. Put the following lines into your ignore file (for example gendarmeignore.txt) and use it with –ignore on the command line.


>> Microsoft cannot decode Base64: News at 11!

Arthur has found a really nasty bug in Microsoft’s streaming Base64 decoder as used in the WCF: Connect Bug#541494


>> Kolab Connector binaries uploaded

Arthur moved on with programming and testing. Now we uploaded the first packages, which now contain the basic calendar and contacts synchronisation. The plugins already are able to synchronize our personal data.


>> Kolab Sync for Android and Outlook: Developer Preview

We are proud to announce the first developer preview for Kolab sync clients for both Android and Outlook. Both are licensed under the GPLv3.


>> Visual Studio 2008 Debugger

I didn’t know that: the VS2008 debugger has many bugs. Specifically, if you have a solution with multiple websites, debugging doesn’t work!

Symptom: Upon reaching a breakpoint, StepOver/Into do not work, but resume execution. This makes the debugger pretty pointless.


>> Building a simple MSBuild Task

On the “Using Studio’s “Custom Tool” in MSBuild” question, I was prompted to share the code. Here is a stripped down skeleton where I removed the actual calls to the custom tool. Since it is open source I didn’t really need to access the Visual Studio’s registry keys.