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

apply plugin: 'kotlin'
apply plugin: 'java-gradle-plugin'

// Gradle plugin must be compiled targeting the same Kotlin version as used by Gradle
kotlin.sourceSets.configureEach {
    languageSettings {
        languageVersion = KotlinConfiguration.getOverridingKotlinLanguageVersion(project) ?: "1.4"
        apiVersion = KotlinConfiguration.getOverridingKotlinApiVersion(project) ?: "1.4"
    }
}

dependencies {
    implementation(project(":atomicfu-transformer")) {
        exclude group: 'org.jetbrains.kotlin', module: 'kotlin-stdlib'
    }

    compileOnly gradleApi()
    compileOnly 'org.jetbrains.kotlin:kotlin-stdlib'
    compileOnly "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    // Atomicfu compiler plugin dependency will be loaded to kotlinCompilerPluginClasspath
    // Atomicfu plugin will only be applied if the flag is set kotlinx.atomicfu.enableJsIrTransformation=true
    implementation "org.jetbrains.kotlin:atomicfu:$kotlin_version"

    testImplementation gradleTestKit()
    testImplementation 'org.jetbrains.kotlin:kotlin-test'
    testImplementation 'org.jetbrains.kotlin:kotlin-test-junit'
    testImplementation 'junit:junit:4.12'
}

configurations {
    testPluginClasspath {
        attributes {
            attribute(
                    Usage.USAGE_ATTRIBUTE,
                    project.objects.named(Usage.class, Usage.JAVA_RUNTIME)
            )
            attribute(
                    Category.CATEGORY_ATTRIBUTE,
                    project.objects.named(Category.class, Category.LIBRARY)
            )
        }
    }
}

dependencies {
    testPluginClasspath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}

evaluationDependsOn(':atomicfu')
def atomicfu = project(':atomicfu')
def atomicfuJvmJarTask = atomicfu.tasks.getByName(atomicfu.kotlin.targets.jvm.artifactsTaskName)

def jsLegacy = atomicfu.kotlin.targets.hasProperty("jsLegacy")
        ? atomicfu.kotlin.targets.jsLegacy
        : atomicfu.kotlin.targets.js
def atomicfuJsJarTask = atomicfu.tasks.getByName(jsLegacy.artifactsTaskName)

def atomicfuMetadataOutput = atomicfu.kotlin.targets.metadata.compilations["main"].output.allOutputs

// Write the plugin's classpath to a file to share with the tests
task createClasspathManifest {
    dependsOn(atomicfuJvmJarTask)
    dependsOn(atomicfuJsJarTask)
    dependsOn(atomicfuMetadataOutput)

    def outputDir = file("$buildDir/$name")
    outputs.dir outputDir

    doLast {
        outputDir.mkdirs()
        file("$outputDir/plugin-classpath.txt").text = (sourceSets.main.runtimeClasspath + configurations.testPluginClasspath).join("\n")
        file("$outputDir/atomicfu-jvm.txt").text = atomicfuJvmJarTask.archivePath
        file("$outputDir/atomicfu-js.txt").text = atomicfuJsJarTask.archivePath
        file("$outputDir/atomicfu-metadata.txt").text = atomicfuMetadataOutput.join("\n")
    }
}

task createKotlinRepoUrlResource {
    def customKotlinRepoUrl = KotlinConfiguration.getCustomKotlinRepositoryURL(project)
    if (customKotlinRepoUrl == null) return

    def outputDir = file("$buildDir/$name")
    outputs.dir outputDir

    doLast {
        outputDir.mkdirs()
        file("$outputDir/kotlin-repo-url.txt").text = customKotlinRepoUrl
    }
}

// Add the classpath file to the test runtime classpath
dependencies {
    testRuntimeOnly files(createClasspathManifest)
    testRuntimeOnly files(createKotlinRepoUrlResource)
}
