Skip to content

Commit

Permalink
[JENKINS-24602] Native support for cloc tool
Browse files Browse the repository at this point in the history
- Parsing of cloc report files (XML variant).
- Conversion to SLOCCount internal representation that is used in the plugin.
- Improved help strings in configuration page.
  • Loading branch information
mixalturek committed Sep 6, 2014
1 parent 0a4c1fb commit 084b360
Show file tree
Hide file tree
Showing 13 changed files with 496 additions and 27 deletions.
13 changes: 10 additions & 3 deletions src/main/java/hudson/plugins/sloccount/SloccountPublisher.java
Expand Up @@ -39,6 +39,7 @@ public class SloccountPublisher extends Recorder implements Serializable {

private final String pattern;
private final String encoding;
private final boolean commentIsCode;

/**
* Maximal number of last successful builds displayed in the trend graphs.
Expand All @@ -50,11 +51,12 @@ public class SloccountPublisher extends Recorder implements Serializable {
private final boolean ignoreBuildFailure;

@DataBoundConstructor
public SloccountPublisher(String pattern, String encoding,
public SloccountPublisher(String pattern, String encoding, boolean commentIsCode,
int numBuildsInGraph, boolean ignoreBuildFailure){
super();
this.pattern = pattern;
this.encoding = encoding;
this.commentIsCode = commentIsCode;
this.numBuildsInGraph = numBuildsInGraph;
this.ignoreBuildFailure = ignoreBuildFailure;
}
Expand Down Expand Up @@ -82,7 +84,8 @@ public boolean perform(AbstractBuild<?,?> build, Launcher launcher, BuildListene
}
}

SloccountParser parser = new SloccountParser(this.getRealEncoding(), this.getRealPattern(), logger);
SloccountParser parser = new SloccountParser(getRealEncoding(),
getRealPattern(), logger, commentIsCode);
SloccountPublisherReport report;

try{
Expand All @@ -102,7 +105,7 @@ public boolean perform(AbstractBuild<?,?> build, Launcher launcher, BuildListene
}

SloccountResult result = new SloccountResult(report.getStatistics(),
getRealEncoding(), null, build);
getRealEncoding(), commentIsCode, null, build);
build.addAction(new SloccountBuildAction(build, result));

try{
Expand Down Expand Up @@ -195,4 +198,8 @@ public int getNumBuildsInGraph() {
public boolean isIgnoreBuildFailure() {
return ignoreBuildFailure;
}

public boolean isCommentIsCode() {
return commentIsCode;
}
}
20 changes: 13 additions & 7 deletions src/main/java/hudson/plugins/sloccount/SloccountResult.java
Expand Up @@ -21,7 +21,7 @@
*/
public class SloccountResult implements Serializable {
/** Serial version UID. */
private static final long serialVersionUID = 0L;
private static final long serialVersionUID = 1L;

private transient SloccountReport report;

Expand All @@ -33,10 +33,14 @@ public class SloccountResult implements Serializable {
/** The encoding that was active at the time of the build. */
private final String encoding;

private final boolean commentIsCode;

public SloccountResult(SloccountReportStatistics statistics, String encoding,
boolean commentIsCode,
SloccountReport report, AbstractBuild<?,?> owner){
this.statistics = statistics;
this.encoding = encoding;
this.commentIsCode = commentIsCode;
this.report = report;
this.owner = owner;
}
Expand Down Expand Up @@ -102,7 +106,8 @@ private SloccountReport lazyLoadReport() {
String realEncoding = (encoding != null && !encoding.isEmpty())
? encoding : SloccountPublisher.DEFAULT_ENCODING;

SloccountParser parser = new SloccountParser(realEncoding, null, null);
SloccountParser parser = new SloccountParser(realEncoding, null, null,
commentIsCode);
return parser.parseFiles(destDir.listFiles());
}

Expand All @@ -126,7 +131,7 @@ public boolean isEmpty() {

public SloccountResult getLanguageResult(String language){
SloccountReport filtered = new SloccountReport(this.getReport(), new LanguageFileFilter(language));
return new BreadCrumbResult(filtered, this.owner, language);
return new BreadCrumbResult(filtered, this.owner, language, commentIsCode);
}

/**
Expand All @@ -139,12 +144,12 @@ public SloccountResult getModuleResult(String module){
SloccountReport filtered = new SloccountReport(this.getReport(),
new ModuleFileFilter(module));

return new BreadCrumbResult(filtered, owner, module);
return new BreadCrumbResult(filtered, owner, module, commentIsCode);
}

public SloccountResult getFolderResult(String folder){
SloccountReport filtered = new SloccountReport(this.getReport(), new FolderFileFilter(folder));
return new BreadCrumbResult(filtered, this.owner, folder);
return new BreadCrumbResult(filtered, this.owner, folder, commentIsCode);
}

/**
Expand Down Expand Up @@ -219,8 +224,9 @@ private static class BreadCrumbResult extends SloccountResult implements ModelOb

private String displayName = null;

public BreadCrumbResult(SloccountReport report, AbstractBuild<?,?> owner, String displayName){
super(null, null, report, owner);
public BreadCrumbResult(SloccountReport report, AbstractBuild<?,?> owner,
String displayName, boolean commentIsCode){
super(null, null, commentIsCode, report, owner);
this.displayName = displayName;
}

Expand Down
37 changes: 26 additions & 11 deletions src/main/java/hudson/plugins/sloccount/model/SloccountParser.java
@@ -1,9 +1,11 @@
package hudson.plugins.sloccount.model;

import hudson.FilePath;
import hudson.plugins.sloccount.model.cloc.ClocReport;
import hudson.plugins.sloccount.util.FileFinder;
import hudson.remoting.VirtualChannel;

import javax.xml.bind.JAXBException;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
Expand All @@ -18,18 +20,21 @@
public class SloccountParser implements
FilePath.FileCallable<SloccountPublisherReport> {
/** Serial version UID. */
private static final long serialVersionUID = 0L;
private static final long serialVersionUID = 1L;

private static final boolean LOG_ENABLED = false;

private final String encoding;
private final String filePattern;
private transient PrintStream logger = null;
private final boolean commentIsCode;

public SloccountParser(String encoding, String filePattern, PrintStream logger){
public SloccountParser(String encoding, String filePattern, PrintStream logger,
boolean commentIsCode) {
this.logger = logger;
this.filePattern = filePattern;
this.encoding = encoding;
this.commentIsCode = commentIsCode;
}


Expand All @@ -46,10 +51,10 @@ public SloccountPublisherReport invoke(java.io.File workspace, VirtualChannel ch

return report;
}

/**
* Parse a list of input files. All errors are silently ignored.
*
*
* @param files
* the files
* @return the content of the parsed files in form of a report
Expand All @@ -71,14 +76,24 @@ public SloccountReport parseFiles(java.io.File[] files) {
}

private void parse(java.io.File file, SloccountReportInterface report) throws IOException {
InputStreamReader in = null;

try {
in = new InputStreamReader(new FileInputStream(file), encoding);
this.parse(in, report);
} finally {
if(in != null) {
in.close();
// Try cloc report file first, XML has precise structure
ClocReport.parse(file).toSloccountReport(report, commentIsCode);
} catch (JAXBException e) {
if(LOG_ENABLED && (this.logger != null)){
this.logger.println("Parsing of cloc format unsuccessful, trying SLOCCount format: " + e);
}

// Try SLOCCount report file
InputStreamReader in = null;

try {
in = new InputStreamReader(new FileInputStream(file), encoding);
this.parse(in, report);
} finally {
if(in != null) {
in.close();
}
}
}
}
Expand Down
67 changes: 67 additions & 0 deletions src/main/java/hudson/plugins/sloccount/model/cloc/ClocFile.java
@@ -0,0 +1,67 @@
package hudson.plugins.sloccount.model.cloc;

import javax.xml.bind.annotation.XmlAttribute;
import java.io.Serializable;

/**
* Cloc XML, file element.
*
* @author Michal Turek
* @since 1.20
*/
public class ClocFile implements Serializable {
private static final long serialVersionUID = 1;

@XmlAttribute
private final String name;

@XmlAttribute
private final int blank;

@XmlAttribute
private final int comment;

@XmlAttribute
private final int code;

@XmlAttribute
private final String language;

/**
* Constructor.
*/
public ClocFile(String name, int blank, int comment, int code, String language) {
this.name = name;
this.blank = blank;
this.comment = comment;
this.code = code;
this.language = language;
}

/**
* This constructor is required by JAXB.
*/
public ClocFile() {
this(null, 0, 0, 0, null);
}

public String getName() {
return name;
}

public int getBlank() {
return blank;
}

public int getComment() {
return comment;
}

public int getCode() {
return code;
}

public String getLanguage() {
return language;
}
}
44 changes: 44 additions & 0 deletions src/main/java/hudson/plugins/sloccount/model/cloc/ClocFiles.java
@@ -0,0 +1,44 @@
package hudson.plugins.sloccount.model.cloc;

import javax.xml.bind.annotation.XmlElement;
import java.io.Serializable;
import java.util.List;

/**
* Cloc XML, files element.
*
* @author Michal Turek
* @since 1.20
*/
public class ClocFiles implements Serializable {
private static final long serialVersionUID = 1;

@XmlElement(name = "file", type = ClocFile.class)
private final List<ClocFile> files;

@XmlElement(name = "total", type = ClocTotal.class)
private final ClocTotal total;

/**
* Constructor.
*/
public ClocFiles(List<ClocFile> files, ClocTotal total) {
this.files = files;
this.total = total;
}

/**
* This constructor is required by JAXB.
*/
public ClocFiles() {
this(null, null);
}

public List<ClocFile> getFiles() {
return files;
}

public ClocTotal getTotal() {
return total;
}
}

0 comments on commit 084b360

Please sign in to comment.