/*
 * 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.
 */
dependencies {
  // fabric8
  implementation(libs.kubernetes.client)
  compileOnly(libs.crd.generator.apt)
  annotationProcessor(libs.crd.generator.apt)

  // utils
  implementation(libs.lombok)
  annotationProcessor(libs.lombok)

  // logging
  implementation(libs.log4j.slf4j2.impl)
  implementation(libs.log4j.core)
  implementation(libs.slf4j.api)

  testImplementation(platform(libs.junit.bom))
  testImplementation(libs.junit.jupiter)
  testRuntimeOnly(libs.junit.platform.launcher)
}

test {
  useJUnitPlatform()
}

// Adds additional printer columns to generated yaml
// This requires yq
tasks.register('finalizeGeneratedCRD', Exec) {
  dependsOn jar
  println "Updating PrinterColumns for generated CRD"
  commandLine 'sh', './src/main/resources/printer-columns.sh'
}

// Copy generated yaml to Helm charts
tasks.register('relocateGeneratedCRD', Copy) {
  dependsOn finalizeGeneratedCRD
  from "build/classes/java/main/META-INF/fabric8/"
  include "*.spark.apache.org-v1.yml"
  into "../build-tools/helm/spark-kubernetes-operator/crds"
  rename '(.+).yml', '$1.yaml'
}

tasks.register("assertGeneratedCRDMatchesHelmChart") {
  dependsOn 'finalizeGeneratedCRD'
  description = 'Asserts that the generated CRD yaml matches the staged version in Helm Chart'
  doLast {
    def currentPath = projectDir.absolutePath
    def generatedCRDFileBase = "$currentPath/build/classes/java/main/META-INF/fabric8/"
    def stagedCRDFileBase = "$currentPath/../build-tools/helm/spark-kubernetes-operator/crds/"
    def generatedAppCRD = [
        "yq",
        "e",
        ".spec.versions[0]",
        "${generatedCRDFileBase}sparkapplications.spark.apache.org-v1.yml"
    ].execute().text.trim()
    def generatedClusterCRD = [
        "yq",
        "e",
        ".spec.versions[0]",
        "${generatedCRDFileBase}sparkclusters.spark.apache.org-v1.yml"
    ].execute().text.trim()
    def stagedAppCRD = [
        "yq",
        "e",
        ".spec.versions[-1]",
        "${stagedCRDFileBase}sparkapplications.spark.apache.org-v1.yaml"
    ].execute().text.trim()
    def stagedClusterCRD = [
        "yq",
        "e",
        ".spec.versions[-1]",
        "${stagedCRDFileBase}sparkclusters.spark.apache.org-v1.yaml"
    ].execute().text.trim()
    if (generatedAppCRD != stagedAppCRD || generatedClusterCRD != stagedClusterCRD) {
      throw new GradleException("Generated CRD yaml does not match the staged version in " +
          "Helm Chart, please keep the chart updated.")
    }
  }
}

test.finalizedBy('assertGeneratedCRDMatchesHelmChart')
