From 5b6bc3d9c8a5eb2b0b9f2540ea67ecee1670b5f1 Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Fri, 18 Dec 2015 12:43:29 -0500 Subject: [PATCH] Fixes #16: Make sbt-checkstyle-plugin an AutoPlugin --- README.md | 8 ++-- .../com/etsy/sbt/checkstyle/Checkstyle.scala | 39 ++----------------- .../sbt/checkstyle/CheckstyleConfig.scala | 27 +++++++++++++ .../checkstyle/CheckstyleSeverityLevel.scala | 9 +++++ .../checkstyle-check-empty/build.sbt | 3 +- .../checkstyle/checkstyle-check/build.sbt | 5 +-- .../build.sbt | 3 +- .../checkstyle-classpath-config/build.sbt | 3 +- .../checkstyle-integration-test/build.sbt | 9 ++--- .../checkstyle-remote-config/build.sbt | 3 +- .../checkstyle/checkstyle-test/build.sbt | 6 +-- src/sbt-test/checkstyle/checkstyle/build.sbt | 1 - src/sbt-test/checkstyle/xslt/build.sbt | 1 - 13 files changed, 53 insertions(+), 64 deletions(-) create mode 100644 src/main/scala/com/etsy/sbt/checkstyle/CheckstyleConfig.scala create mode 100644 src/main/scala/com/etsy/sbt/checkstyle/CheckstyleSeverityLevel.scala diff --git a/README.md b/README.md index e10d74c..1079c9c 100644 --- a/README.md +++ b/README.md @@ -17,12 +17,12 @@ Add the following lines to `project/plugins.sbt`: addSbtPlugin("com.etsy" % "sbt-checkstyle-plugin" % "1.0.0") ``` -Then add the following lines to `build.sbt`: +sbt-checkstyle-plugin is an AutoPlugin, so there is no need to modify the `build.sbt` file to enable it. + +If you want to modify any of the default settings, you should add the following import to `build.sbt`, however: ```scala import com.etsy.sbt.checkstyle._ - -Checkstyle.checkstyleSettings ``` ## Usage @@ -65,8 +65,6 @@ The `xsltTransformations` setting allows applying XSLT transformations to the XM You can set `xsltTransformations` like so in `build.sbt`: ```scala -import com.etsy.sbt.checkstyle._ - Checkstyle.xsltTransformations := { Some(Set(XSLTSettings(baseDirectory(_ / "checkstyle-noframes.xml").value, target(_ / "checkstyle-report.html").value))) } diff --git a/src/main/scala/com/etsy/sbt/checkstyle/Checkstyle.scala b/src/main/scala/com/etsy/sbt/checkstyle/Checkstyle.scala index ef75d51..ba7a290 100644 --- a/src/main/scala/com/etsy/sbt/checkstyle/Checkstyle.scala +++ b/src/main/scala/com/etsy/sbt/checkstyle/Checkstyle.scala @@ -2,14 +2,13 @@ package com.etsy.sbt.checkstyle import javax.xml.transform.stream.StreamSource +import com.etsy.sbt.checkstyle.CheckstyleSeverityLevel.CheckstyleSeverityLevel import com.puppycrawl.tools.checkstyle.Main.{main => CheckstyleMain} import net.sf.saxon.s9api.Processor import sbt.Def.Initialize import sbt.Keys._ import sbt._ -import scala.io.Source - /** * An SBT plugin to run checkstyle over Java code * @@ -17,38 +16,8 @@ import scala.io.Source * @author Alejandro Rivera * @author Joseph Earl */ -object Checkstyle extends Plugin { - sealed abstract class CheckstyleConfig(val location: String) { - def read(resources: Seq[File]): String - } - - object CheckstyleConfig { - case class URL(url: String) extends CheckstyleConfig(url) { - override def read(resources: Seq[sbt.File]): String = Source.fromURL(url).mkString - } - - case class File(path: String) extends CheckstyleConfig(path) { - override def read(resources: Seq[sbt.File]): String = Source.fromFile(path).mkString - } - - case class Classpath(name: String) extends CheckstyleConfig(name) { - override def read(resources: Seq[sbt.File]): String = { - val classpath = resources.map((f) => f.toURI.toURL) - val loader = new java.net.URLClassLoader(classpath.toArray, getClass.getClassLoader) - Source.fromInputStream(loader.getResourceAsStream(name)).mkString - } - } - } - - object CheckstyleSeverityLevel extends Enumeration { - type CheckstyleSeverityLevel = Value - val Ignore = Value("ignore") - val Info = Value("info") - val Warning = Value("warning") - val Error = Value("error") - } - - import Checkstyle.CheckstyleSeverityLevel._ +object Checkstyle extends AutoPlugin { + override def trigger: PluginTrigger = allRequirements val checkstyle = TaskKey[Unit]("checkstyle", "Runs checkstyle") val outputFile = SettingKey[File]("checkstyle-target", "The location of the generated checkstyle report") @@ -166,7 +135,7 @@ object Checkstyle extends Plugin { } } - val checkstyleSettings: Seq[Def.Setting[_]] = Seq( + override def projectSettings: Seq[Def.Setting[_]] = Seq( outputFile <<= target(_ / "checkstyle-report.xml"), outputFile in Test <<= target(_ / "checkstyle-test-report.xml"), configLocation := CheckstyleConfig.File("checkstyle-config.xml"), diff --git a/src/main/scala/com/etsy/sbt/checkstyle/CheckstyleConfig.scala b/src/main/scala/com/etsy/sbt/checkstyle/CheckstyleConfig.scala new file mode 100644 index 0000000..c964739 --- /dev/null +++ b/src/main/scala/com/etsy/sbt/checkstyle/CheckstyleConfig.scala @@ -0,0 +1,27 @@ +package com.etsy.sbt.checkstyle + +import sbt.File + +import scala.io.Source + +sealed abstract class CheckstyleConfig(val location: String) { + def read(resources: Seq[File]): String + } + + object CheckstyleConfig { + case class URL(url: String) extends CheckstyleConfig(url) { + override def read(resources: Seq[sbt.File]): String = Source.fromURL(url).mkString + } + + case class File(path: String) extends CheckstyleConfig(path) { + override def read(resources: Seq[sbt.File]): String = Source.fromFile(path).mkString + } + + case class Classpath(name: String) extends CheckstyleConfig(name) { + override def read(resources: Seq[sbt.File]): String = { + val classpath = resources.map((f) => f.toURI.toURL) + val loader = new java.net.URLClassLoader(classpath.toArray, getClass.getClassLoader) + Source.fromInputStream(loader.getResourceAsStream(name)).mkString + } + } + } diff --git a/src/main/scala/com/etsy/sbt/checkstyle/CheckstyleSeverityLevel.scala b/src/main/scala/com/etsy/sbt/checkstyle/CheckstyleSeverityLevel.scala new file mode 100644 index 0000000..aac2b5d --- /dev/null +++ b/src/main/scala/com/etsy/sbt/checkstyle/CheckstyleSeverityLevel.scala @@ -0,0 +1,9 @@ +package com.etsy.sbt.checkstyle + +object CheckstyleSeverityLevel extends Enumeration { + type CheckstyleSeverityLevel = Value + val Ignore = Value("ignore") + val Info = Value("info") + val Warning = Value("warning") + val Error = Value("error") +} diff --git a/src/sbt-test/checkstyle/checkstyle-check-empty/build.sbt b/src/sbt-test/checkstyle/checkstyle-check-empty/build.sbt index e245272..508e352 100755 --- a/src/sbt-test/checkstyle/checkstyle-check-empty/build.sbt +++ b/src/sbt-test/checkstyle/checkstyle-check-empty/build.sbt @@ -6,5 +6,4 @@ organization := "com.etsy" import com.etsy.sbt.checkstyle._ -Checkstyle.checkstyleSettings -Checkstyle.severityLevel := Some(Checkstyle.CheckstyleSeverityLevel.Error) +Checkstyle.severityLevel := Some(CheckstyleSeverityLevel.Error) diff --git a/src/sbt-test/checkstyle/checkstyle-check/build.sbt b/src/sbt-test/checkstyle/checkstyle-check/build.sbt index c647388..15d451f 100755 --- a/src/sbt-test/checkstyle/checkstyle-check/build.sbt +++ b/src/sbt-test/checkstyle/checkstyle-check/build.sbt @@ -6,6 +6,5 @@ organization := "com.etsy" import com.etsy.sbt.checkstyle._ -Checkstyle.checkstyleSettings -Checkstyle.configLocation := Checkstyle.CheckstyleConfig.File("my-checkstyle-config.xml") -Checkstyle.severityLevel := Some(Checkstyle.CheckstyleSeverityLevel.Error) +Checkstyle.configLocation := CheckstyleConfig.File("my-checkstyle-config.xml") +Checkstyle.severityLevel := Some(CheckstyleSeverityLevel.Error) diff --git a/src/sbt-test/checkstyle/checkstyle-classpath-config-dependency/build.sbt b/src/sbt-test/checkstyle/checkstyle-classpath-config-dependency/build.sbt index 716d5f9..d8fa019 100755 --- a/src/sbt-test/checkstyle/checkstyle-classpath-config-dependency/build.sbt +++ b/src/sbt-test/checkstyle/checkstyle-classpath-config-dependency/build.sbt @@ -8,5 +8,4 @@ libraryDependencies += "com.puppycrawl.tools" % "checkstyle" % "6.13" import com.etsy.sbt.checkstyle._ -Checkstyle.checkstyleSettings -Checkstyle.configLocation := Checkstyle.CheckstyleConfig.Classpath("google_checks.xml") +Checkstyle.configLocation := CheckstyleConfig.Classpath("google_checks.xml") diff --git a/src/sbt-test/checkstyle/checkstyle-classpath-config/build.sbt b/src/sbt-test/checkstyle/checkstyle-classpath-config/build.sbt index 867f04f..8482c44 100755 --- a/src/sbt-test/checkstyle/checkstyle-classpath-config/build.sbt +++ b/src/sbt-test/checkstyle/checkstyle-classpath-config/build.sbt @@ -6,5 +6,4 @@ organization := "com.etsy" import com.etsy.sbt.checkstyle._ -Checkstyle.checkstyleSettings -Checkstyle.configLocation := Checkstyle.CheckstyleConfig.Classpath("com/etsy/sbt/google_checks.xml") +Checkstyle.configLocation := CheckstyleConfig.Classpath("com/etsy/sbt/google_checks.xml") diff --git a/src/sbt-test/checkstyle/checkstyle-integration-test/build.sbt b/src/sbt-test/checkstyle/checkstyle-integration-test/build.sbt index 71ab045..73893db 100755 --- a/src/sbt-test/checkstyle/checkstyle-integration-test/build.sbt +++ b/src/sbt-test/checkstyle/checkstyle-integration-test/build.sbt @@ -10,9 +10,6 @@ Defaults.itSettings import com.etsy.sbt.checkstyle._ -Checkstyle.configLocation := Checkstyle.CheckstyleConfig.File("my-checkstyle-config.xml") -Checkstyle.checkstyleSettings ++ Seq( - Checkstyle.configLocation := Checkstyle.CheckstyleConfig.File("test-checkstyle-config.xml"), - Checkstyle.checkstyle in IntegrationTest <<= Checkstyle.checkstyleTask(IntegrationTest), - Checkstyle.outputFile in IntegrationTest <<= target(_ / "checkstyle-integration-test-report.xml") -) +Checkstyle.configLocation := CheckstyleConfig.File("my-checkstyle-config.xml") +Checkstyle.checkstyle in IntegrationTest <<= Checkstyle.checkstyleTask(IntegrationTest) +Checkstyle.outputFile in IntegrationTest <<= target(_ / "checkstyle-integration-test-report.xml") diff --git a/src/sbt-test/checkstyle/checkstyle-remote-config/build.sbt b/src/sbt-test/checkstyle/checkstyle-remote-config/build.sbt index 3393f07..86fabe5 100755 --- a/src/sbt-test/checkstyle/checkstyle-remote-config/build.sbt +++ b/src/sbt-test/checkstyle/checkstyle-remote-config/build.sbt @@ -6,6 +6,5 @@ organization := "com.etsy" import com.etsy.sbt.checkstyle._ -Checkstyle.checkstyleSettings Checkstyle.configLocation := - Checkstyle.CheckstyleConfig.URL("https://raw.githubusercontent.com/etsy/sbt-checkstyle-plugin/master/src/sbt-test/checkstyle/checkstyle/checkstyle-config.xml") + CheckstyleConfig.URL("https://raw.githubusercontent.com/etsy/sbt-checkstyle-plugin/master/src/sbt-test/checkstyle/checkstyle/checkstyle-config.xml") diff --git a/src/sbt-test/checkstyle/checkstyle-test/build.sbt b/src/sbt-test/checkstyle/checkstyle-test/build.sbt index 132602e..9b114ce 100644 --- a/src/sbt-test/checkstyle/checkstyle-test/build.sbt +++ b/src/sbt-test/checkstyle/checkstyle-test/build.sbt @@ -2,8 +2,4 @@ version := "0.1" name := "checkstyle-test" -organization := "com.etsy" - -import com.etsy.sbt.checkstyle._ - -Checkstyle.checkstyleSettings +organization := "com.etsy" \ No newline at end of file diff --git a/src/sbt-test/checkstyle/checkstyle/build.sbt b/src/sbt-test/checkstyle/checkstyle/build.sbt index 95db97f..ce9dd21 100644 --- a/src/sbt-test/checkstyle/checkstyle/build.sbt +++ b/src/sbt-test/checkstyle/checkstyle/build.sbt @@ -6,5 +6,4 @@ organization := "com.etsy" import com.etsy.sbt.checkstyle._ -Checkstyle.checkstyleSettings Checkstyle.configLocation := CheckstyleConfig.File("checkstyle-config.xml") diff --git a/src/sbt-test/checkstyle/xslt/build.sbt b/src/sbt-test/checkstyle/xslt/build.sbt index c522d71..6657ac0 100644 --- a/src/sbt-test/checkstyle/xslt/build.sbt +++ b/src/sbt-test/checkstyle/xslt/build.sbt @@ -6,7 +6,6 @@ organization := "com.etsy" import com.etsy.sbt.checkstyle._ -Checkstyle.checkstyleSettings Checkstyle.xsltTransformations := { Some(Set(XSLTSettings(baseDirectory(_ / "checkstyle-noframes.xml").value, target(_ / "checkstyle-report.html").value))) }