// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you 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.

// This file contains the common protobuf plugin configuration.
import org.gradle.plugins.ide.eclipse.model.SourceFolder
apply plugin: "com.google.protobuf"

// Use the maven protoc artifact instead of local.
protobuf {
  protoc {
    // If -PuseKuduProtoc is set, use the protoc from Kudu's thirdparty instead
    // of using the protoc Maven artifacts. This is useful if a specific
    // OS or architecture is not published to Maven.
    if (propertyExists("useKuduProtoc")) {
      logger.warn("Using Protoc from: $rootDir/../thirdparty/installed/uninstrumented/bin/protoc")
      path = "$rootDir/../thirdparty/installed/uninstrumented/bin/protoc"
    } else {
      artifact = libs.protoc
    }
  }
  generateProtoTasks {
    all().each { task ->
      task.builtins {
        java {
          // Tell protoc to mark generated java classes with @Generated.
          option 'annotate_code'
        }
      }
    }
  }
}

// Configure Intellij to see the generated classes.
idea {
  module {
    generatedSourceDirs += file("${protobuf.generatedFilesBaseDir}/main/java")
  }
}

// Configure Eclipse to see the generated classes.
eclipse {
  classpath {
    file {
      whenMerged {
         // protobuf.generatedFilesBaseDir contains absolute path to the root project.
         // Here we have to make the path relative to a subproject,
         // because Buildship ignores absolute paths when it evaluates classpath entries.

         // Passing 'null' as a second argument to SourceFolder constructor
         // instructs Eclipse to not compile generated source files
         def relPath = projectDir.toPath().relativize(
           file("${protobuf.generatedFilesBaseDir}/main/java").toPath())
         entries += new SourceFolder(relPath.toString(), null)
      }
    }
  }
}
