plugins {
    kotlin("jvm") version "2.0.0"
    application
    id("com.google.protobuf") version "0.9.4"
}

group = "com.tokilabs"
version = "1.0.5"

kotlin {
    jvmToolchain(17)
}

sourceSets {
    create("crosstest") {
        kotlin.srcDir("crosstest")
        compileClasspath += sourceSets["main"].output + configurations["runtimeClasspath"]
        runtimeClasspath += output + compileClasspath
    }
}

kotlin {
    target {
        val main = compilations.getByName("main")
        compilations.getByName("crosstest") {
            associateWith(main)
        }
    }
}

dependencies {
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.1")
    implementation("com.google.protobuf:protobuf-kotlin:4.27.0")
    implementation("com.squareup.okhttp3:okhttp:4.12.0")
    implementation("org.java-websocket:Java-WebSocket:1.5.6")

    testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.8.1")
    testImplementation(kotlin("test"))
}

protobuf {
    protoc {
        artifact = "com.google.protobuf:protoc:4.27.0"
    }
    generateProtoTasks {
        all().forEach { task ->
            task.builtins {
                create("kotlin")
            }
        }
    }
}

application {
    mainClass.set(
        (findProperty("mainClass") as String?)
            ?: "com.tokilabs.proto_socket.crosstest.MainKt",
    )
}

val crosstestSourceSet = sourceSets["crosstest"]

tasks.named<JavaExec>("run") {
    classpath = crosstestSourceSet.runtimeClasspath
    mainClass.set(
        (findProperty("mainClass") as String?)
            ?: "com.tokilabs.proto_socket.crosstest.MainKt",
    )
}

tasks.test {
    useJUnitPlatform()
    systemProperty("java.security.egd", "file:/dev/./urandom")
}
