plugins {
    id 'eclipse'
    id 'idea'
    id 'java'
    id 'org.gretty' version '@grettyVersion@'
}

ext {
    versions = [
        tapestry: "@tapestryVersion@",
        jackson: "@jacksonVersion@",
        log4j: "@log4jVersion@",
        json: "@jsonVersion@",
        junit: "@junitVersion@",
        yasson: "@yassonVersion@",
        servlet: "@servletVersion@"
    ]
}

repositories {
    mavenCentral()
    maven {
      url 'https://repository.apache.org/content/repositories/staging/'
    }
}

configurations {
    testUnitTapestryImplementation.extendsFrom testImplementation
    testUnitTapestryRuntime.extendsFrom testRuntime
    testIntegrationTapestryImplementation.extendsFrom testImplementation
    testIntegrationTapestryRuntime.extendsFrom testRuntime
}

dependencies {
    // Apache Tapestry
    implementation("org.apache.tapestry:tapestry-core:${versions.tapestry}")

    // CoffeeScript & Less support, plus resource minification
    implementation("org.apache.tapestry:tapestry-webresources:${versions.tapestry}")

    // Uncomment this to add support for spring, hibernate, bean validation and uploads
    //implementation("org.apache.tapestry:tapestry-spring:$versions.tapestry")
    //implementation("org.apache.tapestry:tapestry-hibernate:$versions.tapestry")
    //implementation("org.apache.tapestry:tapestry-beanvalidator:$versions.tapestry")
    //implementation("org.apache.tapestry:tapestry-upload:${tapestry}")

    // Test
    testImplementation("org.apache.tapestry:tapestry-test:${versions.tapestry}")
    testImplementation("org.junit.jupiter:junit-jupiter:${versions.junit}")

    // Miscellaneous
    implementation("org.apache.logging.log4j:log4j-api:${versions.log4j}")
    implementation("org.apache.logging.log4j:log4j-core:${versions.log4j}")

    runtimeOnly("org.apache.logging.log4j:log4j-slf4j-impl:${versions.log4j}")
    runtimeOnly("org.eclipse:yasson:${versions.yasson}")
    runtimeOnly("com.fasterxml.jackson.core:jackson-core:${versions.jackson}")
    runtimeOnly("com.fasterxml.jackson.core:jackson-databind:${versions.jackson}")
    runtimeOnly("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:${versions.jackson}")

    runtimeOnly("jakarta.servlet:jakarta.servlet-api:${versions.servlet}")
}

sourceSets {
    main {
        java {
            exclude '${package.replaceAll("\.", "/")}/spring/*'
        }
    }
    test {
        java {
            exclude '${package.replaceAll("\.", "/")}/tapestry/*'
        }
    }
    testUnitTapestry {
        java {
            srcDir "$projectDir/src/test/java"
            include '${package.replaceAll("\.", "/")}/tapestry/unit/*/*Test.java'
        }
        compileClasspath += main.output + test.output
        runtimeClasspath += main.output + test.output
    }
    testIntegrationTapestry {
        java {
            srcDir "$projectDir/src/test/java"
            include '${package.replaceAll("\.", "/")}/tapestry/integration/*/*Test.java'
        }
        compileClasspath += main.output + test.output
        runtimeClasspath += main.output + test.output
    }
}

java {
    sourceCompatibility = JavaVersion.VERSION_11
}

gretty {
    servletContainer = 'tomcat9'
    contextPath = '/'
}

test {
    useJUnitPlatform()
}

tasks.register('testUnitTapestry', Test) {
    useJUnitPlatform()
    testClassesDirs = sourceSets.testUnitTapestry.output.classesDirs
    classpath = sourceSets.testUnitTapestry.runtimeClasspath
}

tasks.register('testIntegrationTapestry', Test) {
    useTestNG()
    testClassesDirs = sourceSets.testIntegrationTapestry.output.classesDirs
    classpath = sourceSets.testIntegrationTapestry.runtimeClasspath
}
