//file:noinspection GroovyAssignabilityCheck

plugins() {
  id("java")

  id("checkstyle")
  id("com.github.spotbugs").version("6.0.12").apply(false)
  id("org.cadixdev.licenser").version("0.6.1").apply(false)
}

allprojects() {
  apply(plugin: "checkstyle")
  apply(plugin: "com.github.spotbugs")
  apply(plugin: "org.cadixdev.licenser")

  setGroup("net.elytrium.limboapi")
  setVersion("1.1.27-SNAPSHOT")

  compileJava() {
    sourceCompatibility = JavaVersion.VERSION_17
    targetCompatibility = JavaVersion.VERSION_17
  }

  repositories() {
    mavenCentral()

    maven() {
      setName("elytrium-repo")
      setUrl("https://maven.elytrium.net/repo/")
    }
    maven() {
      setName("papermc-repo")
      setUrl("https://repo.papermc.io/repository/maven-public/")
    }
  }

  checkstyle() {
    toolVersion = "10.12.1"
    configFile = file("$rootDir/config/checkstyle/checkstyle.xml")
    configProperties = ["configDirectory": "$rootDir/config/checkstyle"]
    maxErrors = 0
    maxWarnings = 0
  }

  spotbugs() {
    excludeFilter = file("${this.getRootDir()}/config/spotbugs/suppressions.xml")

    if (this.project != rootProject) {
      reports.register("html") {
        required = true
        outputLocation.value(layout.buildDirectory.file("reports/spotbugs/main/spotbugs.html"))
        stylesheet = "fancy-hist.xsl"
      }
    }
  }
}

String getCurrentShortRevision() {
  OutputStream outputStream = new ByteArrayOutputStream()
  exec {
    if (System.getProperty("os.name").toLowerCase().contains("win")) {
      commandLine("cmd", "/c", "git rev-parse --short HEAD")
    } else {
      commandLine("bash", "-c", "git rev-parse --short HEAD")
    }

    setStandardOutput(outputStream)
  }

  return outputStream.toString().trim()
}
