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