Skip to content

Commit

Permalink
Fixes etsy#16: Make sbt-checkstyle-plugin an AutoPlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
ajsquared committed Dec 18, 2015
1 parent 0aea2e5 commit 5b6bc3d
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 64 deletions.
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)))
}
Expand Down
39 changes: 4 additions & 35 deletions src/main/scala/com/etsy/sbt/checkstyle/Checkstyle.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,22 @@ 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
*
* @author Andrew Johnson <[email protected]>
* @author Alejandro Rivera <[email protected]>
* @author Joseph Earl <[email protected]>
*/
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")
Expand Down Expand Up @@ -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"),
Expand Down
27 changes: 27 additions & 0 deletions src/main/scala/com/etsy/sbt/checkstyle/CheckstyleConfig.scala
Original file line number Diff line number Diff line change
@@ -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
}
}
}
Original file line number Diff line number Diff line change
@@ -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")
}
3 changes: 1 addition & 2 deletions src/sbt-test/checkstyle/checkstyle-check-empty/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
5 changes: 2 additions & 3 deletions src/sbt-test/checkstyle/checkstyle-check/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Original file line number Diff line number Diff line change
Expand Up @@ -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")
9 changes: 3 additions & 6 deletions src/sbt-test/checkstyle/checkstyle-integration-test/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
3 changes: 1 addition & 2 deletions src/sbt-test/checkstyle/checkstyle-remote-config/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
6 changes: 1 addition & 5 deletions src/sbt-test/checkstyle/checkstyle-test/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,4 @@ version := "0.1"

name := "checkstyle-test"

organization := "com.etsy"

import com.etsy.sbt.checkstyle._

Checkstyle.checkstyleSettings
organization := "com.etsy"
1 change: 0 additions & 1 deletion src/sbt-test/checkstyle/checkstyle/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ organization := "com.etsy"

import com.etsy.sbt.checkstyle._

Checkstyle.checkstyleSettings
Checkstyle.configLocation := CheckstyleConfig.File("checkstyle-config.xml")
1 change: 0 additions & 1 deletion src/sbt-test/checkstyle/xslt/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
}

0 comments on commit 5b6bc3d

Please sign in to comment.