plugins {
  id 'java'
  id 'idea'
  id 'com.github.johnrengelman.shadow' version '5.2.0'
  id 'com.adarshr.test-logger' version '2.1.0'
}

defaultTasks 'build'

group = 'com.airbnb'
description = 'StatsD Support for Kafka Metrics (kafka version: 0.8 and 0.9)'

repositories {
    mavenCentral()
    maven { url "http://dl.bintray.com/airbnb/jars" }
}

ext {
  defaultKafkaVersion = '0.9.0.1'
  kafkaVersion = project.getProperties().getOrDefault("kafkaVersion", defaultKafkaVersion)
  kafkaVersionsToTest = ['0.8.2.2', '0.9.0.1', '0.10.2.2', '1.1.1', '2.3.1']

  testWithKafkaClient = project.getProperties().getOrDefault("testWithKafkaClient", false)
  kafkaClientVersionsToTest = ['0.9.0.1', '0.10.2.2', '1.1.1', '2.3.1']
}

dependencies {
    compile 'com.indeed:java-dogstatsd-client:2.0.11'

    if (testWithKafkaClient) {
      compileOnly "org.apache.kafka:kafka-clients:${kafkaVersion}"
    } else {
      compileOnly "org.apache.kafka:kafka_2.11:${defaultKafkaVersion}"
    }
    compileOnly 'org.slf4j:slf4j-log4j12:+'

    if (testWithKafkaClient) {
      testCompile "org.apache.kafka:kafka-clients:${kafkaVersion}"
    } else {
      testCompile "org.apache.kafka:kafka_2.11:${kafkaVersion}"
    }
    testCompile 'org.slf4j:slf4j-log4j12:+'
    testCompile 'junit:junit:4.11'
    testCompile 'org.easymock:easymock:3.2'
    testCompile 'org.mockito:mockito-inline:3.5.10'
    testCompile 'com.google.guava:guava:29.0-jre'
}

compileJava {
    options.setDeprecation true
    options.encoding = 'UTF-8'
}

def excludeFromCompile(Task task, Map<String, List<String>> excludeMap) {
  excludeMap.each { path, filesToExclude ->
    filesToExclude.each { fileToExclude ->
      task.exclude(path + fileToExclude + ".java")
    }
  }
}

if (testWithKafkaClient) {
  def srcExcludes = [
    "com/airbnb/kafka/kafka08/": ["**/*"],
    "com/airbnb/metrics/": ["ExcludeMetricPredicate", "MetricNameFormatter", "Parser", "ParserForNoTag", "ParserForTagInMBeanName", "StatsDReporter"]
  ]
  def testExcludes = [
    "com/airbnb/kafka/kafka08/": ["**/*"],
    "com/airbnb/metrics/": ["DimensionTest", "ExcludeMetricPredicateTest", "MetricNameFormatterTest", "ParserTest", "StatsDReporterTest"]
  ]

  excludeFromCompile(compileJava, srcExcludes)
  excludeFromCompile(compileTestJava, testExcludes)
}

configurations {
    // manually excludes some unnecessary dependencies
    compile.exclude module: 'zookeeper'
    compile.exclude module: 'zkclient'
    compile.exclude module: 'javax'
    compile.exclude module: 'jline'
    compile.exclude module: 'jms'
    compile.exclude module: 'jmxri'
    compile.exclude module: 'jmxtools'
    compile.exclude module: 'mail'
}

shadowJar {
    exclude 'META-INF/*.DSA'
    exclude 'META-INF/*.RSA'
    exclude 'META-INF/maven/*'
}

def testKafkaVersionTasks = []

for (kafkaVersion in kafkaVersionsToTest) {
  String kafkaVersionUnderscored = kafkaVersion.replace('.', '_')
  def task = tasks.create(name: "testWithKafkaCore_${kafkaVersionUnderscored}", type: GradleBuild) {
    // Changing `buildName` is a workaround for a Gradle issue when trying to run
    // the aggregate task `testWithAllSupportedKafkaVersions`.
    // See the following issues:
    // 1. https://github.com/gradle/gradle/issues/11301
    // 2. https://github.com/gradle/gradle/issues/12872
    buildName = project.getName() + "-kafka_" + kafkaVersionUnderscored
    buildFile = './build.gradle'
    tasks = ['test']
    startParameter.projectProperties = [kafkaVersion: "${kafkaVersion}"]
    group = 'Verification'
    description = "Runs the unit tests with Kafka core version ${kafkaVersion}"
  }

  if (!testKafkaVersionTasks.isEmpty()) {
    task.mustRunAfter(testKafkaVersionTasks.last())
  }

  testKafkaVersionTasks.add(task)
}

test.doFirst {
  println "Running tests with Kafka version ${kafkaVersion}"
  println "Testing with Kafka clients: ${testWithKafkaClient}"
}

task testWithAllSupportedKafkaCoreVersions {
  group = 'Verification'
  description = "Runs the unit tests with all supported Kafka core versions: ${kafkaVersionsToTest}"
  dependsOn(testKafkaVersionTasks)
}

def testKafkaClientVersionTasks = []

for (kafkaVersion in kafkaClientVersionsToTest) {
  String kafkaVersionUnderscored = kafkaVersion.replace('.', '_')
  def task = tasks.create(name: "testWithKafkaClient_${kafkaVersionUnderscored}", type: GradleBuild) {
    // Changing `buildName` is a workaround for a Gradle issue when trying to run
    // the aggregate task `testWithAllSupportedKafkaVersions`.
    // See the following issues:
    // 1. https://github.com/gradle/gradle/issues/11301
    // 2. https://github.com/gradle/gradle/issues/12872
    buildName = project.getName() + "-kafka-clients_" + kafkaVersionUnderscored
    buildFile = './build.gradle'
    tasks = ['test']
    startParameter.projectProperties = [kafkaVersion: "${kafkaVersion}", testWithKafkaClient: true]
    group = 'Verification'
    description = "Runs the unit tests with Kafka client version ${kafkaVersion}"
  }

  if (!testKafkaClientVersionTasks.isEmpty()) {
    task.mustRunAfter(testKafkaClientVersionTasks.last())
  }

  testKafkaClientVersionTasks.add(task)
}

task testWithAllSupportedKafkaClientVersions {
  group = 'Verification'
  description = "Runs the unit tests with all supported Kafka client versions: ${kafkaClientVersionsToTest}"
  dependsOn(testKafkaClientVersionTasks)
}

task testWithAllSupportedKafkaVersions {
  group = 'Verification'
  description = "Runs the unit tests with all supported Kafka core and client versions: ${kafkaVersionsToTest}"
  dependsOn(testWithAllSupportedKafkaCoreVersions, testWithAllSupportedKafkaClientVersions)
}
