buildscript {
    ext {
        pluginName = 'analysis-morfologik'
        pluginClassname = 'pl.allegro.tech.elasticsearch.plugin.analysis.morfologik.AnalysisMorfologikPlugin'
        pluginDescription = 'Morfologik Polish Lemmatizer plugin for Elasticsearch'
        versions = [
                'elasticsearch' : '9.1.5',
                'lucene'        : '8.11.3',
                'log4j'         : '2.20.0',
                'spock'         : '2.3-groovy-3.0',
                'groovy'        : '3.0.21',
                'testcontainers': '1.19.7'
        ]
    }
}

plugins {
    id 'signing'
    id 'maven-publish'
    id 'io.github.gradle-nexus.publish-plugin' version '1.2.0'
    id 'com.adarshr.test-logger' version '4.0.0'
}

apply plugin: 'java'
apply plugin: 'groovy'

group = 'pl.allegro.tech.elasticsearch.plugin'
version = versions.elasticsearch

repositories {
    mavenCentral()
}

configurations {
    providedCompile
    wagon
    releaseJars {
        extendsFrom implementation
        exclude group: 'org.elasticsearch'
        exclude group: 'com.fasterxml.jackson.core', module: 'jackson-core'
        exclude group: 'org.slf4j'
        exclude group: 'log4j'
        exclude module: 'lucene-core'
        exclude module: 'lucene-analyzers-common'
    }
}

dependencies {
    implementation(group: 'org.elasticsearch', name: 'elasticsearch', version: versions.elasticsearch) {
        exclude group: 'org.elasticsearch', module: 'elasticsearch-preallocate'
    }
    implementation(group: 'org.apache.lucene', name: 'lucene-analyzers-morfologik', version: versions.lucene) {
        exclude group: 'ua.net.nlp'
    }
    testImplementation group: 'org.testcontainers', name: 'testcontainers', version: versions.testcontainers
    testImplementation group: 'org.apache.commons', name: 'commons-compress', version: '1.26.1'
    testImplementation group: 'org.apache.logging.log4j', name: 'log4j-slf4j-impl', version: versions.log4j
    testImplementation group: 'org.spockframework', name: 'spock-core', version: versions.spock
    testImplementation group: 'org.codehaus.groovy', name: 'groovy-all', version: versions.groovy
}

compileJava {
    sourceCompatibility = JavaVersion.VERSION_21
    targetCompatibility = JavaVersion.VERSION_21
}

tasks.withType(GroovyCompile).configureEach {
    options.compilerArgs << "-proc:none"
}

test {
    systemProperties['path.home'] = System.getProperty("user.dir")
    systemProperties['elasticsearchVersion'] = versions.elasticsearch
    useJUnitPlatform()
    testLogging {
        showStandardStreams = false
        exceptionFormat = 'full'
    }
}

testlogger {
    theme 'mocha'
}

tasks.register('makePluginDescriptor', Copy) {
    from 'src/main/templates'
    into layout.buildDirectory.dir("tmp/plugin")
    expand([
            'descriptor': [
                    'name'                : pluginName,
                    'classname'           : pluginClassname,
                    'description'         : pluginDescription,
                    'version'             : project.property('version'),
                    'javaVersion'         : JavaVersion.VERSION_17,
                    'elasticsearchVersion': versions.elasticsearch
            ]
    ])
}

task buildPluginZip(type: Zip, dependsOn: [':javadocJar', ':sourcesJar', ':jar', ':makePluginDescriptor']) {
    from layout.buildDirectory.dir("libs")
    from configurations.releaseJars
    from 'build/tmp/plugin'
}

tasks.register('unpackPlugin', Copy) {
    dependsOn[':buildPluginZip']
    delete "plugins"
    from layout.buildDirectory.dir("libs")
    from configurations.releaseJars
    from 'build/tmp/plugin'
    into "plugins/${pluginName}"
}

tasks.withType(Test).configureEach {
    testLogging {
        exceptionFormat = 'full'
        events = ["failed", "skipped", "passed"]
        showCauses = true
        showStackTraces = true
    }
}

java {
    withSourcesJar()
    withJavadocJar()
}

artifacts {
    archives buildPluginZip
}

wrapper {
    gradleVersion = '8.9'
    distributionType = Wrapper.DistributionType.ALL
}

publishing {
    publications {
        sonatype(MavenPublication) {
            from components.java
            artifact buildPluginZip

            pom {
                name = 'elasticsearch-analysis-morfologik'
                description = 'Morfologik Polish Lemmatizer plugin for Elasticsearch'
                url = 'https://github.com/allegro/elasticsearch-analysis-morfologik'
                inceptionYear = '2017'
                licenses {
                    license {
                        name = "The Apache License, Version 2.0"
                        url = "http://www.apache.org/licenses/LICENSE-2.0.txt"
                    }
                }
                developers {
                    developer {
                        id = "pbobruk"
                        name = "Paweł Bobruk"
                    }
                }
                scm {
                    connection = "scm:git@github.com:allegro/elasticsearch-analysis-morfologik.git"
                    developerConnection = "scm:git@github.com:allegro/elasticsearch-analysis-morfologik.git"
                    url = "https://github.com/allegro/elasticsearch-analysis-morfologik"
                }
            }
        }
    }
}

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 = System.getenv("SONATYPE_USERNAME")
            password = System.getenv("SONATYPE_PASSWORD")
        }
    }
}

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
    }
}
