/** Common setup for manual instrumentation of libraries and javaagent instrumentation. */


/**
 * We define three dependency configurations to use when adding dependencies to libraries being
 * instrumented.
 *
 * - library: A dependency on the instrumented library. Results in the dependency being added to
 *     compileOnly and testImplementation. If the build is run with -PtestLatestDeps=true, the
 *     version when added to testImplementation will be overridden by `+`, the latest version
 *     possible. For simple libraries without different behavior between versions, it is possible
 *     to have a single dependency on library only.
 *
 * - testLibrary: A dependency on a library for testing. This will usually be used to either
 *     a) use a different version of the library for compilation and testing and b) to add a helper
 *     that is only required for tests (e.g., library-testing artifact). The dependency will be
 *     added to testImplementation and will have a version of `+` when testing latest deps as
 *     described above.
 *
 * - latestDepTestLibrary: A dependency on a library for testing when testing of latest dependency
 *   version is enabled. This dependency will be added as-is to testImplementation, but only if
 *   -PtestLatestDeps=true. The version will not be modified but it will be given highest
 *   precedence. Use this to restrict the latest version dependency from the default `+`, for
 *   example to restrict to just a major version by specifying `2.+`.
 */

ext.testLatestDeps = findProperty('testLatestDeps')
configurations {
  // library is where to define dependencies on the library that is being instrumented. It will not
  // be packaged in the agent but will be available at runtime for tests.
  library {
    canBeResolved = false
    canBeConsumed = false
  }
  testLibrary {
    canBeResolved = false
    canBeConsumed = false
  }
  latestDepTestLibrary {
    canBeResolved = false
    canBeConsumed = false
  }
  [library, testLibrary].each { configuration ->
    // We use whenObjectAdded and copy into the real configurations instead of extension to allow
    // mutating the version for latest dep tests.
    configuration.dependencies.whenObjectAdded {
      def dep = it.copy()
      if (testLatestDeps) {
        dep.version {
          it.require '+'
        }
      }
      testImplementation.dependencies.add(dep)
    }
  }
  if (testLatestDeps) {
    latestDepTestLibrary.dependencies.whenObjectAdded {
      def dep = it.copy()
      def declaredVersion = dep.version
      dep.version {
        it.strictly declaredVersion
      }
      testImplementation.dependencies.add(dep)
    }
  }
  compileOnly.extendsFrom(library)
}

if (testLatestDeps) {
  afterEvaluate {
    def latestDepTest = tasks.findByName('latestDepTest')
    if (latestDepTest) {
      tasks.test.dependsOn latestDepTest
    }
  }
}

if (['javaagent', 'library', 'testing'].contains(projectDir.name)) {
  // We don't use this group anywhere in our config, but we need to make sure it is unique per
  // instrumentation so Gradle doesn't merge projects with same name due to a bug in Gradle.
  // https://github.com/gradle/gradle/issues/847
  // In publish.gradle, we set the maven group, which is what matters, to the correct
  // value.
  group = "io.opentelemetry.${projectDir.parentFile.name}"
}
