plugins {
    kotlin("jvm")
}

// Example plugin for the Ikar scripting engine.
//
// The same project builds in two contexts:
//   * inside the Ikar repository — the API comes from the `:script:api` module;
//   * standalone, when this folder is handed out on its own — the API comes from
//     the jars in `libs/`, and `settings.gradle.kts` supplies the Kotlin plugin
//     version and the repositories.
//
// Everything the host provides at runtime is `compileOnly`: the plugin's class
// loader has the host's class loader as its parent, so the API, kotlin-stdlib and
// coroutines are resolved there. Bundling them would load the same classes twice,
// in two different class loaders.
//
// Build: `gradlew :script:examples:myfarm:jar` (in the repository) or `gradlew jar`
// (standalone) -> build/libs/*.jar, then copy the jar into <dist>/data/scripts/.

// Плагин грузится в host, а тот идёт со своим урезанным рантаймом JDK 21
// (script/host/build.gradle.kts). Собранный на более новом JDK джар туда просто не
// загрузится — UnsupportedClassVersionError в момент старта скрипта. Внутри репозитория
// это и так дал бы корневой jvmToolchain(21), но standalone-копия корня не видит,
// поэтому версия пинуется здесь, а не наследуется.
kotlin {
    jvmToolchain(21)
}

val apiProject = findProject(":script:api")

// Keep in sync with gradle/libs.versions.toml.
val coroutinesVersion = "1.10.1"

dependencies {
    if (apiProject != null) {
        compileOnly(apiProject)
    } else {
        compileOnly(fileTree("libs") { include("*.jar") })
    }
    compileOnly("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion")
}

// Packaging tasks exist only inside the repository — they are what produces the
// standalone copy in the first place.
if (apiProject != null) {

    val exportSdk = tasks.register<Copy>("exportSdk") {
        group = "distribution"
        description = "Copies the jars a standalone build of this example compiles against into libs/."

        from(apiProject.tasks.named("jar"))
        from(project(":shared:script-protocol").tasks.named("jvmJar"))
        from(project(":shared:world-dto").tasks.named("jvmJar"))
        from(project(":shared:config-model").tasks.named("jvmJar"))
        into(layout.projectDirectory.dir("libs"))

        rename("^api-.*\\.jar$", "ikar-script-api.jar")
        rename("^script-protocol-jvm-.*\\.jar$", "ikar-script-protocol.jar")
        rename("^world-dto-jvm-.*\\.jar$", "ikar-world-dto.jar")
        rename("^config-model-jvm-.*\\.jar$", "ikar-config-model.jar")
    }

    tasks.register<Zip>("packageExample") {
        group = "distribution"
        description = "Builds the archive handed out to script authors: sources, API jars, build files and the Gradle wrapper."

        dependsOn(exportSdk)
        archiveFileName.set("ikar-script-example.zip")
        destinationDirectory.set(layout.buildDirectory.dir("distributions"))

        from(layout.projectDirectory) {
            include("src/**", "libs/**", "build.gradle.kts", "settings.gradle.kts", "README.md")
        }
        from(rootProject.layout.projectDirectory) {
            include("gradlew", "gradlew.bat", "gradle/wrapper/**")
        }
    }
}
