SLOCCount Plugin

Skip to end of metadata
Go to start of metadata

Plugin Information

Plugin ID sloccount Changes In Latest Release
Since Latest Release
Latest Release
Latest Release Date
Required Core
Dependencies
1.19
Apr 15, 2014
1.447
dashboard-view (version:2.0, optional)
Source Code
Issue Tracking
Maintainer(s)
GitHub
Open Issues
Nicolas Piguet (id: npiguet)
OHTAKE Tomohiro (id: ohtake)
Jesse Farinacci (id: jieryn)
Seiji Sogabe (id: ssogabe)
Karsten Brandt (id: kbrandt)
Michal Turek (id: mixalturek)
Usage Installations 2013-Apr 1767
2013-May 1795
2013-Jun 1774
2013-Jul 1867
2013-Aug 1799
2013-Sep 1814
2013-Oct 1835
2013-Nov 1866
2013-Dec 1832
2014-Jan 1886
2014-Feb 1958
2014-Mar 2051

This plug-in generates the trend report for SLOCCount, an open source program which counts the number of lines of codes in over 25 different languages, including C/C++, Ada, COBOL, Fortran, SQL, Ruby, Python, etc...

Description

The SLOCCount plug-in scans for SLOCCount report files in the build workspace and reports the number of lines found.

The following features are provided by this plug-in:

  • Configuration of the files to scan after a build.
  • Trend reports showing the number of lines per language.
  • Trend reports showing the number of added or deleted lines per language.
  • Overview of the lines per file, folder or language.
  • Dashboard View portlet showing number of lines, files and languages per job.
  • Remote access API (REST API).
  • Works with any project/job type.

This plug-in is developed and maintained by Nicolas Piguet. Please use the Mailing Lists or issue tracker to ask questions, create feature request or bug reports, since I don't read the comment section on this page regularly.

Screenshots

Usage

Linux

  1. Install SLOCCount on the build slave.
  2. For each job configuration page:
    1. Add a execute shell build step to generate SLOCCount report or create the report file in your build scripts (Ant, Maven, Make, ...).
      1. Enter the following shell command: /usr/bin/sloccount --duplicates --wide --details [source-folder] >[output-folder]/sloccount.sc
    2. Activate the checkbox Publish SLOCCount analysis results and provide the path to the generated SLOCCount report.

Windows

Since SLOCCount does not run on a native Windows machine the setup for Windows requires additional steps. Most of the setup is explained in detail in the SLOCCount documentation.

  1. Install Cygwin on the build slave
  2. Compile and install SLOCCount within the Cygwin environment
  3. For each job configuration page:
    1. Add a build step to run Cygwin and SLOCCount. See the ANT example below to learn how to start Cygwin and run SLOCCount from an ANT script.
      1. Execute the following shell command in Cygwin: /usr/bin/sloccount --duplicates --wide --details [source-folder] >[output-folder]/sloccount.sc
        You must convert Windows paths that are passed as parameters to SLOCCount utility to Linux/Cygwin style (C:\ becomes /cygdrive/c/).
    2. Convert the SLOCCount results in sloccount.sc back to windows paths.
    3. Activate the checkbox Publish SLOCCount analysis results and provide the path to the generated SLOCCount report.
If you uses sloccount for multiple jobs sometimes two jobs end at the same time and sloccount is executed simultaneously. This will fail since SLOCCount (by default) uses one temp data folder in $HOME/.slocdata.

Either change sloccount to not use a temp data directory or change the setup explanation so novice users (like me) are explained to use the --datadir option in order to get a datadir per hudson job.

This seems to work:
[ -d slocdata ] || mkdir slocdata
sloccount --datadir slocdata --details "your source path" > sloccount.sc

ANT example for Windows build

The target converts Windows paths to Linux style cygdrive paths (C:\ becomes /cygdrive/c/) before SLOCCount runs. The converted paths are passed as environment variables to Cygwin. After SLOCCount has been executed, the results in sloccount.sc are converted back to windows paths.

ANT target

Add this target to your Jenkins build:

build.xml
<target name="sloccount" depends="createAllSrc">
  <script language="javascript"> <![CDATA[
    function winToCygdrivePath(cygProp, winProp) {
      winPath = project.getProperty(winProp);
      cygPath = winPath.replaceAll("C:", "/cygdrive/c").replace('\\', '/');
      project.setProperty(cygProp, cygPath);
    }
    winToCygdrivePath("cygdrive.workspace.dir", "dir.workspace");
    winToCygdrivePath("cygdrive.allsrc.dir", "dir.allsrc");
    winToCygdrivePath("cygdrive.build.scripts.dir", "dir.build.scripts");
  ]]></script>

  <exec executable="${cygwin.bin}\bash.exe">
    <arg line="-li '${cygdrive.build.scripts.dir}/cygwin_sloccount.sh'" />
    <env key="SLOCCOUNT_SOURCE_DIR" value="${cygdrive.allsrc.dir}" />
    <env key="SLOCCOUNT_OUTPUT_DIR" value="${cygdrive.workspace.dir}" />
  </exec>

  <replace file="${dir.workspace}/sloccount.sc" token="/cygdrive/c" value="C:" />
  <replace file="${dir.workspace}/sloccount.sc" token="/" value="\" />
</target>

You may have to adjust the paths used in the snippet, if your Jenkins workspace is on another drive than C.

Shell script

This shell script is called by Cygwin / ANT.

cygwin_sloccount.sh
sloccount --duplicates --wide --details "$SLOCCOUNT_SOURCE_DIR" > "$SLOCCOUNT_OUTPUT_DIR/sloccount.sc"

Remote access API (REST API)

Use the URLs below to access the SLOCCount specific data.

Description of possibilities

Data

Example of XML data:

<sloccountReportStatistics>
	<language>
		<files>7</files>
		<lines>513</lines>
		<name>xml</name>
	</language>
	<language>
		<files>116</files>
		<lines>8174</lines>
		<name>java</name>
	</language>
	<totalFiles>123</totalFiles>
	<totalLanguages>2</totalLanguages>
	<totalLines>8687</totalLines>
</sloccountReportStatistics>

Example of JSON data:

{
	"languages":
	[
		{"files":7,"lines":513,"name":"xml"},
		{"files":116,"lines":8174,"name":"java"},
	],
	"totalFiles":123,
	"totalLanguages":2,
	"totalLines":8687
}

Support for cloc tool

Cloc (Count Lines of Code) is a tool similar to SLOCCount. It provides output to a XML file that can be simply transformed to the SLOCCount format and used in this plugin. Cloc is written in Perl and should be better portable than SLOCCount.

This howto was created and tested on Debian GNU/Linux, Jenkins 1.447, SLOCCount plugin 1.12, cloc 1.60 and xsltproc 1.1.28.

Install cloc and xsltproc

Download cloc from http://cloc.sourceforge.net/ and install it. You need also a whatever tool for XML transformations (xsltproc used) or you can try clokins tool (not tested at all). You can install the packages present in your GNU/Linux distribution.

# Debian/Ubuntu specific
apt-get install cloc xsltproc

Script for XSLT transformation

Copy-paste the script below and store it to cloc2sloccount.xsl file accessible by your Jenkins server. If you are using master and slave machines, store the script to all slaves where the jobs are executed.

cloc2sloccount.xsl
<xsl:stylesheet version="1.0" xmlns:de="." xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" version="4.0" encoding="iso-8859-1"
  doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
  doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>

<xsl:template match="file">
  <xsl:variable name="file_path" select="translate(@name,'\\','/')"/>
  <xsl:variable name="file_path_part1" select="substring-after($file_path,'/')"/>
  <xsl:variable name="module" select="substring-before($file_path_part1,'/')"/>
  <xsl:value-of select="@code"/>
  <xsl:text>&#09;</xsl:text>
  <xsl:value-of select="@language"/>
  <xsl:text>&#09;</xsl:text>
  <xsl:value-of select="$module"/>
  <xsl:text>&#09;</xsl:text>
  <xsl:value-of select="@name"/>
  <xsl:text>&#13;&#10;</xsl:text>
</xsl:template>

<xsl:template match="files">
<xsl:apply-templates select="file"/>
</xsl:template>

<xsl:template match="cloc_url">For more details see: <xsl:value-of select="."/>
<xsl:text>&#13;&#10;</xsl:text>
</xsl:template>

<xsl:template match="cloc_version">This report has been generated by cloc <xsl:value-of select="."/>.<xsl:text>&#13;&#10;</xsl:text>
</xsl:template>

<xsl:template match="header">
<xsl:apply-templates select="cloc_version"/>
<xsl:apply-templates select="cloc_url"/>
</xsl:template>

<xsl:template match="results">
<xsl:apply-templates select="header"/>
<xsl:text>&#13;&#10;</xsl:text>
<xsl:apply-templates select="files"/>
</xsl:template>

<xsl:template match="/">
<xsl:apply-templates select="results"/>
</xsl:template>

</xsl:stylesheet>

Execute cloc

Change configuration of your job to execute the following shell script. Update the path to the directory where your source files are stored and optionally pass additional parameters to cloc. Then execute XSLT transformation on cloc output to get a SLOCCount compatible report.

cloc --by-file --xml -out=cloc.xml DIRECTORY
xsltproc PATH/TO/cloc2sloccount.xsl cloc.xml > sloccount.sc

Enable SLOCCount publisher, enter path to the generated SLOCCount report and that's all.

Known Issues

Change Log

Version 1.19 (Apr 15, 2014)

  • Fixed: Error 404 when trying to access language details for "C/C++ Header" (issue #22597).

Version 1.18 (Apr 5, 2014)

  • Implemented: Should have option to not care about build failure status (issue #22303).

Version 1.17 (Apr 2, 2014)

  • Unsuccessful.

Version 1.16 (Mar 14, 2014)

  • Fixed: Lazy loaded report details are never released (issue #21921).
  • Fixed: Fix findings from FindBugs static analysis (issue #22160).
  • Implemented: Remote access API (REST API) (issue #21922).

Version 1.15 (Feb 8, 2014)

  • Fixed: Possibly too wide table on summary page (issue #21557).
  • Fixed: Sorting using Distribution column/graph doesn't work (issue #21700).
  • Implemented: Configurable Graph (issue #21552).
  • Implemented: New Modules panel on report page (issue #21697).

Version 1.14 (Jan 26, 2014)

  • Implemented: sloccount report formatting on build summary (issue #13235).
  • Implemented: Create a dash-board view portlet that shows the SLOC of the selected projectes (issue #6876).
  • Implemented: add a sloccount portlet to dashboard (issue #12166).
  • Rejected: Support multimodule sloccount report (issue #13382).

Version 1.13 (Jan 24, 2014)

  • Fixed: Unable to copy the sloccount summary when the job has been executed on a slave with a different OS (issue #21467).
  • Fixed: Link to workspace root directory is not accessible in results (issue #21500).
  • Implemented: Can this plugin support CLOC for even more languages and better Windows support (issue #10274).
    • Existing information in the task tested, a howto about cloc support written and added to wiki.

Version 1.12 (Jan 18, 2014)

  • Fixed: Can't open sloccount table portlet (issue #21419).
    • Release for MS Windows, all long-term issues with Windows backslashes '\' in paths should be fixed now.
  • Outdated information about plugin version <= 1.6 removed from wiki to clean it up.

Version 1.11 (Jan 15, 2014)

  • Fixed: Memory consumption is huge (issue #4769).
    • Changes in data storage that heavily reduces memory consumption and increases performance during a common Jenkins use.
    • Stored data now contains statistics per language (just several numbers), full report is lazy-loaded only while it is needed in the results page. Compare with parsing of giant XML that contains information about each source file present in your sandbox multiplied by number of builds just to show a simple trend graph.
    • Data format is very different but the code should satisfy a backward compatibility, migration tested with plugin versions 1.8 and 1.10.
    • The plugin update will have full impact after builds from a previous version will completely role out and only new data are present.
  • Fixed: NoClassDefFoundError exception on job configuration page (issue #19629).
  • Fixed: Support for matrix projects (issue #12816) and IVY projects (issue #17024).
    • All project types should be supported now.
    • Unnecessary dependencies to Maven and FindBugs removed.
  • Fixed: Summary is missing for the first build (issue #20646).
  • Fixed: Broken link in menu on the job page before the first build (issue #21174).
  • Fixed: Broken differences in build summary for failed builds (issue #21223).
  • Fixed: Possible resource leak in parser (issue #21229).
  • Fixed: Broken HTML code on results page (issue #21230).
  • Fixed: Graphical artifacts in the trend graphs (issue #21258).
  • Implemented: SLOCCount churn graph (issue #14504).
    • New trend graph with differences in lines count between current and previous build.
  • Wiki updates - information, screenshots.

Version 1.10 (Apr 16, 2013)

  • fixed java.io.InvalidClassException (issue #14255).
  • Made the plugin applicable for Job Generator jobs.

Version 1.8 (Jun 16, 2012)

  • sloccount trend report only works up to last failed build (issue #9309).
  • SLOCCount plugin fails with Windows/Cygwin SLOCCount execution (issue #4836).

Version 1.7 (May 19, 2012)

  • String index out of range (issue #13775).
  • added Japanese localization.
  • use commas in numbers for readability (issue #13235).
  • sort lines and files numerically.
  • fixed breadcrumb move down.

Version 1.6 (Oct 19, 2011)

  • Allow Maven support

Version 1.5 (Feb 14, 2011)

  • Update link in help
  • Translation updates (French, Dutch)

Version 1.4 (Feb 10, 2010)

  • Update code for more recent Hudson

Version 1.3 (Jul 7, 2009)

  • Made to work with distributed builds (patch)

Version 1.2 (Feb 28, 2009)

  • Clicking on the SLOCCount icon in the left sidebar will now take you directly to the last build's detailed sloccount report instead of that build's general main page

Version 1.1 (Feb 24, 2009)

  • Added breadcrumbs to help in the navigation of the result report

Version 1.0 (Feb 20, 2009)

  • Initial release

Labels

Edit
plugin-report plugin-report Delete
plugin plugin Delete
plugins plugins Delete
supports-dashboard-view supports-dashboard-view Delete
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.

Add Comment