Skip to content

Commit

Permalink
feat(coverage): move language definition to Java
Browse files Browse the repository at this point in the history
move language definition to Java file so it can be referenced from
other plugins. It didn't work with Scala class
(missing Scala runtime on classpath?)
Package name is consistent with Sonar convention.
  • Loading branch information
Sebastian Chmielewski committed Aug 17, 2017
1 parent 9d18153 commit a140f61
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 31 deletions.
22 changes: 21 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,31 @@
<version>${sonar.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>10.0.1</version>
</dependency>

<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
</dependencies>

<build>
<sourceDirectory>src/main/scala</sourceDirectory>
<!--sourceDirectory>src/main/scala</sourceDirectory-->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.scoverage</groupId>
<artifactId>scoverage-maven-plugin</artifactId>
Expand Down
102 changes: 102 additions & 0 deletions src/main/java/org/sonar/plugins/scala/Scala.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Sonar Scala Plugin
* Copyright (C) 2016-2016 SonarSource SA
* mailto:contact AT sonarsource DOT com
*
* 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 02110-1301, USA.
*/
package org.sonar.plugins.scala;

import org.sonar.api.config.Settings;
import org.sonar.api.resources.AbstractLanguage;

import scalariform.lexer.ScalaLexer;
import scalariform.lexer.Token;

import com.google.common.collect.Lists;
import org.apache.commons.lang.StringUtils;

import java.util.List;

/**
* This language cover JavaServer Pages (Scala).
*
*/
public class Scala extends AbstractLanguage {

/**
* Scala key
*/
public static final String KEY = "scala";

/**
* Scala language name
*/
public static final String NAME = "Scala";


/**
* Key of the file suffix parameter
*/
public static final String FILE_SUFFIXES_KEY = "sonar.scala.file.suffixes";

/**
* Default Java files knows suffixes
*/
public static final String DEFAULT_FILE_SUFFIXES = ".scala";

/**
* Settings of the plugin.
*/
private final Settings settings;

/**
* Default constructor
*/
public Scala(Settings settings) {
super(KEY, NAME);
this.settings = settings;
}

/**
* {@inheritDoc}
*
* @see org.sonar.api.resources.AbstractLanguage#getFileSuffixes()
*/
@Override
public String[] getFileSuffixes() {
String[] suffixes = filterEmptyStrings(settings.getStringArray(FILE_SUFFIXES_KEY));
if (suffixes.length == 0) {
suffixes = StringUtils.split(DEFAULT_FILE_SUFFIXES, ",");
}
return suffixes;
}

private static String[] filterEmptyStrings(String[] stringArray) {
List<String> nonEmptyStrings = Lists.newArrayList();
for (String string : stringArray) {
if (StringUtils.isNotBlank(string.trim())) {
nonEmptyStrings.add(string.trim());
}
}
return nonEmptyStrings.toArray(new String[nonEmptyStrings.size()]);
}


public static scala.collection.immutable.List<Token> tokenize(String sourceCode, String scalaVersion) {
return ScalaLexer.createRawLexer(sourceCode, false, scalaVersion).toList();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
*/
package com.buransky.plugins.scoverage.sensor

import com.buransky.plugins.scoverage.language.Scala
import com.buransky.plugins.scoverage.measure.ScalaMetrics
import com.buransky.plugins.scoverage.pathcleaner.{BruteForceSequenceMatcher, PathSanitizer}
import com.buransky.plugins.scoverage.util.LogUtil
Expand All @@ -32,6 +31,7 @@ import org.sonar.api.measures.{CoverageMeasuresBuilder, Measure}
import org.sonar.api.resources.{Project, Resource}
import org.sonar.api.scan.filesystem.PathResolver
import org.sonar.api.utils.log.Loggers
import org.sonar.plugins.scala.Scala

import scala.collection.JavaConversions._

Expand All @@ -46,7 +46,7 @@ class ScoverageSensor(settings: Settings, pathResolver: PathResolver, fileSystem
protected val SCOVERAGE_REPORT_PATH_PROPERTY = "sonar.scoverage.reportPath"
protected lazy val scoverageReportParser: ScoverageReportParser = XmlScoverageReportParser()

override def shouldExecuteOnProject(project: Project): Boolean = fileSystem.languages().contains(Scala.key)
override def shouldExecuteOnProject(project: Project): Boolean = fileSystem.languages().contains(Scala.KEY)

override def analyse(project: Project, context: SensorContext) {
scoverageReportPath match {
Expand Down Expand Up @@ -185,7 +185,7 @@ class ScoverageSensor(settings: Settings, pathResolver: PathResolver, fileSystem
val p = fileSystem.predicates()
Option(fileSystem.inputFile(p.and(
p.or(p.hasPath(path),p.hasRelativePath(path)),
p.hasLanguage(Scala.key),
p.hasLanguage(Scala.KEY),
p.hasType(InputFile.Type.MAIN))))
} else {
Option(fileSystem.inputDir(pathResolver.relativeFile(fileSystem.baseDir(), path)))
Expand Down
30 changes: 4 additions & 26 deletions src/main/scala/com/sagacify/sonar/scala/ScalaPlugin.scala
Original file line number Diff line number Diff line change
@@ -1,36 +1,14 @@
package com.sagacify.sonar.scala

import scala.collection.JavaConversions._
import scala.collection.mutable.ListBuffer

import org.sonar.plugins.scala.Scala
import com.buransky.plugins.scoverage.measure.ScalaMetrics
import com.buransky.plugins.scoverage.sensor.ScoverageSensor
import com.buransky.plugins.scoverage.widget.ScoverageWidget
import com.ncredinburgh.sonar.scalastyle.ScalastyleQualityProfile
import com.ncredinburgh.sonar.scalastyle.ScalastyleRepository
import com.ncredinburgh.sonar.scalastyle.ScalastyleSensor
import org.sonar.api.config.Settings
import org.sonar.api.Extension
import org.sonar.api.resources.AbstractLanguage
import com.ncredinburgh.sonar.scalastyle.{ScalastyleQualityProfile, ScalastyleRepository, ScalastyleSensor}
import org.sonar.api.SonarPlugin
import scalariform.lexer.ScalaLexer
import scalariform.lexer.Token

/**
* Defines Scala as a language for SonarQube.
*/
class Scala(s: Settings) extends AbstractLanguage("scala", "Scala") {

override def getFileSuffixes: Array[String] = Array("scala")

}

object Scala {

def tokenize(sourceCode: String, scalaVersion: String): List[Token] =
ScalaLexer.createRawLexer(sourceCode, false, scalaVersion).toList

}
import scala.collection.JavaConversions._
import scala.collection.mutable.ListBuffer

/**
* Plugin entry point.
Expand Down
2 changes: 2 additions & 0 deletions src/main/scala/com/sagacify/sonar/scala/ScalaSensor.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.sagacify.sonar.scala

import org.sonar.plugins.scala.Scala

import scala.io.Source
import scala.collection.JavaConversions._

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
// import java.io.File
// import java.util

// import com.buransky.plugins.scoverage.language.Scala
// import org.sonar.plugins.scala.Scala
// import com.buransky.plugins.scoverage.{FileStatementCoverage, DirectoryStatementCoverage, ProjectStatementCoverage, ScoverageReportParser}
// import org.junit.runner.RunWith
// import org.mockito.Mockito._
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import org.mockito.Mockito.verifyNoMoreInteractions;

import java.io.IOException;
import java.nio.file.Paths
import org.sonar.plugins.scala.Scala

import scala.collection.JavaConversions._

import org.junit.Before;
Expand Down
1 change: 1 addition & 0 deletions src/test/scala/com/sagacify/sonar/scala/MeasuresSpec.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.sagacify.sonar.scala

import org.sonar.plugins.scala.Scala
import org.scalatest._
// import scalariform.lexer.ScalaLexer
// import scalariform.lexer.Token
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package com.sagacify.sonar.scala

import org.sonar.plugins.scala.Scala
import com.ncredinburgh.sonar.scalastyle.ScalastyleSensor
import com.ncredinburgh.sonar.scalastyle.ScalastyleRepository
import com.ncredinburgh.sonar.scalastyle.ScalastyleQualityProfile
Expand Down

0 comments on commit a140f61

Please sign in to comment.