import java.time.Duration

plugins {
    id 'java'
    id 'signing'
    id 'maven-publish'
    id 'io.github.gradle-nexus.publish-plugin' version '2.0.0'
}

group = "com.gluonhq"

sourceCompatibility = JavaVersion.VERSION_11

repositories {
    mavenCentral()
}

test {
    dependsOn publishToMavenLocal
    useJUnitPlatform()

    testLogging {
        events 'PASSED', 'FAILED', 'SKIPPED'
        stackTraceFilters = []
    }
}

dependencies {
    implementation 'com.googlecode.plist:dd-plist:1.23'
    implementation 'org.bouncycastle:bcpkix-jdk15on:1.49'
    implementation 'org.graalvm.nativeimage:svm:21.1.0'
    implementation 'org.apidesign.bck2brwsr:aot:0.53'

    testImplementation gradleTestKit()
    testImplementation "org.junit.jupiter:junit-jupiter-api:$jUnitVersion"
    testImplementation "org.junit.jupiter:junit-jupiter-params:$jUnitVersion"
    testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:$jUnitVersion"
}

compileJava {
    doFirst {
        options.compilerArgs = ['--module-path', classpath.filter { f -> !f.name.endsWith(".tar.gz") }.asPath]
        classpath = files()
    }
}

javadoc {
    options.addStringOption("Xdoclint:none", "-quiet")
    options.addBooleanOption('html5', true)
    options.addBooleanOption('javafx', true)
    doFirst {
        options.modulePath = classpath.filter { f -> !f.name.endsWith(".tar.gz") }.flatten()
    }
}

build.dependsOn javadoc

task sourcesJar(type: Jar) {
    from sourceSets.main.allJava
    archiveClassifier = 'sources'
}

task javadocJar(type: Jar) {
    from javadoc
    archiveClassifier = 'javadoc'
}

publishing {
    publications {
        maven(MavenPublication) {
            artifactId = 'substrate'
            from components.java
            artifact sourcesJar
            artifact javadocJar
            pom {
                name = 'Gluon Substrate'
                description = 'Create native Java(FX) apps for desktop, mobile and embedded'
                url = 'https://github.com/gluonhq/substrate'
                licenses {
                    license {
                        name = 'GNU General Public License v2.0'
                        url = 'https://www.gnu.org/licenses/old-licenses/lgpl-2.0.html'
                    }
                }
                developers {
                    developer {
                        name = 'Johan Vos'
                        email = 'johan.vos@gluonhq.com'
                    }
                }
                scm {
                    connection = 'scm:git:git://github.com:gluonhq/substrate.git'
                    developerConnection = 'scm:git:ssh://github.com:gluonhq/substrate.git'
                    url = 'https://github.com/gluonhq/substrate'
                }
            }
        }
    }
}

signing {
    required = { gradle.taskGraph.hasTask(publish) && !version.endsWith("SNAPSHOT") }
    sign publishing.publications.maven
}

nexusPublishing {
    repositories {
        sonatype {
            nexusUrl.set(uri("https://ossrh-staging-api.central.sonatype.com/service/local/"))
            snapshotRepositoryUrl.set(uri("https://central.sonatype.com/repository/maven-snapshots/"))
            username = project.hasProperty('sonatypeUsername') ? project.property('sonatypeUsername') : ''
            password = project.hasProperty('sonatypePassword') ? project.property('sonatypePassword') : ''
        }
    }
    transitionCheckOptions {
        maxRetries = 100
        delayBetween = Duration.ofSeconds(10)
    }
}