// https://nolambda.stream/posts/jacoco-setup-for-multi-module-project/
// https://www.veskoiliev.com/how-to-setup-jacoco-for-android-project-with-java-kotlin-and-multiple-flavours/

def coveredProject = subprojects

apply plugin: 'jacoco'

configure(coveredProject) { prj ->
    apply plugin: 'jacoco'

    jacoco {
        toolVersion = "0.8.7"
    }

    task jacocoReport(type: JacocoReport, dependsOn: 'testDebugUnitTest') {
        group = 'Reporting'
        description = 'Generate Jacoco coverage'

        reports {
            csv.enabled = true
            xml.enabled = false
            html.enabled = true
        }

        final fileFilter = [
                '**/R.class', '**/R$*.class', '**/BuildConfig.*', '**/Manifest*.*', 'android/**/*.*',
                '**/pokedroid/data/fragment/*.*', '**/pokedroid/data/type/*.*', '**/*Query.*', '**/*Query$*.*',
        ]
        final kotlinTree = fileTree(dir: "${prj.buildDir}/tmp/kotlin-classes/debug", excludes: fileFilter)
        final javacTree = fileTree(dir: "${prj.buildDir}/intermediates/javac/debug", excludes: fileFilter)
        final mainSrc = "${prj.projectDir}/src/main/java"

        sourceDirectories.setFrom files([mainSrc])
        classDirectories.setFrom files([kotlinTree, javacTree])
        executionData.setFrom fileTree(dir: prj.buildDir, includes: [
                'jacoco/testDebugUnitTest.exec', 'outputs/code-coverage/connected/*coverage.ec'
        ])
    }
}

/**
 *  Root task that generates an aggregated Jacoco test coverage report for all sub-projects
 */
task jacocoFullReport(type: JacocoReport, group: 'Coverage reports') {
    group = 'Reporting'
    description = 'Generates an aggregate report from all subprojects'

    tasks.withType(Test) {
        ignoreFailures true
    }

    def projects = coveredProject

    //noinspection GrUnresolvedAccess
    dependsOn(projects.jacocoReport)

    final source = files(projects.jacocoReport.sourceDirectories)

    additionalSourceDirs.setFrom source
    sourceDirectories.setFrom source

    classDirectories.setFrom files(projects.jacocoReport.classDirectories)
    executionData.setFrom files(projects.jacocoReport.executionData)

    reports {
        html {
            enabled true
            destination file('build/reports/jacoco/html')
        }
        csv {
            enabled true
            destination file('build/reports/jacoco/jacocoFullReport.csv')
        }
    }

    doFirst {
        //noinspection GroovyAssignabilityCheck
        executionData.setFrom files(executionData.findAll { it.exists() })
    }
}