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

apply plugin: 'scala'
apply plugin: 'application'
apply plugin: 'eclipse'
apply plugin: 'maven-publish'
apply plugin: 'org.scoverage'
apply plugin: 'com.lightbend.akka.grpc.gradle'

ext.dockerImageName = 'scheduler'
apply from: '../../gradle/docker.gradle'
distDocker.dependsOn ':common:scala:distDocker', 'distTar'

project.archivesBaseName = "openwhisk-scheduler"

scoverage {
    scoverageVersion.set("${gradle.scala.scoverageVersion}")
    scoverageScalaVersion.set("${gradle.scala.scoverageScalaVersion}")
}

ext.coverageDirs = [
        "${buildDir}/classes/scala/scoverage",
        "${project(':common:scala').buildDir.absolutePath}/classes/scala/scoverage"
]

buildscript {
    repositories {
        mavenLocal()
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }
    dependencies {
        // see https://plugins.gradle.org/plugin/com.lightbend.akka.grpc.gradle
        // for the currently latest version.
        classpath 'com.lightbend.akka.grpc:akka-grpc-gradle-plugin:1.0.2'
    }
}

protobuf {
    protoc {
        if (osdetector.os == "osx") {
            artifact = 'com.google.protobuf:protoc:3.25.5:osx-x86_64'
        } else {
            artifact = 'com.google.protobuf:protoc:3.25.5'
        }
    }
}

// Define a separate configuration for managing the dependency on Jetty ALPN agent.
configurations {
    alpnagent
}

dependencies {
    configurations.all {
        resolutionStrategy.force "com.lihaoyi:fastparse_${gradle.scala.depVersion}:2.3.0"
        resolutionStrategy.force "com.typesafe.akka:akka-http-core_${gradle.scala.depVersion}:${gradle.akka_http.version}"
        resolutionStrategy.force "com.typesafe.akka:akka-http_${gradle.scala.depVersion}:${gradle.akka_http.version}"
        resolutionStrategy.force "com.typesafe.akka:akka-http2-support_${gradle.scala.depVersion}:${gradle.akka_http.version}"
        resolutionStrategy.force "com.typesafe.akka:akka-http-spray-json_${gradle.scala.depVersion}:${gradle.akka_http.version}"
        resolutionStrategy.force "com.typesafe.akka:akka-parsing_${gradle.scala.depVersion}:${gradle.akka_http.version}"
        resolutionStrategy.force "com.typesafe.akka:akka-http_${gradle.scala.depVersion}:${gradle.akka_http.version}"
    }
    implementation "org.scala-lang:scala-library:${gradle.scala.version}"
    implementation project(':common:scala')
    implementation "io.altoo:akka-kryo-serialization_${gradle.scala.depVersion}:1.0.0"
    implementation "com.lightbend.akka.management:akka-management-cluster-bootstrap_${gradle.scala.depVersion}:${gradle.akka_management.version}"
    implementation "com.lightbend.akka.discovery:akka-discovery-kubernetes-api_${gradle.scala.depVersion}:${gradle.akka_management.version}"
}

// workaround for akka-grpc
// https://github.com/akka/akka-grpc/issues/786
printProtocLogs.doFirst {
    mkdir "$buildDir"
    file("$buildDir/akka-grpc-gradle-plugin.log").text = "x"
    mkdir "$project.rootDir/build"
    file("$project.rootDir/build/akka-grpc-gradle-plugin.log").text = "x"
}
printProtocLogs.configure {
    mkdir "$buildDir"
    file("$buildDir/akka-grpc-gradle-plugin.log").text = "x"
    mkdir "$project.rootDir/build"
    file("$project.rootDir/build/akka-grpc-gradle-plugin.log").text = "x"
}

mainClassName = "org.apache.openwhisk.core.scheduler.Scheduler"
applicationDefaultJvmArgs = ["-Djava.security.egd=file:/dev/./urandom"]

// Explictly declare task dependencies
tasks.named("processResources") {
    dependsOn tasks.named("extractProto")
}

tasks.named("processScoverageResources") {
    dependsOn tasks.named("extractScoverageProto")
}

