/*
 * Copyright 2023 The Data Transfer 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
 *
 * https://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.
 */

apply plugin: 'com.google.protobuf'

sourceSets {
  main {
    proto {
      srcDir 'src/main/proto'
    }
  }
}

protobuf {
  generatedFilesBaseDir = "$projectDir/src/$protobufOutDir"
  protoc {
    if (osdetector.os == 'osx') {
      artifact = "com.google.protobuf:protoc:${protobufVersion}:osx-x86_64"
    } else {
      artifact = "com.google.protobuf:protoc:${protobufVersion}"
    }
  }
}

idea {
  module {
    generatedSourceDirs += file("${protobuf.generatedFilesBaseDir}/main/java");
  }
}

dependencies {
  compile "com.google.protobuf:protobuf-java:$protobufVersion"
}

configurations.all {
  // This enforces our requested version of the protobuf library, and handily disables proto 2
  // since that has the same package name.
  resolutionStrategy.eachDependency { DependencyResolveDetails details ->
    if (details.requested.group == 'com.google.protobuf' && details.requested.name == 'protobuf-java') {
      details.useTarget "com.google.protobuf:protobuf-java:${protobufVersion}"
    }
    if (details.requested.group == 'com.google.protobuf' && details.requested.name == 'protobuf-java-util') {
      details.useTarget "com.google.protobuf:protobuf-java-util:${protobufVersion}"
    }
  }
}

task compileProto {

  outputs.dir file(protobuf.generatedFilesBaseDir)
  doLast {
    file(protobuf.generatedFilesBaseDir).mkdirs()
  }
}
compileJava.dependsOn compileProto
clean.dependsOn cleanCompileProto
