For those of you who use NAntContrib and CruiseContol .NET, you might be using the task <codestats> to get a rough estimate of the number of lines of code you have.

While I enjoy this task, I got tired of opening up the .xml result file and copy/paste those statistics in an Excel spreadsheet.  So I decided to investigate a way to integrate the results of <codestats> in the CruiseControl logs so I can display them on the report page.

Basically, it's a 2 step process:

  • Merge the result.xml (from <codestats>) into CruiseControl logs
  • Modify the modifications.xsl stylesheet

I'm truly sorry how the code looks below.  My blog doesn't have a lot of fancy tools to format my posts.
 
Step 1: Merge result.xml

In ccnet.config, add the following script in order to merge your results in the overall xml file

<publishers>
    <merge>
        <files><file>location of your codestats ouput</file></files>
    </merge> 
</publishers> 

Step 2: Modify modifications.xsl

Under C:\Program Files\Cruise Control .NET\webdashboard\xsl, open modifications.xsl.  Insert this part in the template starting with /

            <xsl:apply-templates select="/cruisecontrol/build/code-summaries"/>

Then, add this other piece of XML at the end of the file:

    <xsl:template match="code-summaries">

        <table class="section-table" cellpadding="2" cellspacing="0" border="0" width="98%">
           
           <tr>
                <td class="sectionheader" colspan="5">Code statistics</td>
           </tr>
           <tr>        
             <td valign="top"><xsl:value-of select="@type"/>        
             <xsl:for-each select="code-summary/linecount">
             Total number of lines: <xsl:value-of select="@totalLineCount" /><br/>
             Lines of code: <xsl:value-of select="@totalLineCount - @emptyLineCount - @commentLineCount" /> (<xsl:value-of select="round(((@totalLineCount - @emptyLineCount - @commentLineCount) div @totalLineCount) * 100)"/> %) <br/>
             Empty lines: <xsl:value-of select="@emptyLineCount"/> (<xsl:value-of select="round((@emptyLineCount div @totalLineCount) * 100)"/> %) <br/>
             Lines of comments:<xsl:value-of select="@commentLineCount" /> (<xsl:value-of select="round((@commentLineCount div @totalLineCount) * 100)"/> %) <br/>
             </xsl:for-each>                 
             </td>
           </tr>

        </table>

    </xsl:template>

Launch a build so the results of <codestats> merge into the log file.  Go to the CruiseControl .NET home page and click on a project.  You should see your statistics on the home page.  The neat thing is the percentage in parenthesis.  Don't forget to save your files before doing all this.

Let me know what you think