tools/android-checkstyle.gradle
author Da Risk <da_risk@beem-project.com>
Tue, 16 Jun 2015 09:48:12 +0200
changeset 1068 5e3cb33d9fe4
parent 1046 6ca974ea549b
permissions -rw-r--r--
Exclude com.gogle.iosched from the checkstyle task

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) {
                                        exclude (name: '**/com/google/android/apps/iosched/util/*.java')
                                    }
                                }
                            }
                        }
                    }
                }
                if (showViolations) {
                    formatter(type: 'plain', useFile: false)
                }
                formatter(type: 'xml', toFile: outputFile)
            }
            if (outputFile.exists()) {
                ant.xslt(in: outputFile,
                        style: xslFile,
                        out: outputHtmlFile
                )
            }
        }
    }
}