import java.time.Duration

plugins {
    id 'java'
    alias(libs.plugins.axion.release)
    alias(libs.plugins.nexus.publish)
}

scmVersion {
    tag {
        prefix = 'json-avro-converter-'
    }
    versionCreator 'versionWithBranch'

    hooks {
        pre 'fileUpdate', [files: ['README.md'], pattern: { v, p -> /'$v'/ }, replacement: { v, p -> "'$v'" }]
        pre 'commit'
    }
}

nexusPublishing {
    packageGroup = "tech.allegro"
    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 = System.getenv("SONATYPE_USERNAME")
            password = System.getenv("SONATYPE_PASSWORD")
        }
    }
    transitionCheckOptions {
        maxRetries.set(15)
        delayBetween.set(Duration.ofMillis(5000))
    }
}

sourceCompatibility = 17

allprojects {
    project.group = 'tech.allegro.schema.json2avro'
    project.version = scmVersion.version
}

subprojects {
    apply plugin: 'java'

    repositories {
        mavenCentral()
        mavenLocal()
    }

    test {
        useJUnitPlatform()
        testLogging {
            exceptionFormat 'full'
        }
    }

    tasks.withType(JavaCompile) {
        options.compilerArgs += ['-Xlint:unchecked', '-Xlint:deprecation']
    }
}

configure(subprojects - project(':validator')) {
    apply plugin: 'maven-publish'
    apply plugin: 'signing'

    java {
        withJavadocJar()
        withSourcesJar()
    }

    publishing {
        publications {
            mavenJava(MavenPublication) {
                artifactId = project.name
                from(components.java)

                pom {
                    name = project.name
                    description = 'JSON to Avro conversion tool designed to make migration to Avro easier'
                    url = 'https://github.com/allegro/json-avro-converter'
                    inceptionYear = '2016'

                    scm {
                        url = 'https://github.com/allegro/json-avro-converter'
                        connection = 'scm:git@github.com:allegro/json-avro-converter.git'
                        developerConnection = 'scm:git@github.com:allegro/json-avro-converter.git'
                    }

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

                    developers {
                        developer {
                            id = 'skylab'
                            name = 'Team Skylab'
                        }
                    }
                }
            }
        }
    }

    if (System.getenv("GPG_KEY_ID")) {
        signing {
            useInMemoryPgpKeys(
                    System.getenv("GPG_KEY_ID"),
                    System.getenv("GPG_PRIVATE_KEY"),
                    System.getenv("GPG_PRIVATE_KEY_PASSWORD")
            )
            sign publishing.publications.mavenJava
        }
    }
}
