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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1046
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
     1
configurations {
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
     2
    codequality
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
     3
}
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
     4
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
     5
dependencies {
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
     6
    codequality 'com.puppycrawl.tools:checkstyle:6.1'
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
     7
}
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
     8
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
     9
task checkstyle(type: AndroidCheckstyleTask) {
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    10
    ignoreFailures true
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    11
    showViolations false
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    12
    configFile file("$project.rootDir/tools/checkstyle.xml")
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    13
    xslFile file("$project.rootDir/tools/checkstyle-noframes-sorted.xsl")
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    14
}
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    15
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    16
task checkstyleCmdLine(type: AndroidCheckstyleTask) {
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    17
    ignoreFailures false
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    18
    showViolations true
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    19
    configFile file("$project.rootDir/tools/checkstyle.xml")
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    20
    xslFile file("$project.rootDir/tools/checkstyle-noframes-sorted.xsl")
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    21
}
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    22
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    23
check.dependsOn(checkstyle)
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    24
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    25
///////////////////////////////////////////////
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    26
////////////// Groovy Task Class //////////////
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    27
///////////////////////////////////////////////
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    28
import org.gradle.api.internal.project.IsolatedAntBuilder
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    29
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    30
/**
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    31
 * See parameters at http://checkstyle.sourceforge.net/anttask.html
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    32
 */
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    33
class AndroidCheckstyleTask extends DefaultTask {
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    34
    @InputFile
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    35
    @Optional
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    36
    File configFile = new File("$project.rootDir/config/checkstyle/checkstyle.xml")
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    37
    @InputFile
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    38
    @Optional
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    39
    File xslFile = new File("$project.rootDir/config/checkstyle/checkstyle-noframes-sorted.xsl")
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    40
    @OutputFile
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    41
    @Optional
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    42
    File outputFile = new File("$project.buildDir/reports/checkstyle/checkstyle-${project.name}.xml")
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    43
    def outputHtmlFile = outputFile.absolutePath.replaceFirst(~/\.[^\.]+$/, ".html")
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    44
    FileCollection checkstyleClasspath = project.configurations.codequality
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    45
    Boolean ignoreFailures = false
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    46
    Boolean showViolations = true
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    47
    Project gradleProject = project
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    48
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    49
    def AndroidCheckstyleTask() {
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    50
        description = 'Runs checkstyle against Android sourcesets.'
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    51
        group = 'Code Quality'
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    52
    }
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    53
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    54
    @TaskAction
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    55
    def runCheckstyle() {
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    56
        outputFile.parentFile.mkdirs()
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    57
        def antBuilder = services.get(IsolatedAntBuilder)
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    58
        antBuilder.withClasspath(checkstyleClasspath).execute {
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    59
            ant.taskdef(name: 'checkstyle', classname: 'com.puppycrawl.tools.checkstyle.CheckStyleTask')
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    60
            // see also, maxWarnings and failureProperty arguments
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    61
            ant.checkstyle(config: configFile, failOnViolation: !ignoreFailures) {
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    62
                gradleProject.allprojects.each { submodule ->
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    63
                    submodule.android.sourceSets.each { sourceSet ->
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    64
                        sourceSet.java.each { file ->
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    65
                            file.getSrcDirs().each {
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    66
                                if (it.exists()) {
1068
5e3cb33d9fe4 Exclude com.gogle.iosched from the checkstyle task
Da Risk <da_risk@beem-project.com>
parents: 1046
diff changeset
    67
                                    fileset(dir: it) {
5e3cb33d9fe4 Exclude com.gogle.iosched from the checkstyle task
Da Risk <da_risk@beem-project.com>
parents: 1046
diff changeset
    68
                                        exclude (name: '**/com/google/android/apps/iosched/util/*.java')
5e3cb33d9fe4 Exclude com.gogle.iosched from the checkstyle task
Da Risk <da_risk@beem-project.com>
parents: 1046
diff changeset
    69
                                    }
1046
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    70
                                }
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    71
                            }
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    72
                        }
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    73
                    }
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    74
                }
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    75
                if (showViolations) {
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    76
                    formatter(type: 'plain', useFile: false)
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    77
                }
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    78
                formatter(type: 'xml', toFile: outputFile)
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    79
            }
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    80
            if (outputFile.exists()) {
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    81
                ant.xslt(in: outputFile,
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    82
                        style: xslFile,
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    83
                        out: outputHtmlFile
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    84
                )
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    85
            }
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    86
        }
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    87
    }
6ca974ea549b Add checkstyle configuration to gradle
Da Risk <da_risk@beem-project.com>
parents:
diff changeset
    88
}