/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * License); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.nio.file.Files
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING


apply plugin: 'java'
apply from: LOGSTASH_CORE_PATH + "/../rubyUtils.gradle"

// ===========================================================================
// plugin info
// ===========================================================================
group                      'org.apache.rocketmq.logstashplugin.input' // must match the package of the main plugin class
version                    "${file("VERSION").text.trim()}" // read from required VERSION file
description                = "A logstash input plugin for sending events to Apache RocketMQ"
pluginInfo.licenses        = ['Apache-2.0'] 
pluginInfo.longDescription = "This gem is a Logstash plugin required to be installed on top of the Logstash core pipeline using \$LS_HOME/bin/logstash-plugin install gemname. This gem is not a stand-alone program"
pluginInfo.authors         = ['RocketMQ developers']
pluginInfo.email           = ['users@rocketmq.apache.org']
pluginInfo.homepage        = "https://rocketmq.apache.org/"
pluginInfo.pluginType      = "input"
pluginInfo.pluginClass     = "RocketMQ"
pluginInfo.pluginName      = "rocketmq" // must match the @LogstashPlugin annotation in the main plugin class
// ===========================================================================

sourceCompatibility = 1.8
targetCompatibility = 1.8


buildscript {
    repositories {
        mavenCentral()
        jcenter()
    }

    dependencies {
        classpath 'com.github.jengelman.gradle.plugins:shadow:4.0.4'
    }
}



repositories {
    mavenCentral()
}

apply plugin: 'com.github.johnrengelman.shadow'

shadowJar {
    archiveClassifier = null
}

dependencies {
    implementation 'org.apache.rocketmq:rocketmq-client:4.9.2'
    implementation 'org.apache.logging.log4j:log4j-api:2.17.1'
    implementation fileTree(dir: LOGSTASH_CORE_PATH, include: "**/logstash-core-?.?.?.jar")

    testImplementation 'junit:junit:4.12'
    testImplementation 'org.jruby:jruby-complete:9.1.13.0'
    testImplementation 'org.apache.logging.log4j:log4j-core:2.9.1'


}

clean {
    delete "${projectDir}/Gemfile"
    delete "${projectDir}/" + pluginInfo.pluginFullName() + ".gemspec"
    delete "${projectDir}/lib/"
    delete "${projectDir}/vendor/"
    new FileNameFinder().getFileNames(projectDir.toString(), pluginInfo.pluginFullName() + "-?.?.?.gem").each { filename ->
        delete filename
    }
}

tasks.withType(JavaCompile) {
    options.encoding = 'UTF-8'
}

tasks.register("vendor"){
    dependsOn shadowJar
    doLast {
        String vendorPathPrefix = "vendor/jar-dependencies"
        String projectGroupPath = project.group.replaceAll('\\.', '/')
        File projectJarFile = file("${vendorPathPrefix}/${projectGroupPath}/${pluginInfo.pluginFullName()}/${project.version}/${pluginInfo.pluginFullName()}-${project.version}.jar")
        projectJarFile.mkdirs()
        Files.copy(file("$buildDir/libs/${project.name}-${project.version}.jar").toPath(), projectJarFile.toPath(), REPLACE_EXISTING)
        validatePluginJar(projectJarFile, project.group)
    }
}

tasks.register("generateRubySupportFiles") {
    doLast {
        generateRubySupportFilesForPlugin(project.description, project.group, version)
    }
}

tasks.register("removeObsoleteJars") {
    doLast {
        new FileNameFinder().getFileNames(
                projectDir.toString(),
                "vendor/**/" + pluginInfo.pluginFullName() + "*.jar",
                "vendor/**/" + pluginInfo.pluginFullName() + "-" + version + ".jar").each { f ->
            delete f
        }
    }
}

tasks.register("gem"){
    dependsOn downloadAndInstallJRuby
    dependsOn removeObsoleteJars
    dependsOn vendor
    dependsOn generateRubySupportFiles

    doLast {
        buildGem(projectDir, buildDir, pluginInfo.pluginFullName() + ".gemspec")
    }
}


