import java.text.SimpleDateFormat

buildscript {
    repositories {
        jcenter()
        mavenLocal()
        maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
    }

    dependencies {
        classpath 'org.kordamp.basilisk:gradle-basilisk-plugin:0.2.0'
        classpath 'nl.javadude.gradle.plugins:license-gradle-plugin:0.11.0'
        classpath 'org.gradle.api.plugins:gradle-izpack-plugin:0.2.3'
        classpath 'com.github.cr0:gradle-macappbundle-plugin:3.1.0'
        classpath 'org.kordamp.gradle:stats-gradle-plugin:0.2.0'
        classpath 'com.github.ben-manes:gradle-versions-plugin:0.12.0'
        classpath 'org.javafxports:jfxmobile-plugin:1.0.8'
        classpath 'net.nemerosa:versioning:2.1.0'
    }
}

apply plugin: 'groovy'
apply plugin: 'org.kordamp.basilisk'
apply plugin: 'org.javafxports.jfxmobile'
apply plugin: 'net.nemerosa.versioning'

repositories {
    maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
    jcenter()
}

Date buildTimeAndDate = new Date()
ext {
    buildDate = new SimpleDateFormat('yyyy-MM-dd').format(buildTimeAndDate)
    buildTime = new SimpleDateFormat('HH:mm:ss.SSSZ').format(buildTimeAndDate)
    macosx = System.getProperty('os.name').contains('Mac OS')
}

basilisk {
    disableDependencyResolution = false
    version = '${basilisk_version}'
    applicationProperties = [
        'build.date'    : buildDate,
        'build.time'    : buildTime,
        'build.revision': versioning.info.commit
    ]
}

mainClassName = '${project_package}.Launcher'

apply from: 'gradle/publishing.gradle'
apply from: 'gradle/code-coverage.gradle'
apply from: 'gradle/code-quality.gradle'
apply from: 'gradle/integration-test.gradle'
apply from: 'gradle/functional-test.gradle'
apply from: 'gradle/package.gradle'
apply from: 'gradle/docs.gradle'
apply plugin: 'org.kordamp.gradle.stats'
apply plugin: 'com.github.ben-manes.versions'

dependencies {
    compile "org.kordamp.basilisk:basilisk-guice:\${basilisk.version}"

    runtime 'org.slf4j:slf4j-simple:1.7.21'

    testCompile "org.kordamp.basilisk:basilisk-javafx-test:\${basilisk.version}"
    testCompile "org.codehaus.groovy:groovy-all:\${groovyVersion}"
    testCompile "org.spockframework:spock-core:\${spockVersion}"

    functionalTestCompile "org.kordamp.basilisk:basilisk-javafx-test:\${basilisk.version}"

    retrolambdaConfig 'net.orfjackal.retrolambda:retrolambda:2.3.0'
}

compileGroovy.enabled = false

tasks.withType(JavaCompile) {
    sourceCompatibility = project.sourceCompatibility
    targetCompatibility = project.targetCompatibility
}

tasks.withType(GroovyCompile) {
    sourceCompatibility = project.sourceCompatibility
    targetCompatibility = project.targetCompatibility
}

jfxmobile {
    ios {
        forceLinkClasses = ['${project_package}.**.*', 'basilisk.**.*', 'org.kordamp.basilisk.**.*']
    }
}

retrolambda {
    javaVersion JavaVersion.VERSION_1_7
}

// The following task is needed because Retrolambda places META-INF files at
// build/retrolambda/main but they should be at build/classes/main
// hardcoding paths for now
task copyMetainf << {
    copy {
        from project.file("\${buildDir}/retrolambda/main")
        into project.file("\${buildDir}/classes/main")
    }
}

classes.finalizedBy copyMetainf

task jacocoRootMerge(type: org.gradle.testing.jacoco.tasks.JacocoMerge, dependsOn: [test, jacocoTestReport, jacocoIntegrationTestReport, jacocoFunctionalTestReport]) {
    executionData = files(jacocoTestReport.executionData, jacocoIntegrationTestReport.executionData, jacocoFunctionalTestReport.executionData)
    destinationFile = file("\${buildDir}/jacoco/root.exec")
}

task jacocoRootReport(dependsOn: jacocoRootMerge, type: JacocoReport) {
    group = 'Reporting'
    description = 'Generate Jacoco coverage reports after running all tests.'
    executionData file("\${buildDir}/jacoco/root.exec")
    sourceDirectories = files(sourceSets.main.allSource.srcDirs)
    classDirectories = files(sourceSets.main.output)
    reports {
        csv.enabled = false
        xml.enabled = true
        html.enabled = true
        html.destination = "\${buildDir}/reports/jacoco/root/html"
        xml.destination = "\${buildDir}/reports/jacoco/root/root.xml"
    }
}