plugins {
  id "java"

  /*
  Instrumentation agent extension mechanism expects a single jar containing everything required
  for your extension. This also includes any external libraries that your extension uses and
  cannot access from application classpath (see comment below about `javax.servlet-api` dependency).

  Thus we use Shadow Gradle plugin to package our classes and all required runtime dependencies
  into a single jar.
  See https://imperceptiblethoughts.com/shadow/ for more details about Shadow plugin.
   */
  id "com.github.johnrengelman.shadow" version "6.1.0"
  id "com.diffplug.spotless" version "6.1.2"

  id "io.opentelemetry.instrumentation.muzzle-generation" version "1.12.0-alpha"
  id "io.opentelemetry.instrumentation.muzzle-check" version "1.12.0-alpha"
}

group 'io.opentelemetry.example'
version '1.0'

ext {
  versions = [
    opentelemetry              : "1.9.1",
    opentelemetryAlpha         : "1.9.1-alpha",
    opentelemetryJavaagent     : "1.12.0",
    opentelemetryJavaagentAlpha: "1.12.0-alpha",
  ]

  deps = [
    autoservice: dependencies.create(group: 'com.google.auto.service', name: 'auto-service', version: '1.0')
  ]
}

repositories {
  mavenCentral()
  maven {
    name = "sonatype"
    url = uri("https://oss.sonatype.org/content/repositories/snapshots")
  }
}

configurations {
  /*
  We create a separate gradle configuration to grab a published Otel instrumentation agent.
  We don't need the agent during development of this extension module.
  This agent is used only during integration test.
  */
  otel
}

spotless {
  java {
    googleJavaFormat()
    licenseHeaderFile(rootProject.file("../../buildscripts/spotless.license.java"), "(package|import|public)")
    target("src/**/*.java")
  }
}

dependencies {
  /*
  Interfaces and SPIs that we implement. We use `compileOnly` dependency because during
  runtime all necessary classes are provided by javaagent itself.
   */
  compileOnly("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure:${versions.opentelemetryAlpha}")
  compileOnly("io.opentelemetry.javaagent:opentelemetry-javaagent-instrumentation-api:${versions.opentelemetryJavaagentAlpha}")
  compileOnly("io.opentelemetry.javaagent:opentelemetry-javaagent-extension-api:${versions.opentelemetryJavaagentAlpha}")

  //Provides @AutoService annotation that makes registration of our SPI implementations much easier
  compileOnly deps.autoservice
  annotationProcessor deps.autoservice

  /*
   Used by our demo instrumentation module to reference classes of the target instrumented library.
   We again use `compileOnly` here because during runtime these classes are provided by the
   actual application that we instrument.

   NB! Only Advice (and "helper") classes of instrumentation modules can access classes from application classpath.
   See https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/main/docs/contributing/writing-instrumentation-module.md#advice-classes
   */
  compileOnly group: 'javax.servlet', name: 'javax.servlet-api', version: '3.0.1'

  /*
  This dependency is required for DemoSpanProcessor both during compile and runtime.
  Only dependencies added to `implementation` configuration will be picked up by Shadow plugin
  and added to the resulting jar for our extension's distribution.
   */
  implementation 'org.apache.commons:commons-lang3:3.11'

  //All dependencies below are only for tests
  testImplementation("org.testcontainers:testcontainers:1.16.2")
  testImplementation("com.fasterxml.jackson.core:jackson-databind:2.11.2")
  testImplementation("com.google.protobuf:protobuf-java-util:3.12.4")
  testImplementation("com.squareup.okhttp3:okhttp:3.12.12")
  testImplementation("io.opentelemetry:opentelemetry-api:${versions.opentelemetry}")
  testImplementation("io.opentelemetry.proto:opentelemetry-proto:0.11.0-alpha")

  testImplementation("org.junit.jupiter:junit-jupiter-api:5.6.2")
  testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.6.2")
  testRuntimeOnly("ch.qos.logback:logback-classic:1.2.3")

  //Otel Java instrumentation that we use and extend during integration tests
  otel("io.opentelemetry.javaagent:opentelemetry-javaagent:${versions.opentelemetryJavaagent}")

  //TODO remove when start using io.opentelemetry.instrumentation.javaagent-instrumentation plugin
  add("codegen", "io.opentelemetry.javaagent:opentelemetry-javaagent-tooling:${versions.opentelemetryJavaagentAlpha}")
  add("codegen", "ch.qos.logback:logback-classic:1.2.3")
  add("muzzleBootstrap", "io.opentelemetry.instrumentation:opentelemetry-instrumentation-api-annotation-support:${versions.opentelemetryJavaagentAlpha}")
  add("muzzleBootstrap", "io.opentelemetry.javaagent:opentelemetry-javaagent-instrumentation-api:${versions.opentelemetryJavaagentAlpha}")
  add("muzzleTooling", "io.opentelemetry.javaagent:opentelemetry-javaagent-extension-api:${versions.opentelemetryJavaagentAlpha}")
  add("muzzleTooling", "io.opentelemetry.javaagent:opentelemetry-javaagent-tooling:${versions.opentelemetryJavaagentAlpha}")
  add("muzzleTooling", "org.slf4j:slf4j-simple:1.7.30")
}

//Produces a copy of upstream javaagent with this extension jar included inside it
//The location of extension directory inside agent jar is hard-coded in the agent source code
task extendedAgent(type: Jar) {
  dependsOn(configurations.otel)
  archiveFileName = "opentelemetry-javaagent.jar"
  from zipTree(configurations.otel.singleFile)
  from(tasks.shadowJar.archiveFile) {
    into "extensions"
  }

  //Preserve MANIFEST.MF file from the upstream javaagent
  doFirst {
    manifest.from(
      zipTree(configurations.otel.singleFile).matching {
        include 'META-INF/MANIFEST.MF'
      }.singleFile
    )
  }
}

tasks {
  test {
    useJUnitPlatform()

    inputs.files(layout.files(tasks.shadowJar))
    inputs.files(layout.files(tasks.extendedAgent))

    systemProperty 'io.opentelemetry.smoketest.agentPath', configurations.otel.singleFile.absolutePath
    systemProperty 'io.opentelemetry.smoketest.extendedAgentPath', tasks.extendedAgent.archiveFile.get().asFile.absolutePath
    systemProperty 'io.opentelemetry.smoketest.extensionPath', tasks.shadowJar.archiveFile.get().asFile.absolutePath
  }

  compileJava {
    options.release.set(8)
  }

  assemble.dependsOn(shadowJar)
}

muzzle {
  pass {
    group.set("javax.servlet")
    module.set("javax.servlet-api")
    versions.set("[3.0,)")
    assertInverse.set(true)
  }
  pass {
    group.set("javax.servlet")
    module.set("servlet-api")
    versions.set("[2.2, 3.0)")
    assertInverse.set(true)
  }
}
