/*
 * Copyright 2021 DataCanvas
 *
 * Licensed 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.
 */

plugins {
    id 'application'
    id 'java-conventions'
    id 'java-library'
    id "com.gorylenko.gradle-git-properties" version "${gradleGitCommitVersion}"
    id "com.github.johnrengelman.shadow" version "7.1.2"
}

application {
    getMainClass().set('io.dingodb.server.executor.Starter')
}

dependencies {
    annotationProcessor group: 'com.google.auto.service', name: 'auto-service', version: 'auto-service'.v()
    compileOnly group: 'com.google.auto.service', name: 'auto-service', version: 'auto-service'.v()

    implementation group: 'ch.qos.logback', name: 'logback-core', version: 'logback'.v()
    implementation group: 'ch.qos.logback', name: 'logback-classic', version: 'logback'.v()
    implementation group: 'com.beust', name: 'jcommander', version: 'jcommander'.v()
    implementation group: 'org.apache.calcite.avatica', name: 'avatica-server', version: 'avatica'.v()
    implementation group: 'org.quartz-scheduler', name: 'quartz', version: 'quartz'.v()
    implementation group: 'org.apache.commons', name: 'commons-lang3', version: 'commons-lang3'.v()
    implementation group: 'io.grpc', name: 'grpc-core', version: 'grpc'.v()
    implementation group: 'io.grpc', name: 'grpc-netty-shaded', version: 'grpc'.v()
    implementation group: 'io.grpc', name: 'grpc-stub', version: 'grpc'.v()

    // implementation group: 'io.dingodb', name: 'dingo-sdk', version: project.version

    implementation ("io.dingodb:dingo-sdk:$project.version") {
        exclude group: 'io.grpc', module: 'grpc-netty-shaded'
    }

    implementation project(":dingo-common")
    implementation project(":dingo-calcite")
    implementation project(":dingo-verify")

    implementation project(":dingo-codec-api")
    implementation project(':dingo-net-api')
    implementation project(":dingo-store-api")
    implementation project(":dingo-scheduler-api")
    implementation project(":dingo-tool-api")
    implementation project(':dingo-store-proxy')
    implementation project(":dingo-partition-base")
    implementation project(":dingo-tool-service")
    implementation project(":dingo-transaction-api")
    implementation project(":dingo-license-api")

    implementation project(":dingo-driver:dingo-mysql-service")

    implementation project(':dingo-driver:dingo-driver-host')
    implementation project(':dingo-driver:dingo-driver-client')
    implementation group: 'com.alibaba.fastjson2', name: 'fastjson2', version: 'fastjson'.v()
    implementation group: 'org.apache.commons', name: 'commons-pool2', version: 'commons-pool2'.v()
    implementation group: 'io.dropwizard.metrics', name: 'metrics-core', version: 'metrics-core'.v()
    testImplementation project(":dingo-store-local")
}

task fatJar(type: Jar) {
    manifest {
        attributes 'Main-Class': 'io.dingodb.server.executor.Starter'
    }
    classifier 'all'
    zip64=true
    from(
        configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) },
    )
    with jar

    exclude "META-INF/*.SF"
    exclude "META-INF/*.DSA"
    exclude "META-INF/*.RSA"
    eachFile { details ->
        if (details.path == 'META-INF/services/io.grpc.NameResolverProvider') {
            def content = details.file.text.replaceAll("io.grpc.netty.shaded.io.grpc.netty.UdsNameResolverProvider", "")
            details.file.text = content
        }
    }
}

tasks.withType(Jar).configureEach {
    duplicatesStrategy = DuplicatesStrategy.INCLUDE
}

tasks.named("build") { x ->
    x.finalizedBy("fatJar")
}
