Skip to content

Commit 5b6bc3d

Browse files
committed
Fixes etsy#16: Make sbt-checkstyle-plugin an AutoPlugin
1 parent 0aea2e5 commit 5b6bc3d

File tree

13 files changed

+53
-64
lines changed

13 files changed

+53
-64
lines changed

README.md

+3-5
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ Add the following lines to `project/plugins.sbt`:
1717
addSbtPlugin("com.etsy" % "sbt-checkstyle-plugin" % "1.0.0")
1818
```
1919

20-
Then add the following lines to `build.sbt`:
20+
sbt-checkstyle-plugin is an AutoPlugin, so there is no need to modify the `build.sbt` file to enable it.
21+
22+
If you want to modify any of the default settings, you should add the following import to `build.sbt`, however:
2123

2224
```scala
2325
import com.etsy.sbt.checkstyle._
24-
25-
Checkstyle.checkstyleSettings
2626
```
2727

2828
## Usage
@@ -65,8 +65,6 @@ The `xsltTransformations` setting allows applying XSLT transformations to the XM
6565

6666
You can set `xsltTransformations` like so in `build.sbt`:
6767
```scala
68-
import com.etsy.sbt.checkstyle._
69-
7068
Checkstyle.xsltTransformations := {
7169
Some(Set(XSLTSettings(baseDirectory(_ / "checkstyle-noframes.xml").value, target(_ / "checkstyle-report.html").value)))
7270
}

src/main/scala/com/etsy/sbt/checkstyle/Checkstyle.scala

+4-35
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,22 @@ package com.etsy.sbt.checkstyle
22

33
import javax.xml.transform.stream.StreamSource
44

5+
import com.etsy.sbt.checkstyle.CheckstyleSeverityLevel.CheckstyleSeverityLevel
56
import com.puppycrawl.tools.checkstyle.Main.{main => CheckstyleMain}
67
import net.sf.saxon.s9api.Processor
78
import sbt.Def.Initialize
89
import sbt.Keys._
910
import sbt._
1011

11-
import scala.io.Source
12-
1312
/**
1413
* An SBT plugin to run checkstyle over Java code
1514
*
1615
* @author Andrew Johnson <ajohnson@etsy.com>
1716
* @author Alejandro Rivera <alejandro.rivera.lopez@gmail.com>
1817
* @author Joseph Earl <joe@josephearl.co.uk>
1918
*/
20-
object Checkstyle extends Plugin {
21-
sealed abstract class CheckstyleConfig(val location: String) {
22-
def read(resources: Seq[File]): String
23-
}
24-
25-
object CheckstyleConfig {
26-
case class URL(url: String) extends CheckstyleConfig(url) {
27-
override def read(resources: Seq[sbt.File]): String = Source.fromURL(url).mkString
28-
}
29-
30-
case class File(path: String) extends CheckstyleConfig(path) {
31-
override def read(resources: Seq[sbt.File]): String = Source.fromFile(path).mkString
32-
}
33-
34-
case class Classpath(name: String) extends CheckstyleConfig(name) {
35-
override def read(resources: Seq[sbt.File]): String = {
36-
val classpath = resources.map((f) => f.toURI.toURL)
37-
val loader = new java.net.URLClassLoader(classpath.toArray, getClass.getClassLoader)
38-
Source.fromInputStream(loader.getResourceAsStream(name)).mkString
39-
}
40-
}
41-
}
42-
43-
object CheckstyleSeverityLevel extends Enumeration {
44-
type CheckstyleSeverityLevel = Value
45-
val Ignore = Value("ignore")
46-
val Info = Value("info")
47-
val Warning = Value("warning")
48-
val Error = Value("error")
49-
}
50-
51-
import Checkstyle.CheckstyleSeverityLevel._
19+
object Checkstyle extends AutoPlugin {
20+
override def trigger: PluginTrigger = allRequirements
5221

5322
val checkstyle = TaskKey[Unit]("checkstyle", "Runs checkstyle")
5423
val outputFile = SettingKey[File]("checkstyle-target", "The location of the generated checkstyle report")
@@ -166,7 +135,7 @@ object Checkstyle extends Plugin {
166135
}
167136
}
168137

169-
val checkstyleSettings: Seq[Def.Setting[_]] = Seq(
138+
override def projectSettings: Seq[Def.Setting[_]] = Seq(
170139
outputFile <<= target(_ / "checkstyle-report.xml"),
171140
outputFile in Test <<= target(_ / "checkstyle-test-report.xml"),
172141
configLocation := CheckstyleConfig.File("checkstyle-config.xml"),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.etsy.sbt.checkstyle
2+
3+
import sbt.File
4+
5+
import scala.io.Source
6+
7+
sealed abstract class CheckstyleConfig(val location: String) {
8+
def read(resources: Seq[File]): String
9+
}
10+
11+
object CheckstyleConfig {
12+
case class URL(url: String) extends CheckstyleConfig(url) {
13+
override def read(resources: Seq[sbt.File]): String = Source.fromURL(url).mkString
14+
}
15+
16+
case class File(path: String) extends CheckstyleConfig(path) {
17+
override def read(resources: Seq[sbt.File]): String = Source.fromFile(path).mkString
18+
}
19+
20+
case class Classpath(name: String) extends CheckstyleConfig(name) {
21+
override def read(resources: Seq[sbt.File]): String = {
22+
val classpath = resources.map((f) => f.toURI.toURL)
23+
val loader = new java.net.URLClassLoader(classpath.toArray, getClass.getClassLoader)
24+
Source.fromInputStream(loader.getResourceAsStream(name)).mkString
25+
}
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.etsy.sbt.checkstyle
2+
3+
object CheckstyleSeverityLevel extends Enumeration {
4+
type CheckstyleSeverityLevel = Value
5+
val Ignore = Value("ignore")
6+
val Info = Value("info")
7+
val Warning = Value("warning")
8+
val Error = Value("error")
9+
}

src/sbt-test/checkstyle/checkstyle-check-empty/build.sbt

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@ organization := "com.etsy"
66

77
import com.etsy.sbt.checkstyle._
88

9-
Checkstyle.checkstyleSettings
10-
Checkstyle.severityLevel := Some(Checkstyle.CheckstyleSeverityLevel.Error)
9+
Checkstyle.severityLevel := Some(CheckstyleSeverityLevel.Error)

src/sbt-test/checkstyle/checkstyle-check/build.sbt

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,5 @@ organization := "com.etsy"
66

77
import com.etsy.sbt.checkstyle._
88

9-
Checkstyle.checkstyleSettings
10-
Checkstyle.configLocation := Checkstyle.CheckstyleConfig.File("my-checkstyle-config.xml")
11-
Checkstyle.severityLevel := Some(Checkstyle.CheckstyleSeverityLevel.Error)
9+
Checkstyle.configLocation := CheckstyleConfig.File("my-checkstyle-config.xml")
10+
Checkstyle.severityLevel := Some(CheckstyleSeverityLevel.Error)

src/sbt-test/checkstyle/checkstyle-classpath-config-dependency/build.sbt

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,4 @@ libraryDependencies += "com.puppycrawl.tools" % "checkstyle" % "6.13"
88

99
import com.etsy.sbt.checkstyle._
1010

11-
Checkstyle.checkstyleSettings
12-
Checkstyle.configLocation := Checkstyle.CheckstyleConfig.Classpath("google_checks.xml")
11+
Checkstyle.configLocation := CheckstyleConfig.Classpath("google_checks.xml")

src/sbt-test/checkstyle/checkstyle-classpath-config/build.sbt

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@ organization := "com.etsy"
66

77
import com.etsy.sbt.checkstyle._
88

9-
Checkstyle.checkstyleSettings
10-
Checkstyle.configLocation := Checkstyle.CheckstyleConfig.Classpath("com/etsy/sbt/google_checks.xml")
9+
Checkstyle.configLocation := CheckstyleConfig.Classpath("com/etsy/sbt/google_checks.xml")

src/sbt-test/checkstyle/checkstyle-integration-test/build.sbt

+3-6
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ Defaults.itSettings
1010

1111
import com.etsy.sbt.checkstyle._
1212

13-
Checkstyle.configLocation := Checkstyle.CheckstyleConfig.File("my-checkstyle-config.xml")
14-
Checkstyle.checkstyleSettings ++ Seq(
15-
Checkstyle.configLocation := Checkstyle.CheckstyleConfig.File("test-checkstyle-config.xml"),
16-
Checkstyle.checkstyle in IntegrationTest <<= Checkstyle.checkstyleTask(IntegrationTest),
17-
Checkstyle.outputFile in IntegrationTest <<= target(_ / "checkstyle-integration-test-report.xml")
18-
)
13+
Checkstyle.configLocation := CheckstyleConfig.File("my-checkstyle-config.xml")
14+
Checkstyle.checkstyle in IntegrationTest <<= Checkstyle.checkstyleTask(IntegrationTest)
15+
Checkstyle.outputFile in IntegrationTest <<= target(_ / "checkstyle-integration-test-report.xml")

src/sbt-test/checkstyle/checkstyle-remote-config/build.sbt

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,5 @@ organization := "com.etsy"
66

77
import com.etsy.sbt.checkstyle._
88

9-
Checkstyle.checkstyleSettings
109
Checkstyle.configLocation :=
11-
Checkstyle.CheckstyleConfig.URL("https://raw.githubusercontent.com/etsy/sbt-checkstyle-plugin/master/src/sbt-test/checkstyle/checkstyle/checkstyle-config.xml")
10+
CheckstyleConfig.URL("https://raw.githubusercontent.com/etsy/sbt-checkstyle-plugin/master/src/sbt-test/checkstyle/checkstyle/checkstyle-config.xml")

src/sbt-test/checkstyle/checkstyle-test/build.sbt

+1-5
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,4 @@ version := "0.1"
22

33
name := "checkstyle-test"
44

5-
organization := "com.etsy"
6-
7-
import com.etsy.sbt.checkstyle._
8-
9-
Checkstyle.checkstyleSettings
5+
organization := "com.etsy"

src/sbt-test/checkstyle/checkstyle/build.sbt

-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@ organization := "com.etsy"
66

77
import com.etsy.sbt.checkstyle._
88

9-
Checkstyle.checkstyleSettings
109
Checkstyle.configLocation := CheckstyleConfig.File("checkstyle-config.xml")

src/sbt-test/checkstyle/xslt/build.sbt

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ organization := "com.etsy"
66

77
import com.etsy.sbt.checkstyle._
88

9-
Checkstyle.checkstyleSettings
109
Checkstyle.xsltTransformations := {
1110
Some(Set(XSLTSettings(baseDirectory(_ / "checkstyle-noframes.xml").value, target(_ / "checkstyle-report.html").value)))
1211
}

0 commit comments

Comments
 (0)