buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "io.codearte.gradle.nexus:gradle-nexus-staging-plugin:0.5.1"
    }
}

plugins {
    id 'org.jetbrains.kotlin.jvm' version '1.6.21'
    id 'maven-publish'
    id 'signing'
}

apply plugin: 'io.codearte.nexus-staging'

def getVersionName = { ->
    def stdout = new ByteArrayOutputStream()
    exec {
        commandLine 'git', 'describe', '--tags'
        standardOutput = stdout
    }
    return stdout.toString().trim()
}

nexusStaging {
    packageGroup = "org.elder.sourcerer"
    username = ossrhUsername
    password = ossrhPassword
}

if ("$official_release" == 'true') {
    project.ext.set("use_signing", true)
    project.ext.set("signing.keyId", sourcerer_signing_keyId)
    project.ext.set("signing.password", sourcerer_signing_password)
    project.ext.set("signing.secretKeyRingFile", sourcerer_signing_secretKeyRingFile)
}

def mavenPublishRepo
def mavenPublishUsername
def mavenPublishPassword

if ("$official_release" == 'true') {
    mavenPublishRepo = "https://oss.sonatype.org/service/local/staging/deploy/maven2/"
    mavenPublishUsername = ossrhUsername
    mavenPublishPassword = ossrhPassword
} else {
    mavenPublishRepo = sourcerer_nexus_releaseUrl
    mavenPublishUsername = sourcerer_nexus_username
    mavenPublishPassword = sourcerer_nexus_password
}

allprojects {
    group = 'org.elder.sourcerer'
    version = getVersionName()

    apply plugin: 'java'
    apply plugin: 'java-library'
    apply plugin: 'maven-publish'
    apply plugin: 'checkstyle'
    apply plugin: 'jacoco'
    apply plugin: 'signing'

    sourceCompatibility = 1.8
    targetCompatibility = 1.8

    sourceSets {
        integrationTest {
            resources.srcDir "$projectDir/src/integrationTest/resources"
            compileClasspath += main.output + test.output
            runtimeClasspath += main.output + test.output
        }
    }

    configurations {
        integrationTestImplementation.extendsFrom testImplementation
        integrationTestRuntime.extendsFrom testRuntime
    }

    task integrationTest(type: Test) {
        testClassesDirs = sourceSets.integrationTest.output.classesDirs
        classpath = sourceSets.integrationTest.runtimeClasspath
    }

    checkstyle {
        configFile = new File(rootDir, "infra/codehealth/checkstyle.xml")
        toolVersion = "7.1.1"
        ignoreFailures = false
    }

    jacoco {
        reportsDir = file("build/coverage/jacoco.exec")
    }

    repositories {
        mavenCentral()
    }

    dependencies {
        implementation 'ch.qos.logback:logback-classic:1.1.7'
        implementation 'org.slf4j:jcl-over-slf4j:1.7.21'
        testImplementation 'junit:junit:4.12'
        testImplementation 'org.hamcrest:hamcrest-all:1.3'
        testImplementation 'org.mockito:mockito-core:1.10.19'
    }

    test {
        reports {
            junitXml.enabled = true
            html.enabled = true
        }
    }

    task sourcesJar(type: Jar, dependsOn: classes) {
        classifier = 'sources'
        from sourceSets.main.allSource
    }

    task javadocJar(type: Jar, dependsOn: javadoc) {
        classifier = 'javadoc'
        from javadoc.destinationDir
    }

    artifacts {
        archives sourcesJar
        archives javadocJar
    }

    publishing {
        publications {
            mavenJava(MavenPublication) {
                from components.java

                artifact sourcesJar {
                    classifier "sources"
                }

                artifact javadocJar {
                    classifier "javadoc"
                }

                pom {
                    name = 'Elder Sourcerer'
                    packaging = 'jar'
                    description = 'An opinionated framework for implementing an CQRS architecture using event sourcing'
                    url = 'https://github.com/elder-oss/sourcerer'

                    scm {
                        connection = 'scm:git:https://github.com/elder-oss/sourcerer.git'
                        developerConnection = 'scm:svn:https://github.com/elder-oss/sourcerer.git'
                        url = 'https://github.com/elder-oss/sourcerer'
                    }

                    licenses {
                        license {
                            name = 'The MIT License (MIT)'
                            url = 'https://opensource.org/licenses/MIT'
                        }
                    }

                    developers {
                        developer {
                            id = 'elder-developers'
                            name = 'Developers At Elder'
                            email = 'tech@elder.org'
                        }
                    }
                }
            }
        }

        repositories {
            maven {
                credentials {
                    username mavenPublishUsername
                    password mavenPublishPassword
                }
                url mavenPublishRepo
            }
        }
    }

    signing {
        required { "$official_release" == 'true' }
        sign publishing.publications.mavenJava
    }
}
