/*
 * Copyright © 2022 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("com.google.protobuf:protobuf-bom:$protobufVersion")
  api project(":servicetalk-grpc-api")
  api project(":servicetalk-concurrent")
  api project(":servicetalk-concurrent-api")
  api project(":servicetalk-encoding-api")
  api project(":servicetalk-router-api")
  api "com.google.protobuf:protobuf-java"

  implementation project(":servicetalk-annotations")
  implementation project(":servicetalk-data-protobuf")
  implementation project(":servicetalk-grpc-protobuf")
  implementation project(":servicetalk-serializer-api")

  testImplementation enforcedPlatform("org.junit:junit-bom:$junit5Version")
  testImplementation testFixtures(project(":servicetalk-transport-netty-internal"))
  testImplementation testFixtures(project(":servicetalk-concurrent-internal"))
  testImplementation project(":servicetalk-grpc-netty")
  testImplementation project(":servicetalk-transport-api")
  testImplementation "org.junit.jupiter:junit-jupiter-api"
  testImplementation "org.hamcrest:hamcrest:$hamcrestVersion"
}

// Instead of copy/pasting the .proto files into our repository, fetch them from maven central.
// This will also more likely to raise any API changes earlier so we can adjust.
configurations {
  grpcProtos { transitive = false }
}

dependencies {
  grpcProtos "io.grpc:grpc-services:$grpcVersion"
}

processResources {
  duplicatesStrategy = 'include'
}

task unzipGrpcProtos(type: Copy) {
  dependsOn processResources
  from zipTree(configurations.grpcProtos.singleFile).matching {
    include '**/health.proto'
  }
  into "$buildDir/extracted-protos/main"
  includeEmptyDirs = false
  duplicatesStrategy = 'include'
  // Rename the java package name to avoid potential classpath conflicts with grpc-java.
  filter { line -> line.replace(
      'option java_package = "io.grpc.health.v1";',
      'option java_package = "io.servicetalk.health.v1";')
  }
}

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, false)
      task.dependsOn unzipGrpcProtos
      task.dependsOn(":servicetalk-grpc-protoc:buildExecutable") // use gradle project local grpc-protoc dependency

      // you may need to manually add the artifact name as an input
      task.inputs
          .file(pluginJar)
          .withNormalizer(ClasspathNormalizer)
          .withPropertyName("servicetalkPluginJar")
          .withPathSensitivity(PathSensitivity.RELATIVE)
      //// REMOVE if outside of ServiceTalk gradle project

      task.plugins {
        servicetalk_grpc {
          // Need to tell protobuf-gradle-plugin to output in the correct directory if all generated
          // code for a single proto goes to a single file (e.g. "java_multiple_files = false" in the .proto).
          outputSubDir = "java"
        }
      }
    }
  }
}
