/*
 * Copyright © 2018-2019, 2022 Apple Inc. and the ServiceTalk 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
 *
 *   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: "io.servicetalk.servicetalk-gradle-plugin-internal-core"
apply plugin: "java-platform"

description = "ServiceTalk BOM that includes all modules and direct dependencies"

javaPlatform {
  allowDependencies()
}

dependencies {
  api platform(project(":servicetalk-bom"))
  api platform("com.fasterxml.jackson:jackson-bom:$jacksonVersion")
  api platform("com.google.protobuf:protobuf-bom:$protobufVersion")
  api platform("io.netty:netty-bom:$nettyVersion")
  api platform("io.opentelemetry:opentelemetry-bom:$opentelemetryVersion")
  api platform("io.opentelemetry.instrumentation:opentelemetry-instrumentation-bom:" +
      "$opentelemetryInstrumentationVersion")
  api platform("io.opentelemetry.instrumentation:opentelemetry-instrumentation-bom-alpha:" +
      "$opentelemetryInstrumentationVersion-alpha")
  api platform("org.apache.logging.log4j:log4j-bom:$log4jVersion")
  api platform("org.glassfish.jersey:jersey-bom:$jerseyVersion")

  constraints {
    api "com.google.api.grpc:proto-google-common-protos:$protoGoogleCommonProtosVersion"
    api "com.google.code.findbugs:jsr305:$jsr305Version"
    api "com.sun.activation:jakarta.activation:$javaxActivationVersion"
    api "com.sun.xml.bind:jaxb-core:$javaxJaxbCoreVersion"

    // Ideally these come from the Netty BOM
    api "io.netty.incubator:netty-incubator-transport-native-io_uring:$nettyIoUringVersion"

    api "io.github.resilience4j:resilience4j-circuitbreaker:$resilience4jVersion"
    api "io.opentracing:opentracing-api:$openTracingVersion"
    api "io.zipkin.reporter2:zipkin-reporter:$zipkinReporterVersion"
    api "jakarta.ws.rs:jakarta.ws.rs-api:$jaxRsVersion"
    api "jakarta.xml.bind:jakarta.xml.bind-api:$javaxJaxbApiVersion"
    api "org.hamcrest:hamcrest:$hamcrestVersion"  // Matchers are exposed by test-resources & some test-fixtures
    api "org.jctools:jctools-core:$jcToolsVersion"
    api "org.openjdk.jmh:jmh-core:$jmhCoreVersion"
    api "org.reactivestreams:reactive-streams:$reactiveStreamsVersion"
    api "org.slf4j:slf4j-api:$slf4jVersion"

    // Use `runtime` for dependencies which are used ONLY at runtime
    api "com.sun.xml.bind:jaxb-impl:$javaxJaxbImplVersion"
  }
}

// Keep publishing and signing configuration in sync with ServiceTalkLibraryPlugin.groovy from
// servicetalk-gradle-plugin-internal
publishing {
  publications {
    mavenJava(MavenPublication) {
      // publish POM
      from components.javaPlatform
      pom {
        name = project.name
        description = project.description
        url = 'https://servicetalk.io'
        licenses {
          license {
            name = 'The Apache License, Version 2.0'
            url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
          }
        }
        developers {
          developer {
            id = 'servicetalk-project-authors'
            name = 'ServiceTalk project authors'
            email = 'servicetalk-oss@group.apple.com'
          }
        }
        scm {
          connection = "scm:git:git://${scmHost}/${scmPath}.git"
          developerConnection = "scm:git:ssh://${scmHost}:${scmPath}.git"
          url = "https://${scmHost}/${scmPath}"
        }
        issueManagement {
          system = 'ServiceTalk Issues'
          url = "${issueManagementUrl}"
        }
        ciManagement {
          system = 'ServiceTalk CI'
          url = "${ciManagementUrl}"
        }
        // Gradle ignores dependencies constraints with classifiers when it generates pom.xml. To workaround it, we are
        // forced to use withXml. See https://github.com/gradle/gradle/issues/8561
        withXml {
          Node pomNode = asNode()
          Node dependencies = pomNode.dependencyManagement.dependencies.get(0)
          addDependency(dependencies, "io.netty.incubator", "netty-incubator-transport-native-io_uring",
              "${nettyIoUringVersion}", "linux-x86_64")
          addDependency(dependencies, "io.netty.incubator", "netty-incubator-transport-native-io_uring",
              "${nettyIoUringVersion}", "linux-aarch_64")
        }
      }
    }
  }

  if (!repositories) {
    repositories {
      maven {
        name = "sonatype"
        def releasesRepoUrl = "https://ossrh-staging-api.central.sonatype.com/service/local/staging/deploy/maven2/"
        def snapshotsRepoUrl = "https://central.sonatype.com/repository/maven-snapshots/"
        url = project.isReleaseBuild ? releasesRepoUrl : snapshotsRepoUrl
        credentials {
          username = System.getenv("SONATYPE_USER")
          password = System.getenv("SONATYPE_TOKEN")
        }
      }
    }
  }
}

if (!!findProperty("signingKey") && !!findProperty("signingPassword")) {
  pluginManager.apply("signing")
  signing {
    def signingKey = findProperty("signingKey")
    def signingPassword = findProperty("signingPassword")
    useInMemoryPgpKeys(signingKey, signingPassword)
    sign publishing.publications.mavenJava
  }
}

def addDependency(Node dependencies, String groupId, String artifactId, String version, String classifier) {
  Node dependency = dependencies.appendNode("dependency")
  dependency.appendNode("groupId", groupId)
  dependency.appendNode("artifactId", artifactId)
  dependency.appendNode("version", version)
  dependency.appendNode("classifier", classifier)
}
