import org.gradle.util.GradleVersion

import static java.lang.Integer.parseInt

plugins {
    id 'java-gradle-plugin'
    id 'com.gradle.plugin-publish' version '1.2.1'
    id 'org.nosphere.gradle.github.actions' version '1.4.0'
    id 'groovy'
}

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

repositories {
    mavenCentral()
}

dependencies {
    implementation 'nu.studer:java-ordered-properties:1.0.4'
    implementation 'commons-codec:commons-codec:1.16.0'

    testImplementation 'org.spockframework:spock-core:2.3-groovy-3.0'
}

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(8)
        vendor = JvmVendorSpec.BELLSOFT
    }
}
tasks.withType(AbstractCompile).configureEach {
    options.compilerArgs <<
        "-Werror" <<
        "-Xlint:all"
}

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

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

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

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

    def incompatibleJavaVsGradleVersions = 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-credentials-plugin'
    vcsUrl = 'https://github.com/etiennestuder/gradle-credentials-plugin'
    plugins {
        pluginDevPlugin {
            id = 'nu.studer.credentials'
            displayName = 'gradle-credentials-plugin'
            description = 'Gradle plugin to store and access encrypted credentials for use in Gradle builds.'
            tags.set(['credentials'])
            implementationClass = 'nu.studer.gradle.credentials.CredentialsPlugin'
        }
    }
}

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