/*
 * Copyright © 2018-2019, 2021 Apple Inc. and the ServiceTalk project authors
 *
 * 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.
 */

buildscript {
  dependencies {
    classpath "com.google.protobuf:protobuf-gradle-plugin:$protobufGradlePluginVersion"
  }
}

apply plugin: "io.servicetalk.servicetalk-gradle-plugin-internal-library"
apply plugin: "com.google.protobuf"

dependencies {
  api platform("io.opentelemetry:opentelemetry-bom:$opentelemetryVersion")
  api project(":servicetalk-http-api")
  api "io.opentelemetry:opentelemetry-api"

  implementation platform("io.opentelemetry.instrumentation:opentelemetry-instrumentation-bom:" +
      "$opentelemetryInstrumentationVersion")
  implementation project(":servicetalk-annotations")
  implementation project(":servicetalk-buffer-api")
  implementation project(":servicetalk-concurrent")
  implementation project(":servicetalk-concurrent-api")
  implementation project(":servicetalk-concurrent-api-internal")
  implementation project(":servicetalk-http-utils")
  implementation project(":servicetalk-transport-api")
  implementation "io.opentelemetry.instrumentation:opentelemetry-instrumentation-api"
  implementation "io.opentelemetry:opentelemetry-context"

  testImplementation enforcedPlatform("com.fasterxml.jackson:jackson-bom:$jacksonVersion")
  testImplementation enforcedPlatform("com.google.protobuf:protobuf-bom:$protobufVersion")
  testImplementation enforcedPlatform("org.junit:junit-bom:$junit5Version")
  testImplementation testFixtures(project(":servicetalk-concurrent-internal"))
  testImplementation testFixtures(project(":servicetalk-http-netty"))
  testImplementation testFixtures(project(":servicetalk-log4j2-mdc-utils"))
  testImplementation testFixtures(project(":servicetalk-transport-netty-internal"))
  testImplementation project(":servicetalk-data-protobuf")
  testImplementation project(":servicetalk-encoding-api")
  testImplementation project(":servicetalk-grpc-api")
  testImplementation project(":servicetalk-grpc-netty")
  testImplementation project(":servicetalk-client-api")
  testImplementation project(":servicetalk-data-jackson")
  testImplementation project(":servicetalk-http-netty")
  testImplementation project(":servicetalk-router-api")
  testImplementation project(":servicetalk-serializer-api")
  testImplementation project(":servicetalk-test-resources")
  testImplementation project(":servicetalk-utils-internal")
  testImplementation "com.fasterxml.jackson.core:jackson-annotations"
  testImplementation "com.fasterxml.jackson.core:jackson-databind"
  testImplementation "com.google.protobuf:protobuf-java"
  testImplementation "io.opentelemetry:opentelemetry-sdk-common"
  testImplementation "io.opentelemetry:opentelemetry-sdk-trace"
  testImplementation "io.opentelemetry:opentelemetry-sdk-testing"
  testImplementation "io.opentelemetry.semconv:opentelemetry-semconv"
  testImplementation "org.hamcrest:hamcrest:$hamcrestVersion"
  testImplementation "org.junit.jupiter:junit-jupiter-api"
  testImplementation "org.junit.jupiter:junit-jupiter-params"
  testImplementation "org.assertj:assertj-core:$assertJCoreVersion"
  testImplementation "org.mockito:mockito-core:$mockitoCoreVersion"
  testImplementation "org.mockito:mockito-junit-jupiter:$mockitoCoreVersion"
  testImplementation "org.slf4j:slf4j-api:$slf4jVersion"

  testRuntimeOnly project(":servicetalk-opentelemetry-asynccontext")
  testRuntimeOnly "io.opentelemetry.instrumentation:opentelemetry-log4j-context-data-2.17-autoconfigure:" +
          "$opentelemetryInstrumentationVersion-alpha"
}

protobuf {
  protoc {
    artifact = "com.google.protobuf:protoc:$protobufVersion"
  }

  //// REMOVE if outside of ServiceTalk gradle project
  def pluginJar = file("${project.rootProject.rootDir}/servicetalk-grpc-protoc/build" +
          "/buildExecutable/servicetalk-grpc-protoc-${project.version}-all.jar")
  //// REMOVE if outside of ServiceTalk gradle project

  plugins {
    servicetalk_grpc {
      //// REMOVE if outside of ServiceTalk gradle project - use "artifact" as demonstrated below
      //// "path" is used only because we want to use the gradle project local version of the plugin.
      path = pluginJar.path
      //// REMOVE if outside of ServiceTalk gradle project - use "artifact" as demonstrated below

      // artifact = "io.servicetalk:servicetalk-grpc-protoc:$serviceTalkVersion:all@jar"
    }
  }
  generateProtoTasks {
    all().each { task ->
      //// REMOVE if outside of ServiceTalk gradle project
      io.servicetalk.gradle.plugin.internal.ProjectUtils.addGeneratedProtoDependsOn(project, task, true)
      if (task.isTest) {
        task.dependsOn(":servicetalk-grpc-protoc:buildExecutable") // use gradle project local grpc-protoc dependency

        task.inputs
                .file(pluginJar)
                .withNormalizer(ClasspathNormalizer)
                .withPropertyName("servicetalkPluginJar")
                .withPathSensitivity(PathSensitivity.RELATIVE)
      }
      //// REMOVE if outside of ServiceTalk gradle project

      task.plugins {
        servicetalk_grpc {
          outputSubDir = "java"
          option "skipDeprecated=true"
        }
      }
    }
  }
}
