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.gradleup.shadow" version "8.3.8"
  id "com.diffplug.spotless" version "7.2.0"

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

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

ext {
  versions = [
    // this line is managed by .github/scripts/update-sdk-version.sh
    opentelemetrySdk           : "1.52.0",

    // these lines are managed by .github/scripts/update-version.sh
    opentelemetryJavaagent     : "2.19.0-SNAPSHOT",
    opentelemetryJavaagentAlpha: "2.19.0-alpha-SNAPSHOT"
  ]

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

repositories {
  mavenCentral()
  maven {
    name = "sonatype"
    url = uri("https://central.sonatype.com/repository/maven-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 {
  implementation(platform("io.opentelemetry:opentelemetry-bom:${versions.opentelemetrySdk}"))

  // these serve as a test of the instrumentation boms
  implementation(platform("io.opentelemetry.instrumentation:opentelemetry-instrumentation-bom:${versions.opentelemetryJavaagent}"))
  implementation(platform("io.opentelemetry.instrumentation:opentelemetry-instrumentation-bom-alpha:${versions.opentelemetryJavaagentAlpha}"))

  /*
  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-spi")
  compileOnly("io.opentelemetry.instrumentation:opentelemetry-instrumentation-api")
  compileOnly("io.opentelemetry.javaagent:opentelemetry-javaagent-extension-api")

  //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.18.0'

  //All dependencies below are only for tests
  testImplementation("org.testcontainers:testcontainers:1.21.3")
  testImplementation("com.fasterxml.jackson.core:jackson-databind:2.19.1")
  testImplementation("com.google.protobuf:protobuf-java-util:4.31.1")
  testImplementation("com.squareup.okhttp3:okhttp:5.1.0")
  testImplementation("io.opentelemetry:opentelemetry-api")
  testImplementation("io.opentelemetry.proto:opentelemetry-proto:1.7.0-alpha")

  testImplementation(enforcedPlatform("org.junit:junit-bom:5.13.3"))
  testImplementation("org.junit.jupiter:junit-jupiter-api")
  testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
  testRuntimeOnly("org.junit.platform:junit-platform-launcher")

  testRuntimeOnly("ch.qos.logback:logback-classic:1.5.18")

  //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("muzzleBootstrap", "io.opentelemetry.instrumentation:opentelemetry-instrumentation-annotations-support:${versions.opentelemetryJavaagentAlpha}")
  add("muzzleTooling", "io.opentelemetry.javaagent:opentelemetry-javaagent-extension-api:${versions.opentelemetryJavaagentAlpha}")
  add("muzzleTooling", "io.opentelemetry.javaagent:opentelemetry-javaagent-tooling:${versions.opentelemetryJavaagentAlpha}")
}

//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)
  }
}
