// this project will run in isolation under the agent's classloader
buildscript {

  repositories {
    mavenLocal()
    mavenCentral()
    jcenter()
  }

  dependencies {
    classpath "net.bytebuddy:byte-buddy-gradle-plugin:${versions.bytebuddy}"
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${versions.kotlin}"
  }
}
plugins {
  id "com.github.johnrengelman.shadow"
}
apply from: "$rootDir/gradle/java.gradle"

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)

      if (subProj.findProperty('packageInAgentBundle')) {
        instr_project.dependencies {
          implementation(project(subProj.getPath()))
        }
      }
    }
  }
}

dependencies {
  compileOnly project(':instrumentation-api')
  compileOnly project(':javaagent-api')
  implementation(project(':javaagent-tooling'))
}

configurations {
  // exclude bootstrap dependencies from shadowJar
  implementation.exclude group: 'org.slf4j'
  implementation.exclude group: 'io.opentelemetry', module: 'opentelemetry-api'
}

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

  dependencies {
    //These classes are added to bootstrap classloader by javaagent module
    exclude(project(':javaagent-bootstrap'))
    exclude(project(':instrumentation-api'))
    exclude(project(':javaagent-api'))
  }

  // rewrite library instrumentation dependencies
  relocate "io.opentelemetry.instrumentation", "io.opentelemetry.javaagent.shaded.instrumentation"

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

  // relocate OpenTelemetry API usage
  relocate "io.opentelemetry.api", "io.opentelemetry.javaagent.shaded.io.opentelemetry.api"
  relocate "io.opentelemetry.semconv", "io.opentelemetry.javaagent.shaded.io.opentelemetry.semconv"
  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"

  // this is for instrumentation on opentelemetry-api itself
  relocate "application.io.opentelemetry", "io.opentelemetry"
}

tasks.register("listInstrumentations") {
  group = "Help"
  description = "List all available instrumentation modules"
  doFirst {
    subprojects
      .findAll { it.plugins.hasPlugin("muzzle") }
      .collect { it.path }
      .each { println it }
  }
}