buildscript {
    ext.kotlin_version = '1.4.31'

    repositories {
        mavenCentral()
        maven { url 'https://jitpack.io' }
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.github.form-com.diff-coverage-gradle:diff-coverage:0.8.0'
        classpath "io.gitlab.arturbosch.detekt:detekt-gradle-plugin:1.20.0"
    }
}

repositories {
    mavenCentral()
}

apply plugin: 'jacoco'
apply plugin: 'com.form.diff-coverage'

ext {
    kotest_version = '4.6.1'
}

subprojects {
    apply plugin: 'kotlin'
    apply plugin: 'maven-publish'
    apply plugin: 'jacoco'
    apply plugin: "io.gitlab.arturbosch.detekt"

    repositories {
        mavenCentral()
    }

    dependencies {
        implementation 'org.apache.logging.log4j:log4j-slf4j-impl:2.18.0'

        testImplementation "io.kotest:kotest-runner-junit5-jvm:$kotest_version"
        testImplementation "io.kotest:kotest-assertions-core-jvm:$kotest_version"
        testImplementation "io.kotest:kotest-property-jvm:$kotest_version"
        testImplementation 'io.mockk:mockk:1.12.0'
    }

    test {
        useJUnitPlatform()
    }

    compileKotlin {
        kotlinOptions.jvmTarget = '1.8'
        kotlinOptions.apiVersion = '1.3'
    }
    compileTestKotlin {
        kotlinOptions.jvmTarget = '1.8'
    }
    sourceCompatibility = 1.8

    task sourcesJar(type: Jar, dependsOn: classes) {
        classifier 'sources'
        from sourceSets.main.allSource
    }

    task javadocJar(type: Jar, dependsOn: javadoc) {
        classifier 'javadoc'
        from javadoc.destinationDir
    }

    artifacts {
        archives sourcesJar
        archives javadocJar
    }

    jacoco {
        toolVersion = "0.8.8"
    }

    jacocoTestReport {
        reports {
            html.required = true
        }
        executionData.from fileTree(buildDir).include("/jacoco/*.exec")
    }
}

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

    additionalSourceDirs.from = files(subprojects.sourceSets.main.allSource.srcDirs)
    sourceDirectories.from = files(subprojects.sourceSets.main.allSource.srcDirs)
    classDirectories.from = files(subprojects.sourceSets.main.output)
    executionData.from = files(subprojects.jacocoTestReport.executionData)

    reports {
        xml.required = true
        html.required = true
    }
}

diffCoverageReport {
    def diffBase = project.hasProperty('diffBase') ? project.diffBase : 'HEAD'

    diffSource.git.compareWith diffBase
    jacocoExecFiles = files(subprojects.jacocoTestReport.executionData)
    classesDirs = files(subprojects.jacocoTestReport.classDirectories)
    srcDirs = files(subprojects.jacocoTestReport.sourceDirectories)

    reportConfiguration.html = true
    reportConfiguration.fullCoverageReport = true
    violationRules.failIfCoverageLessThan 0.9
}

task clean {
    doLast {
        delete project.buildDir
    }
}
