// project plugins
plugins {
  id 'java'
  id 'jacoco'
  id 'com.diffplug.gradle.spotless' version '3.25.0'
  id 'org.openjfx.javafxplugin' version '0.0.8'
  id 'org.beryx.jlink' version '2.16.1'
}

// Jupiter version
version = '3.1'

// dependencies repositories
repositories {
  mavenCentral()
}

// compile and test dependencies
dependencies {
  compile 'commons-io:commons-io:2.6'
  compile 'com.jfoenix:jfoenix:9.0.8'
  compile 'commons-cli:commons-cli:1.4'
  compile files('lib/java-cup-11b-runtime.jar')
  compile 'org.fxmisc.richtext:richtextfx:0.10.2'
  testCompile 'org.junit.jupiter:junit-jupiter-api:5.4.2'
  testRuntime 'org.junit.jupiter:junit-jupiter-engine:5.4.2'
}

// javafx modules
javafx {
  version = '12.0.1'
  modules = ['javafx.base', 'javafx.controls', 'javafx.fxml', 'javafx.graphics']
}

// Jupiter main class
mainClassName = "jupiter/jupiter.Jupiter"

// jlink configuration
jlink {
  imageZip = project.file("${buildDir}/distributions/Jupiter-${version}-${javafx.platform.classifier}.zip")
  options = ['--strip-debug', '--compress', '2', '--no-header-files', '--no-man-pages']
  launcher {
    name = 'jupiter'
    jvmArgs = [
      '--add-exports',
      'javafx.controls/com.sun.javafx.scene.control=com.jfoenix',
      '--add-exports',
      'javafx.controls/com.sun.javafx.scene.control.behavior=com.jfoenix',
      '--add-exports',
      'javafx.graphics/com.sun.javafx.scene=com.jfoenix',
      '--add-exports',
      'javafx.base/com.sun.javafx.event=com.jfoenix'
    ]
    windowsScriptTemplate = file('gradle/jlink/jupiter.bat')
  }
  addExtraDependencies('javafx')
}

// test config
test {
  useJUnitPlatform()
  dependsOn 'cleanTest'
  testLogging {
    events 'passed', 'skipped', 'failed'
  }
}

// jacoco configuration
jacocoTestReport {
  reports {
    xml.enabled true
  }
  afterEvaluate {
    classDirectories.from = files(classDirectories.files.collect {
      fileTree(
        dir: it,
        exclude: [
          // exclude gui package
          'jupiter/gui/*',
          // exclude jlex and jcup classes
          'jupiter/asm/Lexer.class',
          'jupiter/asm/Parser.class',
          'jupiter/asm/Token.class',
          'jupiter/asm/Parser$CUP$Parser$actions.class'
        ])
    })
  }
}

// spotless configuration
spotless {
  java {
    licenseHeaderFile 'gradle/spotless/HEADER'
    importOrder 'java', 'javafx', 'com', 'org', 'jupiter'
    removeUnusedImports()
    trimTrailingWhitespace()
    indentWithSpaces(2)
    endWithNewline()
  }
}

// code coverage task
task codecov {
  dependsOn build, jacocoTestReport
  description "Calls build and then jacocoTestReport"
}

// custom fat jar for development
task devJar(type: Jar) {
  baseName = 'Jupiter-dev'
  manifest {
    attributes (
      'Main-Class': 'jupiter.Jupiter',
      'Implementation-Title': 'Jupiter',
      'Implementation-Version': version
    )
  }
  // create fat jar
  from {
    configurations.runtimeClasspath.collect {
      it.isDirectory() ? it : zipTree(it)
    }
  }
  with jar
}

// launch Jupiter GUI application
task launch(type: JavaExec) {
  group = "Execution"
  description = "Run Jupiter GUI mode"
  classpath = sourceSets.main.runtimeClasspath
  main = "jupiter.Jupiter"
}

// java source and target compatibility
sourceCompatibility = 11
targetCompatibility = 11

// compile java source code in UTF-8
compileJava.options.encoding = 'UTF-8'
