buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'fr.bmartel:gradle-javacard:1.5.2'
        classpath 'org.kt3k.gradle.plugin:coveralls-gradle-plugin:2.8.1'
    }
}

allprojects {
    apply plugin: 'base'
    apply plugin: 'jacoco'
    apply plugin: 'com.github.kt3k.coveralls'

    repositories {
        mavenCentral()
	maven {
           url "https://javacard.pro/maven"
        }
	//Load jcardsim-3.0.4.jar from archive.org
        maven {
           url "https://web.archive.org/web/20210502003943if_/https://dl.bintray.com/bertrandmartel/maven/"
        }
    }

    jacoco {
        toolVersion = '0.7.9'
    }
}

subprojects {
    apply plugin: 'java'

    test {
        systemProperty 'testMode', System.getProperty("testMode") ?: 'simulator'
        include '**/TestSuite.class'
        outputs.upToDateWhen { false }
        testLogging {
            //showStandardStreams = true
            exceptionFormat = 'full'
        }
    }
}

def publishedProjects = subprojects

task jacocoMerge(type: JacocoMerge) {
    publishedProjects.each { subproject ->
        executionData subproject.tasks.withType(Test)
    }
    doFirst {
        executionData = files(executionData.findAll { it.exists() })
    }
}

task jacocoRootReport(type: JacocoReport, group: 'Coverage reports') {
    description = 'Generates an aggregate report from all subprojects'
    dependsOn publishedProjects.test, jacocoMerge

    additionalSourceDirs = files(publishedProjects.sourceSets.main.allSource.srcDirs)
    sourceDirectories = files(publishedProjects.sourceSets.main.allSource.srcDirs)
    classDirectories = files(publishedProjects.sourceSets.main.output)
    executionData jacocoMerge.destinationFile

    reports {
        html.enabled = true // human readable
        xml.enabled = true // required by coveralls
    }
}

coveralls {
    sourceDirs = publishedProjects.sourceSets.main.allSource.srcDirs.flatten()
    jacocoReportPath = "${buildDir}/reports/jacoco/jacocoRootReport/jacocoRootReport.xml"
}

tasks.coveralls {
    group = 'Coverage reports'
    description = 'Uploads the aggregated coverage report to Coveralls'

    dependsOn jacocoRootReport
}