tools/android-checkstyle.gradle
changeset 1042 6ca974ea549b
child 1064 5e3cb33d9fe4
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tools/android-checkstyle.gradle	Sun Mar 15 18:57:24 2015 +0100
@@ -0,0 +1,86 @@
+configurations {
+    codequality
+}
+
+dependencies {
+    codequality 'com.puppycrawl.tools:checkstyle:6.1'
+}
+
+task checkstyle(type: AndroidCheckstyleTask) {
+    ignoreFailures true
+    showViolations false
+    configFile file("$project.rootDir/tools/checkstyle.xml")
+    xslFile file("$project.rootDir/tools/checkstyle-noframes-sorted.xsl")
+}
+
+task checkstyleCmdLine(type: AndroidCheckstyleTask) {
+    ignoreFailures false
+    showViolations true
+    configFile file("$project.rootDir/tools/checkstyle.xml")
+    xslFile file("$project.rootDir/tools/checkstyle-noframes-sorted.xsl")
+}
+
+check.dependsOn(checkstyle)
+
+///////////////////////////////////////////////
+////////////// Groovy Task Class //////////////
+///////////////////////////////////////////////
+import org.gradle.api.internal.project.IsolatedAntBuilder
+
+/**
+ * See parameters at http://checkstyle.sourceforge.net/anttask.html
+ */
+class AndroidCheckstyleTask extends DefaultTask {
+    @InputFile
+    @Optional
+    File configFile = new File("$project.rootDir/config/checkstyle/checkstyle.xml")
+    @InputFile
+    @Optional
+    File xslFile = new File("$project.rootDir/config/checkstyle/checkstyle-noframes-sorted.xsl")
+    @OutputFile
+    @Optional
+    File outputFile = new File("$project.buildDir/reports/checkstyle/checkstyle-${project.name}.xml")
+    def outputHtmlFile = outputFile.absolutePath.replaceFirst(~/\.[^\.]+$/, ".html")
+    FileCollection checkstyleClasspath = project.configurations.codequality
+    Boolean ignoreFailures = false
+    Boolean showViolations = true
+    Project gradleProject = project
+
+    def AndroidCheckstyleTask() {
+        description = 'Runs checkstyle against Android sourcesets.'
+        group = 'Code Quality'
+    }
+
+    @TaskAction
+    def runCheckstyle() {
+        outputFile.parentFile.mkdirs()
+        def antBuilder = services.get(IsolatedAntBuilder)
+        antBuilder.withClasspath(checkstyleClasspath).execute {
+            ant.taskdef(name: 'checkstyle', classname: 'com.puppycrawl.tools.checkstyle.CheckStyleTask')
+            // see also, maxWarnings and failureProperty arguments
+            ant.checkstyle(config: configFile, failOnViolation: !ignoreFailures) {
+                gradleProject.allprojects.each { submodule ->
+                    submodule.android.sourceSets.each { sourceSet ->
+                        sourceSet.java.each { file ->
+                            file.getSrcDirs().each {
+                                if (it.exists()) {
+                                    fileset(dir: it)
+                                }
+                            }
+                        }
+                    }
+                }
+                if (showViolations) {
+                    formatter(type: 'plain', useFile: false)
+                }
+                formatter(type: 'xml', toFile: outputFile)
+            }
+            if (outputFile.exists()) {
+                ant.xslt(in: outputFile,
+                        style: xslFile,
+                        out: outputHtmlFile
+                )
+            }
+        }
+    }
+}