buildscript {
    repositories {
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }
    dependencies {
        classpath 'org.gretty:gretty:4.0.3'
    }
}

apply plugin: 'java'
apply plugin: 'org.gretty'

java {
    sourceCompatibility = JavaVersion.VERSION_11
    targetCompatibility = JavaVersion.VERSION_11
}

archivesBaseName = 'carballo-gwt'

dependencies {
    implementation 'org.gwtproject:gwt-user:2.10.0'
    implementation 'org.gwtproject:gwt-dev:2.10.0'
    implementation 'org.vectomatic:lib-gwt-svg:0.5.15'
    implementation project(':core')
    implementation project(path: ':core', configuration: 'archives')
    implementation project(':gwt')
    implementation project(path: ':gwt', configuration: 'archives')
}

clean.doFirst {
    delete "${project.projectDir}/src/main/gwt-unitCache"
    delete "${project.projectDir}/src/main/webapp/WEB-INF"
    delete "${project.projectDir}/src/main/webapp/org.vectomatic.libsvgchess"
}

task compileGwt (dependsOn: classes, type: JavaExec) {
    ext.buildDir = "${project.projectDir}/src/main/webapp/"
    ext.extraDir = "${project.buildDir}/extra"

    sourceSets.main.java.srcDirs.each{ dir ->
        inputs.dir dir
    }
    inputs.dir sourceSets.main.output.resourcesDir
    outputs.dir buildDir

    doFirst {
        file(buildDir).mkdirs()
    }

    mainClass = 'com.google.gwt.dev.Compiler'

    classpath {
        [
                sourceSets.main.java.srcDirs,           // Java source
                sourceSets.main.output.resourcesDir,    // Generated resources
                sourceSets.main.java.classesDirectory,  // Generated classes
                sourceSets.main.compileClasspath,       // Deps
        ]
    }

    args =
        [
                'org.vectomatic.libgwtsvgchess',
                '-war', buildDir,
                '-extra', extraDir,
                '-logLevel', 'INFO',
                '-localWorkers', '2',
                '-style', 'PRETTY',
                '-XdisableCastChecking',   // Disables runtime checking of cast operations. This can reduce compiled file sizes.
                '-XdisableClassMetadata',  // Disables some java.lang.Class methods. This can reduce compiled file sizes.
        ]

    maxHeapSize = '512M'
}

jar.dependsOn compileGwt

task gwtSuperDev(dependsOn: classes, type: JavaExec) {

    mainClass = 'com.google.gwt.dev.codeserver.CodeServer'

    classpath {
        [
                sourceSets.main.java.srcDirs,           // Java source
                sourceSets.main.output.resourcesDir,    // Generated resources
                sourceSets.main.runtimeClasspath,       // Deps
        ]
    }

    args =
            [
                    'org.vectomatic.libgwtsvgchess'
            ]

    maxHeapSize = '512M'
}

gretty {
    servletContainer = 'tomcat10'
    contextPath = '/chess'
}