//file:noinspection GroovyAssignabilityCheck

plugins {
  id("java-library")
  id("maven-publish")
}

compileJava() {
  getOptions().getRelease().set(17)
  getOptions().setEncoding("UTF-8")
}

dependencies() {
  compileOnly("com.velocitypowered:velocity-api:$velocityVersion")
  api("net.elytrium.commons:config:$elytriumCommonsVersion")
  api("net.elytrium.commons:utils:$elytriumCommonsVersion")
  api("net.elytrium.commons:velocity:$elytriumCommonsVersion")
  api("net.elytrium.commons:kyori:$elytriumCommonsVersion")
  api("net.kyori:adventure-nbt:$adventureVersion")

  compileOnly("com.github.spotbugs:spotbugs-annotations:$spotbugsVersion")
}

license() {
  matching(includes: ["**/mcprotocollib/**"]) {
    header = rootProject.file("HEADER_MCPROTOCOLLIB.txt")
  }

  header = file("HEADER.txt")
}

javadoc() {
  options.setEncoding("UTF-8")
  options.setSource("17")
  options.links("https://docs.oracle.com/en/java/javase/11/docs/api/")
  options.addStringOption("Xdoclint:none", "-quiet")
  if (JavaVersion.current() >= JavaVersion.VERSION_1_9 && JavaVersion.current() < JavaVersion.VERSION_12) {
    options.addBooleanOption("-no-module-directories", true)
  }
}

tasks.register("sourcesJar", Jar) {
  archiveClassifier = "sources"
  from(sourceSets.main.getAllSource())
}

tasks.register("javadocJar", Jar) {
  archiveClassifier = "javadoc"
  from(javadoc)
}

publishing() {
  repositories() {
    maven {
      credentials {
        setUsername(System.getenv("ELYTRIUM_MAVEN_USERNAME"))
        setPassword(System.getenv("ELYTRIUM_MAVEN_PASSWORD"))
      }

      setName("elytrium-repo")
      setUrl("https://maven.elytrium.net/repo/")
    }
  }

  publications.create("publication", MavenPublication) {
    from(components.java)

    artifact(javadocJar)
    artifact(sourcesJar)
  }
}

artifacts() {
  archives(javadocJar)
  archives(sourcesJar)
}

sourceSets.main.java.srcDir(
    getTasks().register("generateTemplates", Copy) {
      task -> {
        String version = getVersion().contains("-") ? "${getVersion()} (git-${getCurrentShortRevision()})" : getVersion()
        task.getInputs().properties("version": version)
        task.from(file("src/main/templates")).into(getLayout().getBuildDirectory().dir("generated/sources/templates"))
        task.expand("version": version)
      }
    }.map {
      it.getOutputs()
    }
)
