/*
 * Copyright 2018 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

buildscript {
    def deployNative = property('native.deploy') == 'true'

    if (project.hasProperty("bootstrap")) {
        ext.compilerVersion = property('kotlin.version.snapshot')
        ext.librariesVersion = property('kotlin.version.snapshot')
        ext.serializationPluginVersion = property('plugin.version.snapshot')
        ext.konanVersion = property('konan.version.snapshot')
        ext["org.jetbrains.kotlin.native.home"] = System.getenv("KONAN_LOCAL_DIST")
    } else {
        ext.compilerVersion = property('kotlin.version')
        ext.librariesVersion = property('kotlin.version')
        ext.serializationPluginVersion = property('plugin.version')
        ext.konanVersion = property('konan.version')
    }
    ext.serializationVersion = property('library.version')
    ext.serializationCoordinates = "org.jetbrains.kotlin:kotlin-serialization"
    ext.tcChannel = 'https://teamcity.jetbrains.com/guestAuth/app/rest/builds/id:2082584/artifacts/content/maven'
    ext.devChannel = 'https://kotlin.bintray.com/kotlin-dev'
    ext.serializationRepo = 'https://kotlin.bintray.com/kotlinx'
    ext.experimentalsEnabled = ["-progressive", "-Xuse-experimental=kotlin.Experimental",
                                "-Xuse-experimental=kotlin.ExperimentalMultiplatform",
                                ]

    ext.experimentalsInTestEnabled = ["-progressive", "-Xuse-experimental=kotlin.Experimental",
                                "-Xuse-experimental=kotlin.ExperimentalMultiplatform",
                                "-Xuse-experimental=kotlinx.serialization.ImplicitReflectionSerializer",
                                ]

    repositories {
        mavenLocal()
        maven { url tcChannel }
        maven { url devChannel }
        maven { url serializationRepo }
        maven { url "https://plugins.gradle.org/m2/" }
        maven { url "https://dl.bintray.com/jetbrains/kotlin-native-dependencies" }
        jcenter()
    }

    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$compilerVersion"
        classpath "$serializationCoordinates:$serializationPluginVersion"
        classpath "org.jetbrains.kotlin:kotlin-native-gradle-plugin:$konanVersion"

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

        // Custom bintray plugin to publish with Gradle Metadata
        classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.4-jetbrains-3"

        classpath "com.github.jengelman.gradle.plugins:shadow:2.0.2"
        classpath "me.champeau.gradle:jmh-gradle-plugin:0.4.5"
        classpath "net.ltgt.gradle:gradle-apt-plugin:0.10"
    }
}

ext {
    libraries = [
            kotlin_stdlib: "org.jetbrains.kotlin:kotlin-stdlib:$librariesVersion",
            kotlin_stdlib_common: "org.jetbrains.kotlin:kotlin-stdlib-common:$librariesVersion",
            kotlin_stdlib_js: "org.jetbrains.kotlin:kotlin-stdlib-js:$librariesVersion",
            kotlin_test: "org.jetbrains.kotlin:kotlin-test:$librariesVersion",
            kotlin_test_junit: "org.jetbrains.kotlin:kotlin-test-junit:$librariesVersion",
            kotlin_test_common: "org.jetbrains.kotlin:kotlin-test-common:$librariesVersion",
            kotlin_test_annotations_common: "org.jetbrains.kotlin:kotlin-test-annotations-common:$librariesVersion",
            kotlin_test_js: "org.jetbrains.kotlin:kotlin-test-js:$librariesVersion",
            kotlin_reflect: "org.jetbrains.kotlin:kotlin-reflect:$librariesVersion",
    ]
}

allprojects {
    group 'org.jetbrains.kotlinx'
    version serializationVersion

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

    repositories {
        mavenLocal()
        maven { url tcChannel }
        maven { url devChannel }
        maven { url serializationRepo }
        jcenter()
    }
}

apply plugin: 'kotlin'

subprojects {
    configurations.all {
        resolutionStrategy.eachDependency { DependencyResolveDetails details ->
            if (details.requested.group == 'org.jetbrains.kotlin' && details.requested.name == 'kotlinx-serialization-unshaded') {
                details.useVersion serializationPluginVersion
                details.because 'use not bundled one'
            }
        }
    }

    if (project.name.contains("native")) return

    apply plugin: 'kotlinx-serialization'

    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
        }
    }

    if (project.name.contains("benchmark")) return

    apply plugin: 'maven-publish'

    def varintName = ""

    if (project.name == "jvm") {
        varintName = "${rootProject.name}"
    } else {
        varintName = "${rootProject.name}-${project.name}"
    }

    afterEvaluate {
        task sourceJar(type: Jar, dependsOn: classes) {
            classifier 'sources'
            from sourceSets.main.kotlin
            if (project.name == "jvm" || project.name == "js") {
                duplicatesStrategy = "exclude"
                def platformSrc = sourceSets.main.kotlin
                def commonSrc = project(':common').sourceSets.main.kotlin
                from(platformSrc + commonSrc)
            }
        }
    }

    tasks.withType(Jar) {
        archivesBaseName = varintName
    }

    apply from: "${rootProject.rootDir}/gradle/mavenMetadata.gradle"

    publishing {
        publications {
            mavenProject(MavenPublication) {
                from components.java
                groupId project.group
                artifactId varintName
                version project.version

                artifact sourceJar {
                    classifier "sources"
                }
                pom.withXml {
                    def root = it.asNode()
                    // NOTE: Don't try to move top-level things (especially "description") to the pomConfig block
                    //       because they would resolve incorrectly to top-level project properties in Gradle/Groovy
                    root.appendNode('name', varintName)
                    root.appendNode('description', 'Kotlin multiplatform serialization runtime library')
                    root.appendNode('url', 'https://github.com/Kotlin/kotlinx.serialization')
                    root.children().last() + pomConfig
                }
            }
        }
    }

    apply plugin: 'com.jfrog.bintray'

    apply from: "${rootProject.rootDir}/gradle/bintray.gradle"

    // Disable metadata for non-native modules
    afterEvaluate {
        publishing.publications.each { pub ->
            pub.moduleDescriptorGenerator = null
        }
    }
}
