// Pmd plugin details: https://docs.gradle.org/current/userguide/pmd_plugin.html
apply plugin: 'pmd'

pmd {
    consoleOutput true
    ignoreFailures false
    rulePriority 5 // Strictest mode available
    ruleSetConfig resources.text.fromFile("$rootProject.projectDir/config/static_analysis/pmd/pmd_rules.xml")
    toolVersion '5.8.1'
    tasks.withType(Pmd) {
        reports {
            xml.enabled true
            html.enabled true
        }
    }
}

task pmd(type: Pmd) {
    source 'src'
    exclude '**/resources/**'
    include '**/*.java'
}

// Checkstyle plugin details: http://checkstyle.sourceforge.net/
apply plugin: 'checkstyle'

checkstyle {
    config resources.text.fromFile("$rootProject.projectDir/config/static_analysis/checkstyle/checkstyle_rules.xml")
    ignoreFailures false
    configProperties = ["checkstyle.suppressions.file": "$rootProject.projectDir/config/static_analysis/checkstyle/checkstyle_suppression_filter.xml"]
    showViolations true
    toolVersion '8.0'
    tasks.withType(Checkstyle) {
        reports {
            xml.enabled true
            html.enabled true
        }
    }
}

task checkstyle(type: Checkstyle) {
    source 'src'
    include '**/*.java'
    classpath = files()
}

// Findbugs plugin details: http://findbugs.sourceforge.net/
apply plugin: 'findbugs'

findbugs {
    effort 'max'
    ignoreFailures false
    excludeFilter file("$rootProject.projectDir/config/static_analysis/findbugs/findbugs-exclusions.xml")
    reportLevel 'low'
    toolVersion '3.0.1'
    tasks.withType(FindBugs) {
        reports {
            xml.enabled false
            html.enabled true
        }
    }
}

task findbugs(type: FindBugs) {
    // Android and java modules have different directories for class files.
    File androidModuleClassesDirectory = file("$project.buildDir/intermediates/classes/debug")
    File javaModuleClassesDirectory = file("$project.buildDir/classes")
    if (javaModuleClassesDirectory.exists()) {
        classes = files(javaModuleClassesDirectory)
    } else if (androidModuleClassesDirectory.exists()) {
        classes = files(androidModuleClassesDirectory)
    }
    classpath = files()
}

// Add the static analysis tasks into the check task of gradle. Lint is already included by default.
if (project.hasProperty("check")) {
    check.doLast {
        project.tasks.getByName('checkstyle').execute()
        project.tasks.getByName('pmd').execute()
        project.tasks.getByName('findbugs').execute()
    }
}
