buildscript {
    repositories {
        jcenter()
        mavenLocal()
        maven {
            url "https://plugins.gradle.org/m2"
        }
    }
    
    dependencies {
        classpath 'com.github.jengelman.gradle.plugins:shadow:5.2.0'
    }
}

plugins {
    id  'application'
    id  "com.github.johnrengelman.shadow" version "5.2.0"
}

repositories {
    jcenter()
}

sourceSets {
    main {
        java {
            srcDir 'src/main/java'
        }
        groovy {
            srcDir 'griffon-app/conf'
            srcDir 'griffon-app/controllers'
            srcDir 'griffon-app/lifecycle'
            srcDir 'griffon-app/models'
            srcDir 'griffon-app/resources'
            srcDir 'griffon-app/services'
            srcDir 'griffon-app/views'
        }
        resources {
            srcDir 'griffon-app/i18n'
            srcDir 'griffon-app/resources'
        }
    }
    test {
        java {
            srcDir 'src/test/java'
        }
    }
}

application {
    mainClassName = 'com.muwire.gui.Launcher'
    applicationName = 'MuWire'
}

run {
    applicationDefaultJvmArgs=[]
}

startScripts.doFirst {
    application.applicationDefaultJvmArgs = ["-Djava.util.logging.config.file=logging.properties",
                                    "-Xms256M",
                                    "-XX:+HeapDumpOnOutOfMemoryError",
                                    "-XX:+ExitOnOutOfMemoryError",
                                    "--add-opens", "java.base/java.lang=ALL-UNNAMED",
                                    "--add-opens", "java.base/sun.nio.fs=ALL-UNNAMED",
                                    "--add-opens", "java.base/java.nio=ALL-UNNAMED",
                                    "--add-opens", "java.desktop/java.awt=ALL-UNNAMED",
                                    "--add-opens", "java.desktop/javax.swing=ALL-UNNAMED",
                                    "--add-opens", "java.desktop/javax.swing.plaf.basic=ALL-UNNAMED",
                                    "--add-opens", "java.desktop/sun.swing=ALL-UNNAMED"]
                                   
}



configurations.all {
        exclude group:'org.codehaus.groovy', module:'groovy-test'
        exclude group:'org.codehaus.groovy', module:'groovy-testng'
        exclude group:'org.codehaus.groovy', module:'groovy-test-junit5'
        exclude group:'org.codehaus.groovy', module:'groovy-ant'
        exclude group:'org.codehaus.groovy', module:'groovy-nio'
        exclude group:'org.codehaus.groovy', module:'groovy-servlet'
        exclude group:'org.codehaus.groovy', module:'groovy-jmx'
        exclude group:'org.codehaus.groovy', module:'groovy-groovydoc'
        exclude group:'org.codehaus.groovy', module:'groovy-groovysh'
        exclude group:'org.codehaus.groovy', module:'groovy-xml'
        exclude group:'org.codehaus.groovy', module:'groovy-docgenerator'
        // TODO: add more as discovered
}

dependencies {
    implementation project(":core")
    implementation "org.codehaus.griffon:griffon-swing-groovy:${griffonVersion}"
    implementation "org.codehaus.griffon:griffon-groovy:${griffonVersion}"
    implementation "org.codehaus.griffon:griffon-groovy-compile:${griffonVersion}"
    implementation "org.codehaus.griffon:griffon-swing:${griffonVersion}"
    implementation "org.codehaus.griffon:griffon-guice:${griffonVersion}"
    implementation "org.codehaus.griffon:griffon-core:${griffonVersion}"
    implementation "org.codehaus.griffon:griffon-core-compile:${griffonVersion}"

    implementation "org.codehaus.groovy:groovy-all:${groovyVersion}"
    implementation group: 'com.bulenkov', name: 'darcula', version: '2018.2' 

    // runtime "org.slf4j:slf4j-simple:${slf4jVersion}"

    runtimeOnly group: 'org.slf4j', name: 'slf4j-jdk14', version: "${slf4jVersion}"
    runtimeOnly group: 'org.slf4j', name: 'slf4j-api', version: "${slf4jVersion}"    
    runtimeOnly group: 'org.slf4j', name: 'jul-to-slf4j', version: "${slf4jVersion}"
    runtimeOnly "javax.annotation:javax.annotation-api:1.3.2"

    // because java 14 doesn't come with it
    runtimeOnly 'mrj:MRJToolkitStubs:1.0'

    testCompileOnly "org.codehaus.griffon:griffon-fest-test:${griffonVersion}"
    testCompileOnly "org.spockframework:spock-core:${spockVersion}"
    testCompileOnly('org.awaitility:awaitility-groovy:3.1.0') {
        exclude group: 'org.codehaus.groovy', module: 'groovy-all'
    }
    testCompileOnly "org.codehaus.groovy:groovy-all:${groovyVersion}"

    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.4.2'
    testImplementation 'junit:junit:4.12'
}

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

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

if (hasProperty('debugRun') && ((project.debugRun as boolean))) {
    run {
        jvmArgs '-Xdebug', '-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005'
    }
}

import java.nio.file.Path
import java.nio.file.Paths

String className(File f, String prefix) {
    Path pwd = file(".").getAbsoluteFile().toPath()
    Path prefixPath = Paths.get(pwd.toString(),prefix)
    
    Path filePath = f.toPath()
    Path relative = prefixPath.relativize(filePath)
    String name = relative.toString()
    name.replace(File.separator,'.').substring(0, name.lastIndexOf(".") )
}

task griffonManifest {
    doLast {
        def models = fileTree("griffon-app/models").getFiles().collect {
            className(it, "griffon-app/models")
        }.sort()
        def views = fileTree("griffon-app/views").getFiles().collect {
            className(it, "griffon-app/views")
        }.sort()
        def controllers = fileTree("griffon-app/controllers").getFiles().collect {
            className(it, "griffon-app/controllers")
        }.sort()
        
        def manifest = file("$buildDir/griffonManifest")
        manifest.mkdirs()
        
        def controllerFile = new File(manifest, "griffon.core.artifact.GriffonController")
        controllerFile.withPrintWriter("US-ASCII", {writer ->
            controllers.each {controller ->
                writer.print(controller)
                writer.print('\n')
            }
        })

        def modelsFile = new File(manifest, "griffon.core.artifact.GriffonModel")
        modelsFile.withPrintWriter("US-ASCII", {writer ->
            models.each {model ->
                writer.print(model)
                writer.print('\n')
            }
        })

        def viewsFile = new File(manifest, "griffon.core.artifact.GriffonView")
        viewsFile.withPrintWriter("US-ASCII", {writer ->
            views.each {view ->
                writer.print(view)
                writer.print('\n')
            }
        })
    }
}

jar {
    dependsOn griffonManifest
    from("$buildDir/griffonManifest") {
        duplicatesStrategy = DuplicatesStrategy.INCLUDE
        into ("META-INF/griffon")
    }
}

shadowJar {
    from("shadow/services") {
        into ("META-INF/services")
    }
}
