Skip to content
Permalink
Browse files
Merge pull request #45 from mixalturek/master
[JENKINS-24602] Native support for cloc tool
  • Loading branch information
mixalturek committed Sep 6, 2014
2 parents 8d04f3d + 084b360 commit 2ad96a005061559a0e3e6b21d3e635f9f11a9fbe
Showing 13 changed files with 496 additions and 27 deletions.
@@ -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.
@@ -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;
}
@@ -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{
@@ -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{
@@ -195,4 +198,8 @@ public int getNumBuildsInGraph() {
public boolean isIgnoreBuildFailure() {
return ignoreBuildFailure;
}

public boolean isCommentIsCode() {
return commentIsCode;
}
}
@@ -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;

@@ -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;
}
@@ -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());
}

@@ -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);
}

/**
@@ -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);
}

/**
@@ -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;
}

@@ -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;
@@ -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;
}


@@ -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
@@ -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();
}
}
}
}
@@ -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;
}
}
@@ -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 2ad96a0

Please sign in to comment.