plugins {
    id 'io.freefair.git-version'
    id 'io.freefair.maven-central.validate-poms' apply false
    id 'io.freefair.lombok' apply false
    id "com.gradle.plugin-publish" version "2.0.0" apply false
    id 'org.jetbrains.kotlin.jvm' apply false
    id 'base'
    id 'org.jreleaser' version '1.20.0'
}

allprojects {
    repositories {
        mavenCentral()
        gradlePluginPortal()
    }

    group = "io.freefair.gradle"
    version = rootProject.version

    plugins.withId("java") {

        java {
            withSourcesJar()
            withJavadocJar()
            toolchain {
                languageVersion = JavaLanguageVersion.of(17)
            }
        }

        plugins.apply "jacoco"
        plugins.apply "io.freefair.lombok"

        jacocoTestReport.reports.xml.required = true

        tasks.withType(Test) {
            useJUnitPlatform()
            jvmArgs('--add-opens=java.base/java.lang=ALL-UNNAMED')
        }

        dependencies {
            testImplementation platform("org.junit:junit-bom:6.0.0")

            testImplementation 'org.assertj:assertj-core:3.27.6'
            testImplementation 'org.junit.jupiter:junit-jupiter-api'
            testImplementation 'org.junit.jupiter:junit-jupiter-params'
            testImplementation 'org.mockito:mockito-core:5.20.0'
            testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
            testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
        }

        tasks.named('jar') {
            manifest {
                attributes 'Implementation-Version': "$project.version"
            }
        }
    }

    tasks.withType(Javadoc).configureEach {
        options.links "https://docs.gradle.org/$gradle.gradleVersion/javadoc/"
        options.addBooleanOption("Xdoclint:-missing", true)
    }

    plugins.withId("com.gradle.plugin-publish") {
        gradlePlugin {
            website = "https://docs.freefair.io/gradle-plugins/$version/reference/"
            vcsUrl = 'https://github.com/freefair/gradle-plugins'
        }
    }

    plugins.withId("maven-publish") {
        project.apply plugin: 'signing'
        project.apply plugin: 'io.freefair.maven-central.validate-poms'

        signing {
            required = { !version.endsWith('SNAPSHOT') && gradle.taskGraph.hasTask("publish") }

            def signingKey = findProperty("signingKey")
            def signingPassword = findProperty("signingPassword")
            useInMemoryPgpKeys(signingKey, signingPassword)
        }

        publishing {
            publications.withType(MavenPublication) {
                pom {
                    url = 'https://github.com/freefair/gradle-plugins'
                    name = provider { project.description }
                    description = provider { project.description }
                    inceptionYear = '2016'
                    licenses {
                        license {
                            name = 'MIT'
                            url = 'https://github.com/freefair/gradle-plugins/blob/master/LICENSE'
                        }
                    }
                    organization {
                        name = 'FreeFair'
                        url = 'https://github.com/freefair'
                    }
                    developers {
                        developer {
                            id = 'larsgrefer'
                            name = 'Lars Grefer'
                            email = 'github@larsgrefer.de'
                            timezone = 'Europe/Berlin'
                        }
                        developer {
                            id = 'frisch12'
                            name = 'Dennis Fricke'
                            email = 'dennis.fricke@freefair.io'
                            timezone = 'Europe/Berlin'
                        }
                    }
                    ciManagement {
                        system = 'GitHub Actions'
                        url = 'https://github.com/freefair/gradle-plugins/actions'
                    }
                    issueManagement {
                        system = 'GitHub Issues'
                        url = 'https://github.com/freefair/gradle-plugins/issues'
                    }
                    scm {
                        connection = 'scm:git:https://github.com/freefair/gradle-plugins.git'
                        developerConnection = 'scm:git:git@github.com:freefair/gradle-plugins.git'
                        url = 'https://github.com/freefair/gradle-plugins/'
                    }
                }
            }

            repositories {
                maven {
                    name = "mavenCentralStaging"
                    url = rootProject.layout.buildDirectory.dir('staging-deploy')
                }
            }
        }
    }
}

tasks.named('wrapper') {
    distributionType = Wrapper.DistributionType.ALL
}

jreleaser {
    project {
        license = 'MIT'
        inceptionYear = '2016'
    }
    signing {
        active = 'NEVER'
    }
    deploy {
        maven {
            mavenCentral {
                sonatype {
                    active = 'RELEASE'
                    namespace = "io.freefair"
                    url = 'https://central.sonatype.com/api/v1/publisher'
                    sign = false //Already done by Gradle
                    checksums = false //Already done by Gradle
                    stagingRepository('build/staging-deploy')
                }
            }
        }
    }
}
