jar.enabled = false

ext {
    arquillianWildflyVersion = '3.0.1.Final'
    wildflyVersion = '26.0.1.Final'
}

repositories {
    // Required by arquillian
    maven { url "https://maven.repository.redhat.com/ga/" }
}

configurations {
    jbossmodule
    jbossdist { transitive = false }
}

dependencies {

    jbossmodule(project(':asciidoctorj-api')) {
        transitive = false
    }
    jbossmodule(project(':asciidoctorj')) {
        transitive = false
    }
    jbossmodule "org.jruby:jruby-complete:$jrubyVersion"

    jbossdist group: 'org.wildfly', name: 'wildfly-dist', version: wildflyVersion, ext: 'zip'

    testCompileOnly project(':asciidoctorj')
    testImplementation "javax:javaee-api:7.0"
    // Arquillian Unit5 support requires 1.7.0.Alpha5
    // https://github.com/arquillian/arquillian-core/issues/137#issuecomment-726596414
    testImplementation("org.junit.vintage:junit-vintage-engine")
    testImplementation "org.jsoup:jsoup:$jsoupVersion"
    testImplementation "org.jboss.arquillian.junit:arquillian-junit-container:$arquillianVersion"

    testRuntimeOnly "org.wildfly.arquillian:wildfly-arquillian-container-managed:$arquillianWildflyVersion"
}

task unpackWildfly(type: Copy) {
    from {
        zipTree(configurations.jbossdist.singleFile)
    }
    into "$buildDir"
    doNotTrackState "Build on Github Actions seems to fail on Windows without it"
}

compileTestJava {
    dependsOn unpackWildfly
}

javadocJar {
    dependsOn unpackWildfly
}

sourcesJar {
    dependsOn unpackWildfly
}

task createModuleDescriptor(type: Copy) {
    from "src/test/resources/module.xml"
    into "build/modules/org/asciidoctor/asciidoctorj/main"
    filter {
        it
                .replaceAll('@@version@@', project.version)
                .replaceAll('@@jrubyVersion@@', jrubyVersion)
    }
}

task createModule(type: Copy) {
    from configurations.jbossmodule
    into "build/modules/org/asciidoctor/asciidoctorj/main"
    dependsOn createModuleDescriptor
}

test {
    testLogging {
        // events 'passed', 'failed', 'skipped', 'standard_out', 'standard_error'
        // events 'standard_out', 'standard_error'
        afterSuite { desc, result ->
            if (!desc.parent && logger.infoEnabled) {
                logger.info "Test results: ${result.resultType} (${result.testCount} tests, ${result.successfulTestCount} passed, ${result.failedTestCount} failed, ${result.skippedTestCount} skipped)"
            }
        }
    }

    def jbossHome = file("build/wildfly-$wildflyVersion").absolutePath
    systemProperty('jboss.home', jbossHome)
    systemProperty('module.path', jbossHome + "/modules" + File.pathSeparator + file('build/modules').absolutePath)

    if (JavaVersion.current().isJava11Compatible()) {
        systemProperty('arquillian.launch', 'wildfly-java11')
    }

    dependsOn createModule
    dependsOn unpackWildfly
    dependsOn configurations.jbossmodule
}
