plugins {
  id("com.github.johnrengelman.shadow") version "6.0.0"
}

Project instr_project = project
subprojects {
  afterEvaluate { Project subProj ->
    if (subProj.getPlugins().hasPlugin('java')) {
      // Make it so all instrumentation subproject tests can be run with a single command.
      instr_project.tasks.test.dependsOn(subProj.tasks.test)

      dependencies {
        compileOnly("io.opentelemetry:opentelemetry-sdk:${versions.opentelemetry}")
        compileOnly("io.opentelemetry.javaagent:opentelemetry-javaagent-api:${versions.opentelemetryJavaagentAlpha}")
        compileOnly("io.opentelemetry.javaagent:opentelemetry-javaagent-tooling:${versions.opentelemetryJavaagentAlpha}")

        compileOnly deps.bytebuddy
        compileOnly deps.bytebuddyagent
        annotationProcessor deps.autoservice
        compileOnly deps.autoservice
      }

      instr_project.dependencies {
        implementation(project(subProj.getPath()))
      }
    }
  }
}

shadowJar {

  mergeServiceFiles()

  exclude '**/module-info.class'

  // Prevents conflict with other SLF4J instances. Important for premain.
  relocate 'org.slf4j', 'io.opentelemetry.javaagent.slf4j'

  duplicatesStrategy = DuplicatesStrategy.FAIL

  // rewrite library instrumentation dependencies
  relocate("io.opentelemetry.instrumentation", "io.opentelemetry.javaagent.shaded.instrumentation") {
    exclude "io.opentelemetry.javaagent.instrumentation.**"
  }

  // rewrite dependencies calling Logger.getLogger
  relocate 'java.util.logging.Logger', 'io.opentelemetry.javaagent.bootstrap.PatchLogger'

  // prevents conflict with library instrumentation
  relocate 'io.opentelemetry.instrumentation.api', 'io.opentelemetry.javaagent.shaded.instrumentation.api'

  // relocate OpenTelemetry API usage
  relocate "io.opentelemetry.api", "io.opentelemetry.javaagent.shaded.io.opentelemetry.api"
  relocate "io.opentelemetry.spi", "io.opentelemetry.javaagent.shaded.io.opentelemetry.spi"
  relocate "io.opentelemetry.context", "io.opentelemetry.javaagent.shaded.io.opentelemetry.context"

  // relocate the OpenTelemetry extensions that are used by instrumentation modules
  // these extensions live in the AgentClassLoader, and are injected into the user's class loader
  // by the instrumentation modules that use them
  relocate "io.opentelemetry.extension.aws", "io.opentelemetry.javaagent.shaded.io.opentelemetry.extension.aws"
  relocate "io.opentelemetry.extension.kotlin", "io.opentelemetry.javaagent.shaded.io.opentelemetry.extension.kotlin"
}