plugins {
    alias(libs.plugins.kotlin.jvm)
    alias(libs.plugins.kotlin.serialization)
    alias(libs.plugins.kotlin.power.assert)
    alias(libs.plugins.shadow)
}

kotlin {
    jvmToolchain(8)

    compilerOptions {
        jvmTarget.set(org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_1_8)
    }
}

java {
    sourceCompatibility = JavaVersion.VERSION_1_8
    targetCompatibility = JavaVersion.VERSION_1_8
}

dependencies {
    implementation(project(":repl-shared"))
    implementation(libs.kotlin.scripting.jvm)
    implementation(libs.kotlin.scripting.jvm.host)
    implementation(libs.kotlin.scripting.compiler.embeddable)
    implementation(libs.kotlin.compiler.embeddable)
    implementation(libs.kotlinx.serialization.json)
    implementation(libs.kotlinx.coroutines.core)
    implementation(kotlin("reflect"))

    testImplementation(libs.kotlin.test)
    testImplementation(libs.kotlinx.coroutines.test)
}

val testKotlinStdlib2 by configurations.creating

dependencies {
    // renovate: ignore-next-line
    testKotlinStdlib2(libs.kotlin.stdlib2)
}

tasks.test {
    useJUnitPlatform()
    val stdlibJar = configurations.runtimeClasspath.get().find { it.name.startsWith("kotlin-stdlib-") && !it.name.contains("jdk") }
    if (stdlibJar != null) {
        systemProperty("kotlin.stdlib.path", stdlibJar.absolutePath)
    }
    systemProperty("kotlin.stdlib.kotlin2.path", testKotlinStdlib2.joinToString(File.pathSeparator) { it.absolutePath })
    systemProperty("GRADLE_MCP_LOG_DIR", layout.buildDirectory.dir("test-logs").get().asFile.absolutePath)
}

tasks.named<com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar>("shadowJar") {
    archiveClassifier.set("all")
    manifest {
        attributes["Main-Class"] = "dev.rnett.gradle.mcp.repl.ReplWorker"
    }
}
