-
Notifications
You must be signed in to change notification settings - Fork 0
/
CheckstyleSettings.scala
52 lines (47 loc) · 1.6 KB
/
CheckstyleSettings.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import sbt._
import Keys._
import play.Project._
// Adapted from https://github.com/ymasory/sbt-code-quality.g8
object CheckstyleSettings {
val checkstyle = TaskKey[Unit]("checkstyle", "run checkstyle, placing results in target/checkstyle")
val checkstyleTask = checkstyle <<=
(streams, baseDirectory, sourceDirectory in Compile, target) map {
(streams, base, src, target) =>
import com.puppycrawl.tools.checkstyle.Main.{ main => CsMain }
import streams.log
val outputDir = (target / "checkstyle").mkdirs
val outputFile = (target / "checkstyle" / "checkstyle-report.xml").getAbsolutePath
val args = List(
"-c", (base / "project" / "checkstyle-config.xml").getAbsolutePath,
"-f", "xml",
"-r", src.getAbsolutePath,
"-o", outputFile
)
log info ("Running checkstyle...")
trappingExits {
CsMain(args.toArray)
}
// Print out results.
val source = scala.io.Source.fromFile(outputFile)
log info (source.mkString)
source.close()
}
def trappingExits(thunk: => Unit): Unit = {
val originalSecManager = System.getSecurityManager
case class NoExitsException() extends SecurityException
System setSecurityManager new SecurityManager() {
import java.security.Permission
override def checkPermission(perm: Permission) {
if (perm.getName startsWith "exitVM") throw NoExitsException()
}
}
try {
thunk
} catch {
case _: NoExitsException =>
case e: Throwable => throw e
} finally {
System setSecurityManager originalSecManager
}
}
}