import aQute.bnd.gradle.Bundle
import io.github.gradlenexus.publishplugin.NexusRepository

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'biz.aQute.bnd:biz.aQute.bnd.gradle:6.4.0'
        classpath "org.jfrog.buildinfo:build-info-extractor-gradle:latest.release"
    }
}
plugins {
    id 'net.researchgate.release' version '3.0.2'
    id 'com.github.hierynomus.license' version '0.16.1'
    id 'maven-publish'
    id 'signing'
    id "io.github.gradle-nexus.publish-plugin" version "1.3.0"
}

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'biz.aQute.bnd.builder'
apply plugin: 'maven-publish'


// custom tasks for creating source/javadoc jars
tasks.register('sourcesJar', Jar) {
    dependsOn classes
    archiveClassifier = 'sources'
    from sourceSets.main.allSource
}

tasks.register('javadocJar', Jar) {
    dependsOn javadoc
    archiveClassifier = 'javadoc'
    from javadoc.destinationDir
}

release {
    tagTemplate = 'v${version}'
    failOnPublishNeeded = false
    failOnCommitNeeded = false
}

repositories {
    mavenCentral()
}

gradle.projectsEvaluated {
    tasks.withType(JavaCompile).tap {
        configureEach {
            doLast {
                options.compilerArgs += "-parameters"
            }
        }
    }
}

dependencies {
    implementation 'javax.validation:validation-api:1.1.0.Final'
    implementation 'com.graphql-java:graphql-java:24.3'
    implementation 'com.graphql-java:graphql-java-extended-scalars:24.0'
    implementation 'javax.xml.bind:jaxb-api:2.3.1'

    // OSGi
    compileOnly 'org.osgi:org.osgi.core:6.0.0'
    compileOnly 'org.osgi:org.osgi.service.cm:1.5.0'
    compileOnly 'org.osgi:org.osgi.service.component:1.3.0'
    compileOnly 'biz.aQute.bnd:biz.aQute.bndlib:3.2.0'

    testImplementation 'org.testng:testng:7.5.1'
    testImplementation 'org.hamcrest:hamcrest-all:1.3'
    testImplementation 'org.mockito:mockito-core:2.+'
}

test.useTestNG()

publishing {
    publications {
        maven(MavenPublication) {
            from components.java
            groupId 'io.github.graphql-java'
            artifactId project.name
            version project.version

            artifact sourcesJar
            artifact javadocJar

            pom.withXml {
                asNode().children().last() + {
                    resolveStrategy = Closure.DELEGATE_FIRST
                    name 'graphql-java-annotations'
                    description 'Annotations-based syntax for GraphQL schema definition'
                    url 'https://github.com/graphql-java/graphql-java-annotations'
                    inceptionYear '2016'

                    scm {
                        url 'https://github.com/graphql-java/graphql-java-annotations'
                        connection 'scm:https://yrashk@github.com/graphql-java/graphql-java-annotations.git'
                        developerConnection 'scm:git://github.com/graphql-java/graphql-java-annotations.git'
                    }

                    licenses {
                        license {
                            name 'The Apache Software License, Version 2.0'
                            url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                            distribution 'repo'
                        }
                    }

                    developers {
                        developer {
                            id 'yrashk'
                            name 'Yurii Rashkovskii'
                            email 'yrashk@gmail.com'
                        }
                        developer {
                            id 'apottere'
                            name 'Andrew Potter'
                            email 'Andrew.Potter@coxautoinc.com'
                        }
                        developer {
                            id 'bbakerman'
                            name 'Brad Baker'
                            email 'bbakerman@gmail.com'
                        }
                        developer {
                            id 'yarinvak'
                            name 'Yarin Vaknin'
                            email 'yarinvak@gmail.com'
                        }
                        developer {
                            id 'guy120494'
                            name 'Guy Smorodinsky'
                        }
                        developer {
                            id 'osher-sade'
                            name 'Osher Sade'
                        }
                    }
                }
            }
        }
    }
}

afterReleaseBuild.dependsOn nexusPublishing

nexusPublishing {
    repositories {
        sonatype {
            username = System.getenv("MAVEN_CENTRAL_USERTOKEN_USERNAME")
            password = System.getenv("MAVEN_CENTRAL_USERTOKEN_PASSWORD")
        }
    }
}

// to publish to local maven repo skip signing: ./gradlew publishToMavenLocal  -x signGraphqlJavaPublication
signing {
    def signingKey = System.getenv("MAVEN_CENTRAL_PGP_KEY")
    useInMemoryPgpKeys(signingKey, "")
    sign publishing.publications
}

tasks.register('bundle', Bundle) {
    from sourceSets.main.output
}

wrapper {
    gradleVersion = '8.3'
}
