/*
 * Copyright 2017-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

buildscript {
    if (project.hasProperty("bootstrap")) {
        ext.kotlin_version = property('kotlin.version.snapshot')
        ext["kotlin.native.home"] = System.getenv("KONAN_LOCAL_DIST")
    } else {
        ext.kotlin_version = property('kotlin.version')
    }
    if (project.hasProperty("library.version")) {
        ext.overriden_version = property('library.version')
    }
    ext.experimentalsEnabled = ["-progressive", "-opt-in=kotlin.Experimental",
                                "-opt-in=kotlin.ExperimentalMultiplatform",
                                "-opt-in=kotlinx.serialization.InternalSerializationApi"
    ]

    ext.experimentalsInTestEnabled = ["-progressive", "-opt-in=kotlin.Experimental",
                                      "-opt-in=kotlin.ExperimentalMultiplatform",
                                      "-opt-in=kotlinx.serialization.ExperimentalSerializationApi",
                                      "-opt-in=kotlinx.serialization.InternalSerializationApi",
                                      "-opt-in=kotlin.ExperimentalUnsignedTypes"
    ]
    ext.koverEnabled = property('kover.enabled') ?: true

    /*
    * This property group is used to build kotlinx.serialization against Kotlin compiler snapshot.
    * When build_snapshot_train is set to true, kotlin_version property is overridden with kotlin_snapshot_version.
    * DO NOT change the name of these properties without adapting kotlinx.train build chain.
    */
    def prop = rootProject.properties['build_snapshot_train']
    ext.build_snapshot_train = prop != null && prop != ""
    if (build_snapshot_train) {
        ext.kotlin_version = rootProject.properties['kotlin_snapshot_version']
        if (kotlin_version == null) {
            throw new IllegalArgumentException("'kotlin_snapshot_version' should be defined when building with snapshot compiler")
        }
        repositories {
            maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
        }
    }

    repositories {
        maven { url 'https://maven.pkg.jetbrains.space/kotlin/p/dokka/dev' }
        // kotlin-dev with space redirector
        maven { url "https://cache-redirector.jetbrains.com/maven.pkg.jetbrains.space/kotlin/p/kotlin/dev" }
        mavenCentral()
        gradlePluginPortal()
        // For Dokka that depends on kotlinx-html
        maven { url "https://maven.pkg.jetbrains.space/public/p/kotlinx-html/maven" }
        mavenLocal()
    }

    configurations.classpath {
        resolutionStrategy.eachDependency { DependencyResolveDetails details ->
            if (details.requested.group == 'org.jetbrains.kotlin') {
                details.useVersion kotlin_version
            }
        }
    }

    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"
        classpath "org.jetbrains.dokka:dokka-gradle-plugin:$dokka_version"
        classpath "org.jetbrains.kotlinx:kover:$kover_version"
        classpath "org.jetbrains.kotlinx:binary-compatibility-validator:$validator_version"
        classpath "org.jetbrains.kotlinx:kotlinx-knit:$knit_version"
        classpath 'ru.vyarus:gradle-animalsniffer-plugin:1.5.3' // Android API check

        classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.18'

        // Various benchmarking stuff
        classpath "com.github.jengelman.gradle.plugins:shadow:4.0.2"
        classpath "me.champeau.gradle:jmh-gradle-plugin:0.5.3"
        classpath "net.ltgt.gradle:gradle-apt-plugin:0.21"
    }
}

// To make it visible for compiler-version.gradle
ext.compilerVersion = org.jetbrains.kotlin.config.KotlinCompilerVersion.VERSION
ext.nativeDebugBuild = org.jetbrains.kotlin.gradle.plugin.mpp.NativeBuildType.DEBUG

apply plugin: 'binary-compatibility-validator'
apply plugin: 'base'
apply plugin: 'kotlinx-knit'

apiValidation {
    ignoredProjects += ["benchmark", "guide", "kotlinx-serialization"]
}

knit {
    siteRoot = "https://kotlin.github.io/kotlinx.serialization"
    moduleDocs = "build/dokka/htmlMultiModule"
}

// Build API docs for all modules with dokka before running Knit
knitPrepare.dependsOn "dokka"

apply plugin: 'org.jetbrains.dokka'
dependencies {
    dokkaPlugin("org.jetbrains.kotlinx:dokka-pathsaver-plugin:$knit_version")
}

allprojects {
    group 'org.jetbrains.kotlinx'

    def deployVersion = properties['DeployVersion']
    if (deployVersion != null) version = deployVersion

    if (project.hasProperty("bootstrap")) {
        version = version + '-SNAPSHOT'
    }

    // the only place where HostManager could be instantiated
    project.ext.hostManager = new org.jetbrains.kotlin.konan.target.HostManager()

    if (build_snapshot_train) {
        // Snapshot-specific
        repositories {
            mavenLocal()
            maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
        }
    }

    configurations.all {
        resolutionStrategy.eachDependency { DependencyResolveDetails details ->
            if (details.requested.group == 'org.jetbrains.kotlin') {
                details.useVersion kotlin_version
            }
        }
    }

    repositories {
        mavenCentral()
        maven { url 'https://maven.pkg.jetbrains.space/kotlin/p/dokka/dev' }
        // kotlin-dev with space redirector
        maven { url "https://cache-redirector.jetbrains.com/maven.pkg.jetbrains.space/kotlin/p/kotlin/dev" }
        // For Dokka that depends on kotlinx-html
        maven { url "https://maven.pkg.jetbrains.space/public/p/kotlinx-html/maven" }
        // For local development
        mavenLocal()

    }
}

subprojects {
    tasks.withType(org.jetbrains.kotlin.gradle.tasks.AbstractKotlinCompile).all { task ->
        if (task.name.contains("Test") || task.name.contains("Jmh")) {
            task.kotlinOptions.freeCompilerArgs += experimentalsInTestEnabled
        } else {
            task.kotlinOptions.freeCompilerArgs += experimentalsEnabled
        }
    }

    apply from: rootProject.file('gradle/teamcity.gradle')
    // Configure publishing for some artifacts
    if (project.name != "benchmark" && project.name != "guide") {
        apply from: rootProject.file('gradle/publishing.gradle')
    }

}

subprojects {
    // Can't be applied to BOM
    if (project.name == "kotlinx-serialization-bom" || project.name == "benchmark" || project.name == "guide") return

    // Animalsniffer setup
    apply plugin: 'ru.vyarus.animalsniffer'

    afterEvaluate { // Can be applied only when the project is evaluated
        animalsniffer {
            sourceSets = [sourceSets.main]
        }
        dependencies {
            signature 'net.sf.androidscents.signature:android-api-level-14:4.0_r4@signature'
            signature 'org.codehaus.mojo.signature:java18:1.0@signature'
        }
    }

    // Kover setup
    apply from: rootProject.file("gradle/kover.gradle")
}

apply from: rootProject.file('gradle/compiler-version.gradle')
apply from: rootProject.file("gradle/dokka.gradle")
apply from: rootProject.file("gradle/benchmark-parsing.gradle")
