/*
 * 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.
 */

apply from: "$rootDir/buildscripts/java-core.gradle"
apply from: "$rootDir/buildscripts/publishing.gradle"
apply from: "$rootDir/buildscripts/java-junit5.gradle"
apply from: "$rootDir/buildscripts/java-test-fixtures.gradle"
apply from: "$rootDir/buildscripts/java-integration-test.gradle"
apply from: "$rootDir/buildscripts/generate-version-file.gradle"

description = 'ignite-runner'

sourceSets {
    defaultsGenerator {
        compileClasspath += sourceSets.main.output
        runtimeClasspath += sourceSets.main.output
    }
}

configurations {
    defaultsGeneratorImplementation.extendsFrom implementation
    defaultsGenerator {
        canBeConsumed = true
        canBeResolved = false
        // this configuration will share the same dependencies as the main one
        extendsFrom implementation, runtimeOnly
    }
}

dependencies {
    annotationProcessor project(':ignite-configuration-annotation-processor')
    annotationProcessor libs.picocli.annotation.processor
    annotationProcessor libs.micronaut.inject.annotation.processor
    annotationProcessor libs.auto.service

    implementation project(':ignite-configuration')
    implementation project(':ignite-configuration-root')
    implementation project(':ignite-configuration-system')
    implementation project(':ignite-configuration-presentation')
    implementation project(':ignite-rest')
    implementation project(':ignite-api')
    implementation project(':ignite-vault')
    implementation project(':ignite-network')
    implementation project(':ignite-raft-api')
    implementation project(':ignite-raft')
    implementation project(':ignite-metastorage')
    implementation project(':ignite-metastorage-cache')
    implementation project(':ignite-partition-distribution')
    implementation project(':ignite-table')
    implementation project(':ignite-index')
    implementation project(':ignite-sql-engine-api')
    implementation project(':ignite-sql-engine')
    implementation project(':ignite-client-handler')
    implementation project(':ignite-compute')
    implementation project(':ignite-storage-rocksdb')
    implementation project(':ignite-storage-page-memory')
    implementation project(':ignite-transactions')
    implementation project(':ignite-schema')
    implementation project(':ignite-storage-api')
    implementation project(':ignite-rest-api')
    implementation project(':ignite-core')
    implementation project(':ignite-cluster-management')
    implementation project(':ignite-metrics')
    implementation project(':ignite-cluster-metrics')
    implementation project(':ignite-metrics-exporter-otlp')
    implementation project(':ignite-replicator')
    implementation project(':ignite-distribution-zones')
    implementation project(':ignite-placement-driver')
    implementation project(':ignite-code-deployment')
    implementation project(':ignite-security-api')
    implementation project(':ignite-security')
    implementation project(':ignite-catalog')
    implementation project(':ignite-system-view-api')
    implementation project(':ignite-system-view')
    implementation project(':ignite-workers')
    implementation project(':ignite-failure-handler')
    implementation project(':ignite-catalog-dsl')
    implementation project(':ignite-eventlog')
    implementation project(':ignite-low-watermark')
    implementation project(':ignite-partition-replicator')
    implementation project(':ignite-catalog-compaction')
    implementation project(':ignite-system-disaster-recovery')
    implementation project(':ignite-system-disaster-recovery-api')
    implementation project(':ignite-configuration-storage')
    implementation libs.jetbrains.annotations
    implementation libs.micronaut.inject
    implementation libs.micronaut.validation
    implementation libs.micronaut.runtime
    implementation libs.micronaut.picocli
    implementation libs.picocli.core
    implementation libs.typesafe.config
    implementation libs.auto.service.annotations
    implementation libs.disruptor

    testAnnotationProcessor project(':ignite-configuration-annotation-processor')
    testAnnotationProcessor libs.auto.service

    testImplementation project(':ignite-cluster-management')
    testImplementation project(':ignite-configuration')
    testImplementation project(':ignite-core')
    testImplementation project(':ignite-network')
    testImplementation project(':ignite-vault')
    testImplementation project(':ignite-client')
    testImplementation testFixtures(project(':ignite-core'))
    testImplementation testFixtures(project(':ignite-configuration'))
    testImplementation testFixtures(project(':ignite-vault'))
    testImplementation testFixtures(project(':ignite-metastorage'))
    testImplementation testFixtures(project(':ignite-jdbc'))
    testImplementation(libs.jsonpath.assert) {
        //IDEA test runner doesn't apply Gradle dependency resolve strategy, this is just not implemented
        //So, exclude asm-core transitive dependency to protect of jar-hell.
        exclude group: 'org.ow2.asm', module: 'asm'
    }
    testImplementation libs.auto.service.annotations

    integrationTestAnnotationProcessor project(':ignite-configuration-annotation-processor')
    integrationTestAnnotationProcessor libs.jmh.annotation.processor
    integrationTestImplementation project(':ignite-partition-distribution')
    integrationTestImplementation project(':ignite-jdbc')
    integrationTestImplementation project(':ignite-page-memory')
    integrationTestImplementation project(':ignite-raft-api')
    integrationTestImplementation project(':ignite-replicator')
    integrationTestImplementation project(':ignite-client')
    integrationTestImplementation project(':ignite-compute')
    integrationTestImplementation project(':ignite-client-handler')
    integrationTestImplementation project(':ignite-storage-api')
    integrationTestImplementation project(':ignite-storage-api')
    integrationTestImplementation project(':ignite-storage-page-memory')
    integrationTestImplementation project(':ignite-storage-rocksdb')
    integrationTestImplementation project(':ignite-index')
    integrationTestImplementation project(':ignite-raft')
    integrationTestImplementation project(':ignite-rest')
    integrationTestImplementation project(':ignite-rest-api')
    integrationTestImplementation project(':ignite-sql-engine-api')
    integrationTestImplementation(project(':ignite-sql-engine')) {
        //IDEA test runner don't apply Gradle dependency resolve strategy, this is just not implemented
        //So, exclude asm-core transitive dependency to protect of jar-hell.
        exclude group: 'com.fasterxml.jackson.core', module: 'jackson-databind'
    }
    integrationTestImplementation project(':ignite-metastorage')
    integrationTestImplementation project(':ignite-metastorage-cache')
    integrationTestImplementation project(':ignite-metrics')
    integrationTestImplementation project(':ignite-table')
    integrationTestImplementation project(':ignite-transactions')
    integrationTestImplementation project(':ignite-code-deployment')
    integrationTestImplementation project(':ignite-jdbc')
    integrationTestImplementation project(':ignite-security')
    integrationTestImplementation project(':ignite-catalog')
    integrationTestImplementation project(':ignite-placement-driver')
    integrationTestImplementation project(':ignite-distribution-zones')
    integrationTestImplementation project(':ignite-system-view')
    integrationTestImplementation project(':ignite-system-view-api')
    integrationTestImplementation project(':ignite-workers')
    integrationTestImplementation project(':ignite-failure-handler')
    integrationTestImplementation project(':ignite-low-watermark')
    integrationTestImplementation project(':ignite-partition-replicator')
    integrationTestImplementation project(':ignite-configuration-root')
    integrationTestImplementation project(':ignite-configuration-system')
    integrationTestImplementation project(':ignite-system-disaster-recovery')
    integrationTestImplementation project(':ignite-eventlog')
    integrationTestImplementation project(':ignite-configuration-storage')
    integrationTestImplementation project(':ignite-configuration-presentation')
    integrationTestImplementation testFixtures(project(':ignite-api'))
    integrationTestImplementation testFixtures(project(':ignite-core'))
    integrationTestImplementation testFixtures(project(':ignite-configuration'))
    integrationTestImplementation testFixtures(project(':ignite-distribution-zones'))
    integrationTestImplementation testFixtures(project(':ignite-schema'))
    integrationTestImplementation testFixtures(project(':ignite-cluster-management'))
    integrationTestImplementation testFixtures(project(':ignite-metastorage'))
    integrationTestImplementation testFixtures(project(':ignite-network'))
    integrationTestImplementation testFixtures(project(':ignite-vault'))
    integrationTestImplementation testFixtures(project(':ignite-table'))
    integrationTestImplementation testFixtures(project(':ignite-storage-api'))
    integrationTestImplementation testFixtures(project(':ignite-sql-engine'))
    integrationTestImplementation testFixtures(project(':ignite-transactions'))
    integrationTestImplementation testFixtures(project(':ignite-runner'))
    integrationTestImplementation testFixtures(project(':ignite-placement-driver-api'))
    integrationTestImplementation testFixtures(project(':ignite-jdbc'))
    integrationTestImplementation testFixtures(project(':ignite-failure-handler'))
    integrationTestImplementation testFixtures(project(':ignite-metrics:'))
    integrationTestImplementation testFixtures(project(':ignite-raft'))
    integrationTestImplementation testFixtures(project(':ignite-replicator'))
    integrationTestImplementation testFixtures(project(':ignite-catalog'))
    integrationTestImplementation libs.awaitility
    integrationTestImplementation libs.rocksdb.jni
    integrationTestImplementation libs.disruptor
    integrationTestImplementation libs.jackson.databind
    integrationTestImplementation libs.jmh.core
    integrationTestImplementation libs.typesafe.config
    integrationTestImplementation libs.netty.common
    integrationTestImplementation libs.netty.handler

    testFixturesAnnotationProcessor libs.auto.service

    testFixturesImplementation project(':ignite-core')
    testFixturesImplementation project(':ignite-api')
    testFixturesImplementation project(':ignite-raft-api')
    testFixturesImplementation project(':ignite-raft')
    testFixturesImplementation project(':ignite-sql-engine')
    testFixturesImplementation project(':ignite-table')
    testFixturesImplementation project(':ignite-schema')
    testFixturesImplementation project(':ignite-metastorage')
    testFixturesImplementation project(':ignite-transactions')
    testFixturesImplementation project(':ignite-system-view-api')
    testFixturesImplementation project(':ignite-system-view')
    testFixturesImplementation project(':ignite-storage-api')
    testFixturesImplementation project(':ignite-cluster-management')
    testFixturesImplementation project(':ignite-configuration')
    testFixturesImplementation project(':ignite-vault')
    testFixturesImplementation project(':ignite-placement-driver-api')
    testFixturesImplementation project(':ignite-partition-distribution')
    testFixturesImplementation project(':ignite-configuration-storage')
    testFixturesImplementation testFixtures(project(':ignite-api'))
    testFixturesImplementation testFixtures(project(':ignite-core'))
    testFixturesImplementation testFixtures(project(':ignite-network'))
    testFixturesImplementation testFixtures(project(':ignite-catalog'))
    testFixturesImplementation testFixtures(project(':ignite-table'))
    testFixturesImplementation testFixtures(project(':ignite-storage-api'))
    testFixturesImplementation libs.typesafe.config
}

tasks.register('postRelease', JavaExec) {
    def testResourcePath = sourceSets.test.resources.srcDirs.iterator().next().absolutePath
    def snapshotFilePath = java.nio.file.Path.of(testResourcePath, "compatibility/configuration", "ignite-${projectVersion}.bin").toString()

    description = 'Generates compatibility snapshot for the project.'
    classpath = sourceSets.test.runtimeClasspath
    mainClass = 'org.apache.ignite.internal.configuration.compatibility.GenerateConfigurationSnapshot'
    args = [snapshotFilePath]
    jvmArgs += defaultJvmArgs

    doFirst {
        if (version.toString().endsWith('-SNAPSHOT')) {
            throw new GradleException("The task can be executed only on release version: ${version}")
        }
    }
}

tasks.register('runnerPlatformTest', JavaExec) {
    dependsOn tasks.integrationTestClasses
    mainClass = 'org.apache.ignite.internal.runner.app.PlatformTestNodeRunner'

    jvmArgs += defaultJvmArgs

    classpath = sourceSets.integrationTest.runtimeClasspath

    enableAssertions = true
}

tasks.register('runnerPlatformBenchmark', JavaExec) {
    mainClass = 'org.apache.ignite.internal.runner.app.PlatformBenchmarkNodeRunner'

    jvmArgs += defaultJvmArgs

    classpath = sourceSets.integrationTest.runtimeClasspath

    enableAssertions = true
}

tasks.register('runUpsertBenchmark', JavaExec) {
    mainClass = 'org.apache.ignite.internal.benchmark.UpsertKvBenchmark'

    jvmArgs += addOpens + ['-Dio.netty.tryReflectionSetAccessible=true', '-Xmx16g']

    classpath = sourceSets.integrationTest.runtimeClasspath

    enableAssertions = true
}

tasks.register('runClientPutBenchmark', JavaExec) {
    mainClass = 'org.apache.ignite.internal.benchmark.ClientKvPutBenchmark'

    jvmArgs += addOpens + ['-Dio.netty.tryReflectionSetAccessible=true', '-Xmx16g']

    classpath = sourceSets.integrationTest.runtimeClasspath

    enableAssertions = true
}

tasks.register('runClientGetBenchmark', JavaExec) {
    mainClass = 'org.apache.ignite.internal.benchmark.ClientKvGetBenchmark'

    jvmArgs += addOpens + ['-Dio.netty.tryReflectionSetAccessible=true', '-Xmx16g']

    classpath = sourceSets.integrationTest.runtimeClasspath

    enableAssertions = true
}

tasks.register('runClientGetAllBenchmark', JavaExec) {
    mainClass = 'org.apache.ignite.internal.benchmark.ClientKvGetAllBenchmark'

    jvmArgs += addOpens + ['-Dio.netty.tryReflectionSetAccessible=true', '-Xmx16g']

    classpath = sourceSets.integrationTest.runtimeClasspath

    enableAssertions = true
}

tasks.register('runClientGetAllExplicitBenchmark', JavaExec) {
    mainClass = 'org.apache.ignite.internal.benchmark.ClientKvGetAllExplicitBenchmark'

    jvmArgs += addOpens + ['-Dio.netty.tryReflectionSetAccessible=true', '-Xmx16g']

    classpath = sourceSets.integrationTest.runtimeClasspath

    enableAssertions = true
}

jar {
    manifest {
        attributes(
                'Main-Class': 'org.apache.ignite.internal.app.IgniteRunner'
        )
    }
}


tasks.register('defaultsJar', Jar) {
    from sourceSets.defaultsGenerator.output
    archiveAppendix = 'generator'
}

artifacts {
    // declare both jars to provide enough dependencies to the caller
    defaultsGenerator defaultsJar, jar
}
