// Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.

import org.jetbrains.java.decompiler.build.JasmCompile

plugins {
  id 'org.jetbrains.kotlin.jvm' version '1.5.21'
}

apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'maven-publish'

compileJava {
  sourceCompatibility '1.8'
  targetCompatibility '1.8'
}

group = 'org.quiltmc'
archivesBaseName = 'quiltflower'

version = '1.6.0'

def ENV = System.getenv()
version = version + (ENV.GITHUB_ACTIONS ? "" : "+local")

sourceSets {
  main.java.srcDirs 'src'
  test.java.srcDirs 'test'
  // We need specific java9+ versions of certain classes, so we have a separate source set for them
  java9.java.srcDirs 'java9'
  testDataGroovy.groovy.srcDirs files("testData/src/groovy/")
  testDataKotlin.kotlin.srcDirs files("testData/src/kt/")
}

repositories { mavenCentral() }

dependencies {
  implementation 'org.jetbrains.kotlinx:kotlinx-metadata-jvm:0.3.0'

  testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.2'
  testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.2'
  testImplementation 'org.hamcrest:hamcrest:2.2'

  // Java 9 classes need the main source set
  java9Implementation sourceSets.main.output
  testDataGroovyImplementation 'org.codehaus.groovy:groovy:3.0.8'
  testDataKotlinImplementation platform('org.jetbrains.kotlin:kotlin-bom')
  testDataKotlinImplementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
}

test {
  useJUnitPlatform()
  systemProperty "DOT_EXPORT_DIR", System.getProperty("DOT_EXPORT_DIR", null)
  systemProperty "DOT_ERROR_EXPORT_DIR", System.getProperty("DOT_ERROR_EXPORT_DIR", null)
}

java.toolchain {
  languageVersion = JavaLanguageVersion.of(8)
}

tasks.withType(JavaCompile) {
  options.deprecation = true
}

task testDataClasses {
  group = 'build'
}
testClasses.dependsOn(testDataClasses)

[8, 9, 11, 16].forEach {version ->
  def sourceSet = sourceSets.create("testDataJava${version}") {
    it.java.srcDirs file("testData/src/java${version}")
  }
  def task = tasks.getByName("compileTestDataJava${version}Java") {
    destinationDir = file("testData/classes/java${version}")
    javaCompiler = javaToolchains.compilerFor {
      languageVersion = JavaLanguageVersion.of(version)
    }
  }
  testDataClasses.dependsOn("testDataJava${version}Classes")
}

task compileTestDataJasm(type: JasmCompile) {
  source = fileTree("testData/src/jasm/")
  destinationDir = file("testData/classes/jasm/")
  options.compilerArgs += ["-g"]
}
task testDataJasmClasses {
  group = 'build'
}
testDataJasmClasses.dependsOn(compileTestDataJasm)
testDataClasses.dependsOn(testDataJasmClasses)

compileTestDataGroovyGroovy {
  destinationDir = file("testData/classes/groovy")
}
testDataClasses.dependsOn(testDataGroovyClasses)

compileTestDataKotlinKotlin {
  destinationDir = file("testData/classes/kt")
}
testDataClasses.dependsOn(testDataKotlinClasses)

// Compile the java 9 classes
compileJava9Java {
  javaCompiler = javaToolchains.compilerFor {
    languageVersion = JavaLanguageVersion.of(9)
  }
}

jar {
  // put the java 9 source set into the jar to make a multi-release jar
  into('META-INF/versions/9') {
    from sourceSets.java9.output
  }

  manifest {
    attributes (
      'Multi-Release': 'true',
      'Main-Class': 'org.jetbrains.java.decompiler.main.decompiler.ConsoleDecompiler'
    )
  }
}

task sourceJar(type:Jar) {
  classifier = "sources"
  from sourceSets.main.allSource
}

tasks.withType(JavaCompile).configureEach {
  it.options.encoding = "UTF-8"
}

publishing {
  publications {
    mavenJava(MavenPublication) {
      from components.java
      artifact(sourceJar)
    }
  }

  // select the repositories you want to publish to
  repositories {
    if (ENV.MAVEN_URL) {
      maven {
        url ENV.MAVEN_URL
        credentials {
          username ENV.MAVEN_USERNAME
          password ENV.MAVEN_PASSWORD
        }
      }
    }
  }
}
