/*
 * 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 groovy.json.JsonOutput

plugins { id 'org.apache.beam.module'
    id 'org.jetbrains.kotlin.jvm' version '1.6.10'
}

applyJavaNature(exportJavadoc: false,  automaticModuleName: 'org.apache.beam.examples.kotlin')
provideIntegrationTestingDependencies()
enableJavaPerformanceTesting()

description = "Apache Beam :: Examples :: Kotlin"
ext.summary = """Apache Beam SDK provides a simple, Kotlin-based
interface for processing virtually any size data. This
artifact includes all Apache Beam Kotlin SDK examples."""

/** Define the list of runners which execute a precommit test.
 * Some runners are run from separate projects, see the preCommit task below
 * for details.
 */
def preCommitRunners = ["directRunner", "flinkRunner", "sparkRunner"]
for (String runner : preCommitRunners) {
  configurations.create(runner + "PreCommit")
}
configurations.sparkRunnerPreCommit {
  // Ban certain dependencies to prevent a StackOverflow within Spark
  // because JUL -> SLF4J -> JUL, and similarly JDK14 -> SLF4J -> JDK14
  exclude group: "org.slf4j", module: "jul-to-slf4j"
  exclude group: "org.slf4j", module: "slf4j-jdk14"
}

def kotlin_version = "1.6.10"

dependencies {
  implementation enforcedPlatform(library.java.google_cloud_platform_libraries_bom)
  implementation library.java.vendored_guava_32_1_2_jre
  implementation project(path: ":sdks:java:core", configuration: "shadow")
  // Add the dependency that sdks:java:core that is marked as provided
  implementation library.java.hamcrest
  permitUnusedDeclared library.java.hamcrest
  implementation project(":sdks:java:extensions:avro")
  implementation project(":sdks:java:extensions:google-cloud-platform-core")
  implementation project(":sdks:java:io:google-cloud-platform")
  implementation library.java.avro
  implementation library.java.bigdataoss_util
  implementation library.java.google_api_client
  implementation library.java.google_api_services_bigquery
  implementation library.java.google_api_services_pubsub
  implementation library.java.google_auth_library_credentials
  implementation library.java.google_auth_library_oauth2_http
  implementation library.java.google_http_client
  implementation library.java.joda_time
  implementation library.java.slf4j_api
  implementation library.java.guava
  implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
  implementation "org.jetbrains:annotations:13.0"
  runtimeOnly project(path: ":runners:direct-java", configuration: "shadow")
  testImplementation project(":sdks:java:io:google-cloud-platform")
  testImplementation library.java.junit

  // Add dependencies for the PreCommit configurations
  // For each runner a project level dependency on the examples project.
  for (String runner : preCommitRunners) {
    delegate.add(runner + "PreCommit", project(path: ":examples:kotlin", configuration: "testRuntimeMigration"))
  }
  directRunnerPreCommit project(project.path)
  flinkRunnerPreCommit project(project.path)
  sparkRunnerPreCommit project(project.path)
  directRunnerPreCommit project(path: ":runners:direct-java", configuration: "shadow")
  flinkRunnerPreCommit project(":runners:flink:${project.ext.latestFlinkVersion}")
  sparkRunnerPreCommit project(":runners:spark:3")
  sparkRunnerPreCommit project(":sdks:java:io:hadoop-file-system")
}

/*
 * Create a ${runner}PreCommit task for each runner which runs a set
 * of integration tests for WordCount and WindowedWordCount.
 */
def preCommitRunnerClass = [
  directRunner: "org.apache.beam.runners.direct.DirectRunner",
  flinkRunner: "org.apache.beam.runners.flink.TestFlinkRunner",
  sparkRunner: "org.apache.beam.runners.spark.TestSparkRunner",
]
def gcpProject = project.findProperty('gcpProject') ?: 'apache-beam-testing'
def gcsTempRoot = project.findProperty('gcsTempRoot') ?: 'gs://temp-storage-for-end-to-end-tests/'

for (String runner : preCommitRunners) {
  tasks.create(name: runner + "PreCommit", type: Test) {
    def preCommitBeamTestPipelineOptions = [
       "--project=${gcpProject}",
       "--tempRoot=${gcsTempRoot}",
       "--runner=" + preCommitRunnerClass[runner],
    ]
    classpath = configurations."${runner}PreCommit"
    include "**/kotlin/**/*Test.class"
    forkEvery 1
    maxParallelForks 4
    systemProperty "beamTestPipelineOptions", JsonOutput.toJson(preCommitBeamTestPipelineOptions)
  }
}

/* Define a common precommit task which depends on all the individual precommits. */
task preCommit() {
  for (String runner : preCommitRunners) {
    dependsOn runner + "PreCommit"
  }
}

compileKotlin {
    kotlinOptions {
        jvmTarget = project.javaVersion
    }
}
compileTestKotlin {
    kotlinOptions {
        jvmTarget = project.javaVersion
    }
}
repositories {
  mavenCentral()
}
