import net.ltgt.gradle.errorprone.errorprone
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
  `java-library`
  id("io.sentry.javadoc")
  alias(libs.plugins.kotlin.jvm)
  alias(libs.plugins.errorprone)
  alias(libs.plugins.gradle.versions)
  alias(libs.plugins.buildconfig)
  id("io.sentry.animalsniffer.android")
}

tasks.withType<KotlinCompile>().configureEach {
  compilerOptions.jvmTarget = org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_1_8
}

dependencies {
  errorprone(libs.errorprone.core)
  errorprone(libs.nopen.checker)
  errorprone(libs.nullaway)
  compileOnly(libs.jetbrains.annotations)
  compileOnly(libs.nopen.annotations)
  // tests
  testImplementation(kotlin(Config.kotlinStdLib))
  testImplementation(libs.awaitility.kotlin)
  testImplementation(libs.google.truth)
  testImplementation(libs.javafaker)
  testImplementation(libs.kotlin.test.junit)
  testImplementation(libs.mockito.kotlin)
  testImplementation(libs.mockito.inline)
  testImplementation(libs.msgpack)
  testImplementation(libs.okio)
  testImplementation(projects.sentryTestSupport)
}

sentryAnimalSniffer {
  // We manually check on Android if it's available (API 26+).
  ignoreClasses("java.time.Instant")
  // Uses java.util.function.Supplier, but must be manually invoked.
  mainExcludes("**/io/sentry/SentryWrapper.class")
}

tasks {
  test {
    // java.lang open is needed by tests that reflectively rewrite Class names; it was previously
    // provided implicitly by the jacoco test agent, which has been removed.
    jvmArgs(
      "--add-opens",
      "java.base/java.util.concurrent=ALL-UNNAMED",
      "--add-opens",
      "java.base/java.lang=ALL-UNNAMED",
    )
    environment["SENTRY_TEST_PROPERTY"] = "\"some-value\""
    environment["SENTRY_TEST_MAP_KEY1"] = "\"value1\""
    environment["SENTRY_TEST_MAP_KEY2"] = "value2"
  }
}

buildConfig {
  useJavaOutput()
  packageName("io.sentry")
  buildConfigField("String", "SENTRY_JAVA_SDK_NAME", "\"${Config.Sentry.SENTRY_JAVA_SDK_NAME}\"")
  buildConfigField("String", "VERSION_NAME", "\"${project.version}\"")
}

tasks.withType<JavaCompile>().configureEach {
  dependsOn(tasks.generateBuildConfig)
  options.errorprone {
    check("NullAway", net.ltgt.gradle.errorprone.CheckSeverity.ERROR)
    option("NullAway:AnnotatedPackages", "io.sentry")
  }
  options.errorprone.errorproneArgs.add("-XepExcludedPaths:.*/io/sentry/vendor/.*")
}

tasks.jar {
  from(rootProject.file("THIRD_PARTY_NOTICES.md")) {
    into("META-INF")
    rename { "SENTRY_THIRD_PARTY_NOTICES.md" }
  }
  manifest {
    attributes(
      "Sentry-Version-Name" to project.version,
      "Sentry-SDK-Name" to Config.Sentry.SENTRY_JAVA_SDK_NAME,
      "Sentry-SDK-Package-Name" to "maven:io.sentry:sentry",
      "Implementation-Vendor" to "Sentry",
      "Implementation-Title" to project.name,
      "Implementation-Version" to project.version,
    )
  }
}
