/*
 * 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.
 */

/** Define the list of runners which execute a precommit test.
 * Some runners are run from separate projects, see the preCommit task below
 * for details.
 */
project.ext.preCommitRunners = ["directRunner", "flinkRunner", "sparkRunner"]
// The following runners have configuration created but not added to preCommit
project.ext.nonPreCommitRunners = ["dataflowRunner", "prismRunner"]
for (String runner : ext.preCommitRunners) {
  configurations.create(runner + "PreCommit")
}
for (String runner: ext.nonPreCommitRunners) {
  configurations.create(runner + "PreCommit")
}
configurations.sparkRunnerPreCommit {
  // Ban certain dependencies to prevent a StackOverflow within Spark
  // because JUL -> SLF4J -> JUL, and similarly JDK14 -> SLF4J -> JDK14
  exclude group: "org.slf4j", module: "jul-to-slf4j"
  exclude group: "org.slf4j", module: "slf4j-jdk14"
}

dependencies {
  directRunnerPreCommit project(path: ":runners:direct-java", configuration: "shadow")
  flinkRunnerPreCommit project(":runners:flink:${project.ext.latestFlinkVersion}")
  sparkRunnerPreCommit project(":runners:spark:3")
  sparkRunnerPreCommit project(":sdks:java:io:hadoop-file-system")
  dataflowRunnerPreCommit project(":runners:google-cloud-dataflow-java")
  dataflowRunnerPreCommit project(":runners:google-cloud-dataflow-java:worker") // v1 worker
  dataflowRunnerPreCommit project(":sdks:java:harness") // v2 worker
  prismRunnerPreCommit project(":runners:prism:java")
}

 /*
 * A convenient task to run individual example directly on Beam repo.
 *
 * Usage:
 * ./gradlew :examples:java:execute -PmainClass=org.apache.beam.examples.<ClassName>`\
 *  -Pexec.args="runner=[DataflowRunner|DirectRunner|FlinkRunner|SparkRunner|PrismRunner] \
 * <pipeline options>"
 */
tasks.create(name:"execute", type:JavaExec) {
  mainClass = project.hasProperty("mainClass") ? project.getProperty("mainClass") : "NONE"
  def execArgs =  project.findProperty("exec.args")
  String runner
  if (execArgs) {
    // configure runner dependency from args
    def runnerPattern = /runner[ =]([A-Za-z]+)/
    def matcher = execArgs =~ runnerPattern
    if (matcher) {
      runner = matcher[0][1]
      runner = runner.substring(0, 1).toLowerCase() + runner.substring(1);
      if (!(runner in (preCommitRunners + nonPreCommitRunners))) {
        throw new GradleException("Unsupported runner: " + runner)
      }
    }
  }
  if (runner) {
    classpath = sourceSets.main.runtimeClasspath + configurations."${runner}PreCommit"
  } else {
    classpath = sourceSets.main.runtimeClasspath
  }
  systemProperties System.getProperties()
  args execArgs ? execArgs.split() : []
}
