import org.gradle.util.GradleVersion

import static java.lang.Integer.parseInt

plugins {
    id 'java-gradle-plugin'
    alias(libs.plugins.publish)
    alias(libs.plugins.github.actions)
    id 'groovy'
}

group = 'nu.studer'
version = '10.1.2-DEV'

configurations.all { Configuration c ->
    c.resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        if (details.requested.group == 'jakarta.xml.bind' && details.requested.name == 'jakarta.xml.bind-api') {
            details.useVersion '3.0.1'
        }
    }
}

repositories {
    mavenCentral()
}

dependencies {
    api libs.bundles.jooq

    runtimeOnly libs.bundles.jaxb

    testImplementation libs.h2
    testImplementation libs.spock
    testRuntimeOnly libs.junit5.launcher
}

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(21)
        vendor = JvmVendorSpec.BELLSOFT
    }
}

tasks.withType(AbstractCompile).configureEach {
    options.compilerArgs <<
        "-Werror" <<
        "-Xlint:all"
}

def dvBuildScan = develocity.buildScan
tasks.withType(Test).configureEach {
    maxParallelForks = 1 // there is currently only a single test class
    useJUnitPlatform()

    String testJavaRuntimeVersion = findProperty('testJavaRuntimeVersion') ?: '21'
    String testGradleVersion = findProperty('testGradleVersion') ?: GradleVersion.current().version

    javaLauncher.set(javaToolchains.launcherFor { spec ->
        spec.languageVersion.set(JavaLanguageVersion.of(testJavaRuntimeVersion))
        dvBuildScan.value(identityPath.path + "#jvmVersion", testJavaRuntimeVersion)
    } as Provider<? extends JavaLauncher>)

    systemProperty 'testContext.gradleVersion', testGradleVersion
    dvBuildScan.value(identityPath.path + "#gradleVersion", testGradleVersion)

    def incompatibleJavaVsGradleVersions = parseInt(testJavaRuntimeVersion) > 20 && GradleVersion.version(testGradleVersion) < GradleVersion.version('8.6') ||
            parseInt(testJavaRuntimeVersion) > 16 && GradleVersion.version(testGradleVersion) < GradleVersion.version('7.3') ||
            parseInt(testJavaRuntimeVersion) > 15 && GradleVersion.version(testGradleVersion) < GradleVersion.version('7.0')
    if (incompatibleJavaVsGradleVersions) {
        enabled = false
    }
}

tasks.withType(Javadoc).configureEach {
    options.addStringOption('Xdoclint:none', '-quiet')
}

gradlePlugin {
    website = 'https://github.com/etiennestuder/gradle-jooq-plugin'
    vcsUrl = 'https://github.com/etiennestuder/gradle-jooq-plugin'
    plugins {
        pluginDevPlugin {
            id = 'nu.studer.jooq'
            displayName = 'gradle-jooq-plugin'
            description = 'Gradle plugin that integrates jOOQ.'
            tags.set(['jooq'])
            implementationClass = 'nu.studer.gradle.jooq.JooqPlugin'
        }
    }
}

tasks.withType(ValidatePlugins.class).configureEach {
    failOnWarning = true
    enableStricterValidation = true
}
