plugins {
    id 'epilink.kotlin-conventions'
    id 'application'
    id 'distribution'
}

application {
    mainClass = "org.epilink.bot.MainKt"
}

dependencies {
    implementation libs.bundles.kotlin.scripting
    implementation libs.bundles.ktor.server
    implementation libs.bundles.ktor.client
    implementation libs.bundles.jackson
    implementation libs.bundles.exposed
    implementation libs.bundles.kotlinx.coroutines
    implementation libs.bundles.koin

    implementation libs.kotlin.reflect
    implementation libs.logback
    implementation libs.sqlite
    implementation libs.argparser
    implementation libs.discord4j.core
    implementation libs.lettuce.core
    implementation libs.ktorRateLimit
    implementation libs.jose4j
    implementation libs.commonsDbcp2

    if (project.hasProperty("withFrontend"))
        runtimeOnly project(path: ":epilink-frontend", configuration: "frontendJarCfg")

    testImplementation libs.koin.test
    testImplementation libs.ktor.server.testHost
    testImplementation libs.ktor.client.mock.jvm
}


license {
    // Exclude front-end test files
    exclude("frontend/*")
}

distributions {
    main {
        if (project.hasProperty("withFrontend"))
            distributionBaseName = "epilink-backend-withFrontend"
        contents {
            from "../LICENSE"
        }
    }
}

check.configure {
    dependsOn = dependsOn.findAll { it != 'detekt' }
    dependsOn += 'detektMain'
}

/* ###################################################################
 * ################# Project version file generation #################
 * ###################################################################
 */

// From https://stackoverflow.com/a/51944448, adapted for Kotlin

task generateKotlin(type: Copy) {
    def templateContext = [version: project.version]
    inputs.properties templateContext
    from 'src/template/kotlin'
    into "$buildDir/generated/kotlin"
    expand templateContext
}

sourceSets {
    main.kotlin.srcDirs += "$buildDir/generated/kotlin"
}

compileKotlin.dependsOn generateKotlin
licenseMain.dependsOn generateKotlin
/*
// Detekt creates its tasks after project evaluation (i.e. after evaluating this buildscript file) so that it can find
// all of the relevant sources, so we need to add the dependency on generateKotlin *after* the evaluation phase.
afterEvaluate {
    tasks.getByName("detektMain").dependsOn generateKotlin
}
*/
