/*
 * Copyright © 2020 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: "java"
apply plugin: "com.google.protobuf"
apply from: "../../gradle/idea.gradle"

dependencies {
  implementation project(":servicetalk-annotations")
  implementation project(":servicetalk-grpc-netty")
  implementation project(":servicetalk-grpc-protoc")

  implementation "org.slf4j:slf4j-api:$slf4jVersion"
  runtimeOnly "org.apache.logging.log4j:log4j-slf4j-impl:$log4jVersion"
}

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
      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"
          // Option to append a suffix to type names to avoid naming collisions with other code generation.
          option 'typeNameSuffix=St'
          // Option to tell the compiler to exclude all Deprecated fields, types, and methods from the output
          option 'skipDeprecated=true'
          // Option to generate default throwing service definitions on the service interfaces. This will allow
          // teams to evolve their codebases and not break dependent libraries.
          option 'defaultServiceMethods=true'
        }
      }
    }
  }
}
