plugins {
    alias catalog.plugins.kotlin.jvm apply false
    alias catalog.plugins.sambal
    alias catalog.plugins.lombok apply false
}


import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

allprojects { subproject ->
    group = 'net.woggioni'

        if(project.currentTag.isPresent()) {
            version = project.currentTag.map { it[0] }.get()
        } else {
            version = "${getProperty('rbcs.version')}-SNAPSHOT"
        }

    repositories {
        maven {
            url = getProperty('gitea.maven.url')
            content {
                includeModule 'net.woggioni', 'jwo'
                includeGroup 'com.lys'
            }
        }
        mavenCentral()
    }

    pluginManager.withPlugin('java') {

        ext {
            jpmsModuleName = subproject.group + '.' + subproject.name.replace('-', '.')
        }

        java {
            withSourcesJar()
            modularity.inferModulePath = true
            toolchain {
                languageVersion = JavaLanguageVersion.of(25)
            }
        }

        dependencies {
            testImplementation catalog.junit.jupiter.api
            testImplementation catalog.junit.jupiter.params
            testRuntimeOnly catalog.junit.jupiter.engine
            testRuntimeOnly catalog.junit.platform.launcher
        }

        test {
            useJUnitPlatform()
        }

        tasks.withType(JavaCompile) {
            modularity.inferModulePath = true
            options.release = 25
        }

        tasks.named(JavaPlugin.COMPILE_JAVA_TASK_NAME, JavaCompile) {
            options.compilerArgumentProviders << new CommandLineArgumentProvider() {
                @Override
                Iterable<String> asArguments() {
                    return ['--patch-module', subproject.jpmsModuleName + '=' + subproject.sourceSets.main.output.asPath]
                }
            }
            options.javaModuleVersion = version
        }
    }

    pluginManager.withPlugin('jacoco') {
        test {
            finalizedBy jacocoTestReport
        }
        jacocoTestReport {
            dependsOn test
        }
    }

    pluginManager.withPlugin(catalog.plugins.kotlin.jvm.get().pluginId) {
        tasks.withType(KotlinCompile.class) {
            compilerOptions.jvmTarget = JvmTarget.JVM_25
        }
    }

    pluginManager.withPlugin(catalog.plugins.lombok.get().pluginId) {
        lombok {
            version = catalog.versions.lombok
        }
    }

    pluginManager.withPlugin('maven-publish') {

        publishing {
            repositories {
                maven {
                    name = "Gitea"
                    url = uri(getProperty('gitea.maven.url'))

                    credentials(HttpHeaderCredentials) {
                        name = "Authorization"
                        value = "token ${System.getenv()["PUBLISHER_TOKEN"]}"
                    }

                    authentication {
                        header(HttpHeaderAuthentication)
                    }
                }
            }
        }
    }
}

tasks.register('version') {
    doLast {
        println("VERSION=$version")
    }
}

