import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

buildscript {
    ext.kotlin_version = '1.9.21'
    ext.kotlin_coroutines_version = '1.8.1'
    ext.slf4j_version = '1.7.36'
    ext.junit_version = '4.13.2'
    repositories {
        google()
        mavenCentral()
        flatDir { dirs 'app/libs' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:8.5.2'
        classpath 'org.owasp:dependency-check-gradle:10.0.4'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

        // Huawei agconnect plugin
        classpath 'com.huawei.agconnect:agcp-1.9.1.303'
        classpath 'com.huawei.agconnect:agconnect-crash-symbol-lib-1.9.1.301'
        classpath 'com.huawei.agconnect:agconnect-apms-plugin-1.6.2.300'
        classpath 'com.huawei.agconnect:agconnect-core-1.9.1.301@aar'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

plugins {
    id 'jacoco'
    id 'org.sonarqube' version '4.4.1.3373'
}

allprojects {
    repositories {
        google()
        mavenCentral()
        maven { url 'https://jitpack.io' }
        flatDir { dirs 'libs' }

        // Huawei
        exclusiveContent {
            forRepository {
                maven { url 'https://developer.huawei.com/repo/' }
            }
            filter {
                // Only matching dependencies will be installed from this repository.
                includeGroup "com.huawei.hms"
                includeGroup "com.huawei.android.hms"
                includeGroup "com.huawei.hmf"
            }
        }
    }

    // OWASP dependency-check-gradle plugin
    apply plugin: 'org.owasp.dependencycheck'
    dependencyCheck {
        skipConfigurations += 'lintClassPath'
        suppressionFile 'dependencyCheckSuppressions.xml'

        // Fail dependency check if any dependency has a CVE with a score of 3+
        failBuildOnCVSS 3

        // NVD API config. The key is not very secret, but please don't mis-use it.
        nvd {
            apiKey = "fc7aab31-d936-4807-b43b-e061c84a12dd"
            validForHours = 6
        }
    }

    group = 'ch.threema'
}

wrapper {
    distributionType = Wrapper.DistributionType.ALL
}


sonarqube {
    properties {
        property 'sonar.projectKey', 'android-client'
        property 'sonar.projectName', 'Threema for Android'
    }
}

jacoco {
    toolVersion = '0.8.7'
}

subprojects {
    tasks.withType(KotlinCompile).tap {
        configureEach {
            kotlinOptions.jvmTarget = JavaVersion.VERSION_11.toString()
        }
    }
}

subprojects {
    configurations.configureEach {
        resolutionStrategy {
            eachDependency { details ->
                if ('org.jacoco' == details.requested.group) {
                    details.useVersion "0.8.7"
                }
            }
        }
    }
}


// task to gather code coverage from multiple subprojects
// NOTE: the `JacocoReport` tasks do *not* depend on the `test` task by default. Meaning you have to ensure
// that `test` (or other tasks generating code coverage) run before generating the report.
tasks.register("codeCoverageReport", JacocoReport) {
    // If a subproject applies the 'jacoco' plugin, add the result it to the report
    subprojects { subproject ->
        subproject.plugins.withType(JacocoPlugin).configureEach {
            subproject.tasks.matching({ t -> t.extensions.findByType(JacocoTaskExtension) }).configureEach { testTask ->
                sourceSets subproject.sourceSets.main
                executionData(testTask)
            }
        }
    }
    reports {
        xml.required = true
    }
}
