From df042081f34925bc79ddf7b52b5d898d12a084a9 Mon Sep 17 00:00:00 2001 From: Bert Date: Sun, 23 Aug 2015 10:53:19 +0200 Subject: [PATCH] Support TFS team builds --- .../cxx/compiler/CxxCompilerVcParser.java | 8 +- .../plugins/cxx/utils/CxxReportSensor.java | 73 ++++++++++--------- 2 files changed, 43 insertions(+), 38 deletions(-) diff --git a/sonar-cxx-plugin/src/main/java/org/sonar/plugins/cxx/compiler/CxxCompilerVcParser.java b/sonar-cxx-plugin/src/main/java/org/sonar/plugins/cxx/compiler/CxxCompilerVcParser.java index 866a1041e..bc1e1ba82 100644 --- a/sonar-cxx-plugin/src/main/java/org/sonar/plugins/cxx/compiler/CxxCompilerVcParser.java +++ b/sonar-cxx-plugin/src/main/java/org/sonar/plugins/cxx/compiler/CxxCompilerVcParser.java @@ -24,9 +24,9 @@ import java.util.Scanner; import java.util.regex.Pattern; import java.util.regex.MatchResult; + import org.sonar.api.batch.SensorContext; import org.sonar.api.resources.Project; - import org.sonar.plugins.cxx.utils.CxxUtils; /** @@ -39,7 +39,7 @@ public class CxxCompilerVcParser implements CompilerParser { // ToDo: as long as java 7 API is not used the support of named groups for regular expression is not possible // sample regex for VS2012/2013: "^.*>(?.*)\\((?\\d+)\\):\\x20warning\\x20(?C\\d+):(?.*)$"; // get value with e.g. scanner.match().group("filename"); - public static final String DEFAULT_CHARSET_DEF = "UTF-8"; // use "UTF-16" for VS2010 build log + public static final String DEFAULT_CHARSET_DEF = "UTF-8"; // use "UTF-16" for VS2010 build log or TFS Team build log file /** * {@inheritDoc} @@ -74,10 +74,10 @@ public String defaultCharset() { */ public void processReport(final Project project, final SensorContext context, File report, String charset, String reportRegEx, List warnings) throws java.io.FileNotFoundException { - CxxUtils.LOG.info("Parsing 'Visual C++' format"); + CxxUtils.LOG.info("Parsing 'Visual C++' format ({})",charset); Scanner scanner = new Scanner(report, charset); Pattern p = Pattern.compile(reportRegEx, Pattern.MULTILINE); - CxxUtils.LOG.debug("Using pattern : '" + p.toString() + "'"); + CxxUtils.LOG.info("Using pattern : '" + p.toString() + "'"); MatchResult matchres = null; while (scanner.findWithinHorizon(p, 0) != null) { matchres = scanner.match(); diff --git a/sonar-cxx-plugin/src/main/java/org/sonar/plugins/cxx/utils/CxxReportSensor.java b/sonar-cxx-plugin/src/main/java/org/sonar/plugins/cxx/utils/CxxReportSensor.java index 1b9c1c86e..c19a00f80 100644 --- a/sonar-cxx-plugin/src/main/java/org/sonar/plugins/cxx/utils/CxxReportSensor.java +++ b/sonar-cxx-plugin/src/main/java/org/sonar/plugins/cxx/utils/CxxReportSensor.java @@ -25,7 +25,6 @@ import java.util.List; import java.util.ArrayList; import java.util.Set; - import org.apache.tools.ant.DirectoryScanner; import org.apache.commons.io.FilenameUtils; import org.sonar.api.batch.Sensor; @@ -157,53 +156,59 @@ public static List getReports(Settings conf, String reportPath = conf.getString(reportPathPropertyKey); List reports = new ArrayList(); if (reportPath != null && !reportPath.isEmpty()) { +// ToDo: Remove the dependency to org.apache.commons.io and use java.nio.file.Paths instead (Java 1.7) reportPath = FilenameUtils.normalize(reportPath); - CxxUtils.LOG.debug("Using pattern '{}' to find reports", reportPath); - - DirectoryScanner scanner = new DirectoryScanner(); - String[] includes = new String[1]; - includes[0] = reportPath; - scanner.setIncludes(includes); - String baseDirPath = baseDirPath1; - scanner.setBasedir(new File(baseDirPath)); - String[] relPaths = new String[0]; - try { - scanner.scan(); - relPaths = scanner.getIncludedFiles(); - } catch (IllegalStateException e) { - CxxUtils.LOG.error("Invalid report baseDir '{}'", baseDirPath); - } - if (relPaths.length < 1 && !baseDirPath2.isEmpty()) { - baseDirPath = baseDirPath2; + File singleFile = new File(reportPath); + if (singleFile.exists()) { + reports.add(singleFile); + } else { + CxxUtils.LOG.debug("Using pattern '{}' to find reports", reportPath); +// ToDo: Does the ant pattern search makes any sense for tool reports??? +// Why shouldn't we use a list of report files ... + DirectoryScanner scanner = new DirectoryScanner(); + String[] includes = new String[1]; + includes[0] = reportPath; + scanner.setIncludes(includes); + String baseDirPath = baseDirPath1; scanner.setBasedir(new File(baseDirPath)); + String[] relPaths = new String[0]; try { scanner.scan(); relPaths = scanner.getIncludedFiles(); } catch (IllegalStateException e) { CxxUtils.LOG.error("Invalid report baseDir '{}'", baseDirPath); } - } - - for (String relPath : relPaths) { - String path = CxxUtils.normalizePath(new File(baseDirPath, relPath).getAbsolutePath()); - try { - File reportFile = new File(path); - if (reportFile.exists()) { - reports.add(reportFile); - } else { - CxxUtils.LOG.error("Can't read report '{}'", path); + if (relPaths.length < 1 && !baseDirPath2.isEmpty()) { + baseDirPath = baseDirPath2; + scanner.setBasedir(new File(baseDirPath)); + try { + scanner.scan(); + relPaths = scanner.getIncludedFiles(); + } catch (IllegalStateException e) { + CxxUtils.LOG.error("Invalid report baseDir '{}'", baseDirPath); } - } catch (SecurityException e) { - CxxUtils.LOG.error("Read access to report '{}' denied", path); } - } - if (reports.isEmpty()) { - CxxUtils.LOG.warn("Cannot find a report for '{}={}'", reportPathPropertyKey, reportPath); + + for (String relPath : relPaths) { + String path = CxxUtils.normalizePath(new File(baseDirPath, relPath).getAbsolutePath()); + try { + File reportFile = new File(path); + if (reportFile.exists()) { + reports.add(reportFile); + } else { + CxxUtils.LOG.error("Can't read report '{}'", path); + } + } catch (SecurityException e) { + CxxUtils.LOG.error("Read access to report '{}' denied", path); + } + } + if (reports.isEmpty()) { + CxxUtils.LOG.warn("Cannot find a report for '{}={}'", reportPathPropertyKey, reportPath); + } } } else { CxxUtils.LOG.error("Undefined report path value for key '{}'", reportPathPropertyKey); } - return reports; }