// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    apply from: 'dependencies.gradle'
    apply from: 'dependencies_groups.gradle'

    repositories {
        google()
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }

    dependencies {
        // Release notes of Android Gradle Plugin (AGP):
        // https://developer.android.com/studio/releases/gradle-plugin
        classpath libs.gradle.gradlePlugin
        classpath libs.gradle.kotlinPlugin
        classpath libs.gradle.hiltPlugin
        classpath 'com.google.gms:google-services:4.3.10'
        classpath 'org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:3.3'
        classpath 'com.google.android.gms:oss-licenses-plugin:0.10.5'
        classpath "com.likethesalad.android:stem-plugin:2.0.0"
        classpath 'org.owasp:dependency-check-gradle:7.0.4.1'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

// ktlint Plugin
plugins {
    id "org.jlleitschuh.gradle.ktlint" version "10.2.1"
}

// https://github.com/jeremylong/DependencyCheck
apply plugin: 'org.owasp.dependencycheck'

dependencyCheck {
    // See https://jeremylong.github.io/DependencyCheck/general/suppression.html
    suppressionFiles = [
            "./tools/dependencycheck/suppressions.xml"
    ]
}

allprojects {
    apply plugin: "org.jlleitschuh.gradle.ktlint"

    repositories {
        mavenCentral {
            content {
                groups.mavenCentral.regex.each { includeGroupByRegex it }
                groups.mavenCentral.group.each { includeGroup it }
            }
        }
        maven {
            url 'https://jitpack.io'
            content {
                groups.jitpack.regex.each { includeGroupByRegex it }
                groups.jitpack.group.each { includeGroup it }
            }
        }
        // Jitsi repo
        maven {
            url "https://github.com/vector-im/jitsi_libre_maven/raw/main/android-sdk-5.0.2"
            // Note: to test Jitsi release you can use a local file like this:
            // url "file:///Users/bmarty/workspaces/jitsi_libre_maven/android-sdk-3.10.0"
            content {
                groups.jitsi.regex.each { includeGroupByRegex it }
                groups.jitsi.group.each { includeGroup it }
            }
        }
        google {
            content {
                groups.google.regex.each { includeGroupByRegex it }
                groups.google.group.each { includeGroup it }
            }
        }
        //noinspection JcenterRepositoryObsolete
        jcenter {
            content {
                groups.jcenter.regex.each { includeGroupByRegex it }
                groups.jcenter.group.each { includeGroup it }
            }
        }
    }

    tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
        // Warnings are potential errors, so stop ignoring them
        // You can override by passing `-PallWarningsAsErrors=false` in the command line
        kotlinOptions.allWarningsAsErrors = project.getProperties().getOrDefault("allWarningsAsErrors", "true").toBoolean()
    }

    // Fix "Java heap space" issue
    tasks.withType(org.jlleitschuh.gradle.ktlint.tasks.BaseKtLintCheckTask).configureEach {
        it.workerMaxHeapSize.set("2G")
    }

    // See https://github.com/JLLeitschuh/ktlint-gradle#configuration
    ktlint {
        // See https://github.com/pinterest/ktlint/releases/
        version = "0.45.1"
        android = true
        ignoreFailures = false
        enableExperimentalRules = true
        // display the corresponding rule
        verbose = true
        disabledRules = [
                "spacing-between-declarations-with-comments",
                "no-multi-spaces",
                "experimental:spacing-between-declarations-with-annotations",
                "experimental:annotation",
                // - Missing newline after "("
                // - Missing newline before ")"
                "wrapping",
                // - Unnecessary trailing comma before ")"
                "experimental:trailing-comma",
                // - A block comment in between other elements on the same line is disallowed
                "experimental:comment-wrapping",
                // - A KDoc comment after any other element on the same line must be separated by a new line
                "experimental:kdoc-wrapping",
        ]
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

def launchTask = getGradle()
        .getStartParameter()
        .getTaskRequests()
        .toString()
        .toLowerCase()

if (launchTask.contains("codeCoverageReport".toLowerCase())) {
    apply from: 'coverage.gradle'
}

apply plugin: 'org.sonarqube'

// To run a sonar analysis:
// Run './gradlew sonarqube -Dsonar.login=<REPLACE_WITH_SONAR_KEY>'
// The SONAR_KEY is stored in passbolt as Token Sonar Cloud Bma

sonarqube {
    properties {
        property "sonar.projectName", "Element-Android"
        property "sonar.projectKey", "im.vector.app.android"
        property "sonar.host.url", "https://sonarcloud.io"
        property "sonar.projectVersion", project(":vector").android.defaultConfig.versionName
        property "sonar.sourceEncoding", "UTF-8"
        property "sonar.links.homepage", "https://github.com/vector-im/element-android/"
        property "sonar.links.ci", "https://github.com/vector-im/element-android/actions"
        property "sonar.links.scm", "https://github.com/vector-im/element-android/"
        property "sonar.links.issue", "https://github.com/vector-im/element-android/issues"
        property "sonar.organization", "new_vector_ltd_organization"
        property "sonar.java.coveragePlugin", "jacoco"
        property "sonar.coverage.jacoco.xmlReportPaths", "${project.buildDir}/reports/jacoco/allCodeCoverageReport/allCodeCoverageReport.xml"
        property "sonar.login", project.hasProperty("SONAR_LOGIN") ? SONAR_LOGIN : "invalid"
    }
}

project(":vector") {
    sonarqube {
        properties {
            property "sonar.sources", project(":vector").android.sourceSets.main.java.srcDirs
            // exclude source code from analyses separated by a colon (:)
            // Exclude Java source
            property "sonar.exclusions", "**/BugReporterMultipartBody.java"
        }
    }
}

project(":library:diff-match-patch") {
    sonarqube {
        skipProject = true
    }
}

//project(":matrix-sdk-android") {
//    sonarqube {
//        properties {
//            property "sonar.sources", project(":matrix-sdk-android").android.sourceSets.main.java.srcDirs
//            // exclude source code from analyses separated by a colon (:)
//            // property "sonar.exclusions", "**/*.*"
//        }
//    }
//}