forked from fmueller/sonar-scala
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
24 changed files
with
1,303 additions
and
921 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/main/java/org/sonar/plugins/scala/cobertura/CoberturaSensor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Sonar Scala Plugin | ||
* Copyright (C) 2011 - 2014 All contributors | ||
* [email protected] | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 | ||
*/ | ||
package org.sonar.plugins.scala.cobertura; | ||
|
||
import java.io.File; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.sonar.api.batch.CoverageExtension; | ||
import org.sonar.api.batch.Sensor; | ||
import org.sonar.api.batch.SensorContext; | ||
import org.sonar.api.batch.fs.FileSystem; | ||
import org.sonar.api.resources.Project; | ||
import org.sonar.plugins.cobertura.api.AbstractCoberturaParser; | ||
import org.sonar.plugins.cobertura.api.CoberturaUtils; | ||
import org.sonar.plugins.scala.language.Scala; | ||
|
||
public class CoberturaSensor implements Sensor, CoverageExtension { | ||
private static final Logger LOG = LoggerFactory.getLogger(CoberturaSensor.class); | ||
|
||
private final FileSystem fileSystem; | ||
|
||
private static final AbstractCoberturaParser COBERTURA_PARSER = new ScalaCoberturaParser(); | ||
|
||
public CoberturaSensor(FileSystem fileSystem) { | ||
this.fileSystem = fileSystem; | ||
} | ||
|
||
public boolean shouldExecuteOnProject(Project project) { | ||
if(fileSystem.languages().contains(Scala.KEY)){ | ||
LOG.info("CoberturaSensor will be executed"); | ||
return true; | ||
} else { | ||
LOG.info("CoberturaSensor will NOT be executed"); | ||
return false; | ||
} | ||
} | ||
|
||
public void analyse(Project project, SensorContext context) { | ||
File report = CoberturaUtils.getReport(project); | ||
if (report != null) { | ||
parseReport(report, context); | ||
} | ||
} | ||
|
||
protected void parseReport(File xmlFile, final SensorContext context) { | ||
LOG.info("parsing {}", xmlFile); | ||
COBERTURA_PARSER.parseReport(xmlFile, context); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Scala CoberturaSensor"; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/main/java/org/sonar/plugins/scala/cobertura/ScalaCoberturaParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Sonar Scala Plugin | ||
* Copyright (C) 2011 - 2014 All contributors | ||
* [email protected] | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 | ||
*/ | ||
package org.sonar.plugins.scala.cobertura; | ||
|
||
import org.sonar.plugins.cobertura.api.AbstractCoberturaParser; | ||
import org.sonar.api.resources.Resource; | ||
import org.sonar.plugins.scala.language.ScalaFile; | ||
import java.io.File; | ||
|
||
public class ScalaCoberturaParser extends AbstractCoberturaParser { | ||
@Override | ||
protected Resource getResource(String fileName) { | ||
// TODO update the sbt scct plugin to provide the correct fully qualified class name. | ||
if (fileName.startsWith("src.main.scala.")) | ||
fileName = fileName.replace("src.main.scala.", ""); | ||
else if (fileName.startsWith("app.")) | ||
fileName = fileName.replace("app.", ""); | ||
int packageTerminator = fileName.lastIndexOf('.'); | ||
if (packageTerminator < 0) { | ||
return new ScalaFile(null, fileName, false); | ||
} else { | ||
String packageName = fileName.substring(0, packageTerminator); | ||
String className = fileName.substring(packageTerminator + 1, fileName.length()); | ||
return new ScalaFile(packageName, className, false); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
167 changes: 167 additions & 0 deletions
167
src/main/java/org/sonar/plugins/scala/language/ScalaFile.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
/* | ||
* Sonar Scala Plugin | ||
* Copyright (C) 2011 - 2014 All contributors | ||
* [email protected] | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 | ||
*/ | ||
package org.sonar.plugins.scala.language; | ||
|
||
import org.apache.commons.lang.StringUtils; | ||
import org.apache.commons.lang.builder.ToStringBuilder; | ||
import org.sonar.api.resources.InputFile; | ||
import org.sonar.api.resources.Language; | ||
import org.sonar.api.resources.Qualifiers; | ||
import org.sonar.api.resources.Resource; | ||
import org.sonar.api.resources.Scopes; | ||
import org.sonar.api.utils.WildcardPattern; | ||
|
||
/** | ||
* This class implements a Scala source file for Sonar. | ||
* | ||
* @author Felix Müller | ||
* @since 0.1 | ||
*/ | ||
public class ScalaFile extends Resource { | ||
|
||
private final boolean isUnitTest; | ||
|
||
private final String filename; | ||
|
||
private final String longName; | ||
|
||
private final ScalaPackage parent; | ||
|
||
public ScalaFile(String packageKey, String className, boolean isUnitTest) { | ||
super(); | ||
this.isUnitTest = isUnitTest; | ||
filename = className.trim(); | ||
|
||
String key; | ||
if (StringUtils.isBlank(packageKey)) { | ||
packageKey = ScalaPackage.DEFAULT_PACKAGE_NAME; | ||
key = new StringBuilder().append(packageKey).append(".").append(this.filename).toString(); | ||
longName = filename; | ||
} else { | ||
packageKey = packageKey.trim(); | ||
key = new StringBuilder().append(packageKey).append(".").append(this.filename).toString(); | ||
longName = key; | ||
} | ||
parent = new ScalaPackage(packageKey); | ||
setKey(key); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return filename; | ||
} | ||
|
||
@Override | ||
public String getLongName() { | ||
return longName; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public Language getLanguage() { | ||
return Scala.INSTANCE; | ||
} | ||
|
||
@Override | ||
public String getScope() { | ||
return Scopes.FILE; | ||
} | ||
|
||
@Override | ||
public String getQualifier() { | ||
return isUnitTest ? Qualifiers.UNIT_TEST_FILE : Qualifiers.FILE; | ||
} | ||
|
||
@Override | ||
public ScalaPackage getParent() { | ||
return parent; | ||
} | ||
|
||
@Override | ||
public boolean matchFilePattern(String antPattern) { | ||
final String patternWithoutFileSuffix = StringUtils.substringBeforeLast(antPattern, "."); | ||
final WildcardPattern matcher = WildcardPattern.create(patternWithoutFileSuffix, "."); | ||
return matcher.match(getKey()); | ||
} | ||
|
||
public boolean isUnitTest() { | ||
return isUnitTest; | ||
} | ||
|
||
/** | ||
* Shortcut for {@link #fromInputFile(InputFile, boolean)} for source files. | ||
*/ | ||
public static ScalaFile fromInputFile(InputFile inputFile) { | ||
return ScalaFile.fromInputFile(inputFile, false); | ||
} | ||
|
||
/** | ||
* Creates a {@link ScalaFile} from a file in the source directories. | ||
* | ||
* @param inputFile | ||
* the file object with relative path | ||
* @param isUnitTest | ||
* whether it is a unit test file or a source file | ||
* @return the {@link ScalaFile} created if exists, null otherwise | ||
*/ | ||
public static ScalaFile fromInputFile(InputFile inputFile, boolean isUnitTest) { | ||
if (inputFile == null || inputFile.getFile() == null || inputFile.getRelativePath() == null) { | ||
return null; | ||
} | ||
|
||
String packageName = PackageResolver.resolvePackageNameOfFile(inputFile.getFile().getAbsolutePath()); | ||
String className = resolveClassName(inputFile); | ||
|
||
if (isPackageObjectInFirstLevelPackage(packageName, className)) { | ||
String lastFolderName = extractLastFolderName(inputFile); | ||
return new ScalaFile(StringUtils.EMPTY, lastFolderName + "." + className, isUnitTest); | ||
} | ||
|
||
return new ScalaFile(packageName, className, isUnitTest); | ||
} | ||
|
||
private static boolean isPackageObjectInFirstLevelPackage(String packageName, String className) { | ||
return "<empty>".equalsIgnoreCase(packageName) && "package".equalsIgnoreCase(className); | ||
} | ||
|
||
private static String extractLastFolderName(InputFile inputFile) { | ||
String absolutePath = inputFile.getFile().getAbsolutePath(); | ||
int lastPathSeparator = absolutePath.lastIndexOf("/"); | ||
return absolutePath.substring(absolutePath.lastIndexOf("/", lastPathSeparator - 1) + 1, lastPathSeparator); | ||
} | ||
|
||
private static String resolveClassName(InputFile inputFile) { | ||
String classname = inputFile.getRelativePath(); | ||
if (inputFile.getRelativePath().indexOf('/') >= 0) { | ||
classname = StringUtils.substringAfterLast(inputFile.getRelativePath(), "/"); | ||
} | ||
return StringUtils.substringBeforeLast(classname, "."); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this).append("key", getKey()).append("fileName", filename) | ||
.append("isUnitTest", isUnitTest).append("longName", longName).append("parent", parent).toString(); | ||
} | ||
} |
Oops, something went wrong.