Skip to content

Commit

Permalink
Fixes #117 - Incorrect error messages from sbt plugin
Browse files Browse the repository at this point in the history
This was because the configuration used was incorrect.
  • Loading branch information
matthewfarwell committed Aug 19, 2014
1 parent 5465c15 commit 85b2a53
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 17 deletions.
14 changes: 13 additions & 1 deletion .classpath
Expand Up @@ -3,7 +3,19 @@
<classpathentry output="target/scala-2.10/sbt-0.13/classes" kind="src" path="src/main/java"/>
<classpathentry output="target/scala-2.10/sbt-0.13/test-classes" kind="src" path="src/test/scala"/>
<classpathentry output="target/scala-2.10/sbt-0.13/test-classes" kind="src" path="src/test/java"/>
<classpathentry kind="lib" path="/home/mfarwell/.ivy2/cache/org.scalastyle/scalastyle_2.10/jars/scalastyle_2.10-0.5.0.jar"/>
<classpathentry kind="lib" path="/home/mfarwell/.ivy2/cache/scala_2.10/sbt_0.13/com.typesafe.sbt/sbt-pgp/jars/sbt-pgp-0.8.2.jar"/>
<classpathentry kind="lib" path="/home/mfarwell/.ivy2/cache/com.jsuereth/gpg-library_2.10/jars/gpg-library_2.10-0.8.2.jar"/>
<classpathentry kind="lib" path="/home/mfarwell/.ivy2/cache/org.bouncycastle/bcpg-jdk15on/jars/bcpg-jdk15on-1.49.jar"/>
<classpathentry kind="lib" path="/home/mfarwell/.ivy2/cache/org.bouncycastle/bcprov-jdk15on/jars/bcprov-jdk15on-1.49.jar"/>
<classpathentry kind="lib" path="/home/mfarwell/.ivy2/cache/net.databinder/dispatch-http_2.10/jars/dispatch-http_2.10-0.8.10.jar"/>
<classpathentry kind="lib" path="/home/mfarwell/.ivy2/cache/net.databinder/dispatch-core_2.10/jars/dispatch-core_2.10-0.8.10.jar"/>
<classpathentry kind="lib" path="/home/mfarwell/.ivy2/cache/org.apache.httpcomponents/httpclient/jars/httpclient-4.1.3.jar"/>
<classpathentry kind="lib" path="/home/mfarwell/.ivy2/cache/org.apache.httpcomponents/httpcore/jars/httpcore-4.1.4.jar"/>
<classpathentry kind="lib" path="/home/mfarwell/.ivy2/cache/commons-logging/commons-logging/jars/commons-logging-1.1.1.jar"/>
<classpathentry kind="lib" path="/home/mfarwell/.ivy2/cache/commons-codec/commons-codec/jars/commons-codec-1.4.jar"/>
<classpathentry kind="lib" path="/home/mfarwell/.ivy2/cache/net.databinder/dispatch-futures_2.10/jars/dispatch-futures_2.10-0.8.10.jar"/>
<classpathentry kind="lib" path="/home/mfarwell/.ivy2/cache/org.scala-lang/scala-actors/jars/scala-actors-2.10.2.jar"/>
<classpathentry kind="lib" path="/home/mfarwell/.ivy2/cache/org.scalastyle/scalastyle_2.10/jars/scalastyle_2.10-0.6.0-SNAPSHOT.jar"/>
<classpathentry kind="lib" path="/home/mfarwell/.ivy2/cache/org.scalariform/scalariform_2.10/jars/scalariform_2.10-0.1.4.jar"/>
<classpathentry kind="con" path="org.scala-ide.sdt.launching.SCALA_CONTAINER"/>
<classpathentry kind="lib" path="/home/mfarwell/.ivy2/cache/com.typesafe/config/bundles/config-1.2.0.jar"/>
Expand Down
2 changes: 1 addition & 1 deletion .settings/org.scala-ide.sdt.core.prefs
@@ -1,5 +1,5 @@
#Generated by sbteclipse
#Thu Jun 26 08:38:01 CEST 2014
#Mon Aug 18 02:41:10 CEST 2014
deprecation=true
unchecked=true
scala.compiler.useProjectSettings=true
6 changes: 3 additions & 3 deletions build.sbt
Expand Up @@ -6,7 +6,7 @@ organization := "org.scalastyle"

name := "scalastyle-sbt-plugin"

version := "0.5.0"
version := "0.6.0-SNAPSHOT"

//import com.typesafe.sbt.SbtGit._

Expand All @@ -28,12 +28,12 @@ resolvers += "sonatype-snapshots" at "https://oss.sonatype.org/content/repositor

resolvers += "sonatype-releases" at "https://oss.sonatype.org/content/repositories/releases/"

//resolvers += "Local Maven Repository" at "file://"+Path.userHome.absolutePath+"/dev/repo/"
resolvers += "Local Maven Repository" at "file://"+Path.userHome.absolutePath+"/dev/repo/"

//publishTo := Some("Sonatype Snapshots Nexus" at "https://oss.sonatype.org/content/repositories/snapshots")

libraryDependencies ++= Seq(
"org.scalastyle" %% "scalastyle" % "0.5.0"
"org.scalastyle" %% "scalastyle" % "0.6.0-SNAPSHOT"
)

publishTo <<= version { (v: String) =>
Expand Down
4 changes: 2 additions & 2 deletions project/plugins.sbt
@@ -1,6 +1,6 @@
resolvers += "sonatype-snapshots" at "https://oss.sonatype.org/content/repositories/releases/"
//resolvers += "sonatype-snapshots" at "https://oss.sonatype.org/content/repositories/releases/"

addSbtPlugin("org.scalastyle" % "scalastyle-sbt-plugin" % "0.5.0")
//addSbtPlugin("org.scalastyle" % "scalastyle-sbt-plugin" % "0.5.0")

resolvers += "jgit-repo" at "http://download.eclipse.org/jgit/maven"

Expand Down
24 changes: 14 additions & 10 deletions src/main/scala/Plugin.scala
Expand Up @@ -49,6 +49,7 @@ import sbt.richFile
import sbt.std.TaskStreams
import sbt.ScopedKey
import com.typesafe.config.ConfigFactory
import com.typesafe.config.Config

object ScalastylePlugin extends Plugin {
import PluginKeys._ // scalastyle:ignore import.grouping underscore.import
Expand Down Expand Up @@ -87,18 +88,20 @@ object Tasks {
streams: TaskStreams[ScopedKey[_]]): Unit = {
val logger = streams.log
if (config.exists) {
val messageConfig = ConfigFactory.load(new ScalastyleChecker().getClass().getClassLoader())
//streams.log.error("messageConfig=" + messageConfig.root().render())
val messages = runScalastyle(config, sourceDir)

saveToXml(messages, output.absolutePath)
saveToXml(messageConfig, messages, output.absolutePath)

val warnError = args.exists(_ == "w")
val result = printResults(logger, messages, quiet = args.exists(_ == "q"),
val result = printResults(messageConfig, logger, messages, quiet = args.exists(_ == "q"),
warnError = warnError)
logger.success("created: %s".format(target))

def onHasErrors(message: String): Unit = {
if (failOnError) {
error(message)
sys.error(message)
} else {
logger.error(message)
}
Expand All @@ -123,11 +126,11 @@ object Tasks {
new ScalastyleChecker().checkFiles(configuration, Directory.getFiles(None, List(sourceDir)))
}

private[this] def printResults(logger: Logger, messages: List[Message[FileSpec]], quiet: Boolean = false, warnError: Boolean = false): OutputResult = {
private[this] def printResults(config: Config, logger: Logger, messages: List[Message[FileSpec]], quiet: Boolean = false, warnError: Boolean = false): OutputResult = {
def now: Long = new Date().getTime
val start = now
val outputResult =
new SbtLogOutput(logger, warnError = warnError).output(messages)
new SbtLogOutput(config, logger, warnError = warnError).output(messages)
// scalastyle:off regex
if (!quiet) {
logger.info("Processed " + outputResult.files + " file(s)")
Expand All @@ -141,8 +144,8 @@ object Tasks {
outputResult
}

private[this] def saveToXml(messages: List[Message[FileSpec]], path: String)(implicit codec: Codec): Unit = {
XmlOutput.save(path, codec.charSet.toString, messages)
private[this] def saveToXml(config: Config, messages: List[Message[FileSpec]], path: String)(implicit codec: Codec): Unit = {
XmlOutput.save(config, path, codec.charSet.toString, messages)
}

private[this] implicit def enumToIterator[A](e: java.util.Enumeration[A]): Iterator[A] = new Iterator[A] {
Expand Down Expand Up @@ -190,23 +193,24 @@ object Tasks {
* @todo factor with TextOutput from scalastyle Output.scala
*/
private[sbt]
class SbtLogOutput[T <: FileSpec](logger: Logger, warnError: Boolean = false)
class SbtLogOutput[T <: FileSpec](config: Config, logger: Logger, warnError: Boolean = false)
extends Output[T] {
import org.scalastyle.{
StartWork, EndWork, StartFile, EndFile, StyleError, StyleException,
Level, ErrorLevel, WarningLevel, InfoLevel, MessageHelper
}

private val messageHelper = new MessageHelper(ConfigFactory.load())
private val messageHelper = new MessageHelper(config)

override def message(m: Message[T]): Unit = m match {
case StartWork() => logger.verbose("Starting scalastyle")
case EndWork() =>
case StartFile(file) => logger.verbose("start file " + file)
case EndFile(file) => logger.verbose("end file " + file)
case StyleError(file, clazz, key, level, args, line, column, customMessage) =>
case StyleError(file, clazz, key, level, args, line, column, customMessage) => {
plevel(level)(location(file, line, column) + ": " +
Output.findMessage(messageHelper, key, args, customMessage))
}
case StyleException(file, clazz, message, stacktrace, line, column) =>
logger.error(location(file, line, column) + ": " + message)
}
Expand Down
18 changes: 18 additions & 0 deletions src/sbt-test/messages/real-messages/build.sbt
@@ -0,0 +1,18 @@
org.scalastyle.sbt.ScalastylePlugin.Settings

version := "0.1"

scalaVersion := "2.10.0"

val containsMessage = taskKey[Boolean]("contains message")

containsMessage := {
val search = "File length exceeds"
val filename = "target/scalastyle-result.xml"
val lines = sbt.IO.readLines(file(filename))
val contains = lines.find(s => s.contains(search)).isDefined
if (!contains) {
error("Could not find " + search + " in " + filename)
}
contains
}
@@ -0,0 +1 @@
sbt.version=0.13.0
9 changes: 9 additions & 0 deletions src/sbt-test/messages/real-messages/project/plugins.sbt
@@ -0,0 +1,9 @@
resolvers += "sonatype-snapshots" at "https://oss.sonatype.org/content/repositories/snapshots/"

{
val pluginVersion = System.getProperty("plugin.version")
if(pluginVersion == null)
throw new RuntimeException("""|The system property 'plugin.version' is not defined.
|Specify this property using the scriptedLaunchOpts -D.""".stripMargin)
else addSbtPlugin("org.scalastyle" % "scalastyle-sbt-plugin" % pluginVersion)
}
13 changes: 13 additions & 0 deletions src/sbt-test/messages/real-messages/scalastyle-config.xml
@@ -0,0 +1,13 @@
<scalastyle>
<name>Scalastyle standard configuration</name>
<check level="warning" class="org.scalastyle.file.FileLengthChecker" enabled="true">
<parameters>
<parameter name="maxFileLength"><![CDATA[5]]></parameter>
</parameters>
</check>
<check level="warning" class="org.scalastyle.scalariform.ObjectNamesChecker" enabled="true">
<parameters>
<parameter name="regex"><![CDATA[[A-Z][A-Za-z]*]]></parameter>
</parameters>
</check>
</scalastyle>
@@ -0,0 +1,9 @@

object Main extends App {
println("hello")
}

object foo {
println("hello")
}

4 changes: 4 additions & 0 deletions src/sbt-test/messages/real-messages/test
@@ -0,0 +1,4 @@
# scalastyle alternative config file
> clean
> scalastyle
> containsMessage

0 comments on commit 85b2a53

Please sign in to comment.