evaluationDependsOn(':java')

description = "Timebase Server"

ext {
    dockerImageName = 'timebase-ce-server'
    dockerImages = [
            main   : 'src/main/docker/Dockerfile',
            java17 : 'src/main/docker/java17/Dockerfile',
    ]
}

shadowJar {
    zip64 = true
    mergeServiceFiles('META-INF/spring.*')
    mergeServiceFiles('META-INF/services/javax.servlet.ServletContainerInitializer') // for tomcat
}

dependencies {

    api project(':java:timebase:commons')
    api project(':java:timebase:api')
    api project(':java:timebase:aerondirect')

    implementation project(':java:timebase:computations-api')
    runtimeOnly project(':java:timebase:computations-std')

    implementation project(':java:timebase:snmpagent')

    implementation ('com.epam.deltix:timebase-lang')
    compile ('com.epam.deltix:timebase-util')
    implementation ('com.epam.deltix:timebase-messages')
    implementation ('com.epam.deltix:timebase-collections')
    implementation ('com.epam.deltix:dfp')
    implementation ('com.epam.deltix:containers')
    implementation ('com.epam.deltix:thread-affinity')

    implementation ('org.apache.commons:commons-lang3')

    implementation 'com.epam.deltix:gflog-api', 'com.epam.deltix:gflog-core',
            'com.epam.deltix:gflog-jul'

    implementation 'io.micrometer:micrometer-registry-prometheus'

    implementation 'org.apache.taglibs:taglibs-standard-impl' // JSTL Support
    implementation 'javax.servlet.jsp.jstl:jstl-api'; // JSTL Support

    implementation 'javax.xml.bind:jaxb-api'
    implementation 'com.sun.xml.bind:jaxb-impl'
    implementation 'com.sun.xml.bind:jaxb-core'

    implementation group: 'org.ehcache', name: 'ehcache', version: '3.9.3'

    implementation 'org.lz4:lz4-java:1.8.0' // LZH codec
    implementation 'org.xerial.snappy:snappy-java'

    //implementation('org.glassfish:javax.json')

    implementation 'com.google.guava:guava'

    implementation group: 'com.microsoft.azure', name: 'azure-data-lake-store-sdk', version: '2.3.9'

    implementation 'com.github.ben-manes.caffeine:caffeine:2.5.6' // Cache implementation
    implementation 'com.lmax:disruptor'
    implementation 'commons-io:commons-io'

    implementation 'org.reflections:reflections'

    // TODO: Replace by JSR305
    compileOnly 'com.intellij:annotations'
    compileOnly 'de.jflex:jflex:1.4.3'

    // For JMH-based tests
    testImplementation 'org.openjdk.jmh:jmh-core'
    testImplementation 'org.openjdk.jmh:jmh-generator-annprocess'

    compile 'org.owasp.encoder:encoder'
}

// Docker

// Use :java:timebase:server:dockerPublishImage to publish the image
task copyTimebaseServerContainerDependensies(type: Copy) {
    group 'docker'
    description 'Copy all timebase server dependencies'
    //dependsOn ':java:cleanSubprojects' // This is necessary to force clean build.
    dependsOn 'build'
    dependsOn ':java:timebase:server:copyWebapp'
    dependsOn ':java:timebase:commons:copyWebapp'
    dependsOn ':java:timebase:s3:build'
    dependsOn ':distribution:build'
    dependsOn ':generateSetQsVersionScript'
    //dependsOn ':java:timebase:client:jar'

    into project.file("$buildDir/docker/timebase")

    //from "$rootDir/inst.properties"
    into ("web") {
        from "$rootDir/web" // TODO: Avoid sharing resources thought global folder. Use explicit task outputs instead.
        include 'tb/**'
        include 'default/**'
    }
    into ("bin") {
        from "$rootDir/bin" // TODO: Avoid sharing resources thought global folder. Use explicit task outputs instead.
    }
    into ("lib") {
        // Runtime dependencies
        from configurations.runtimeClasspath
        // Main TimeBase server jar
        from jar

        from project(':java:timebase:client').configurations.runtimeClasspath
        from project(':java:timebase:client').tasks.findByName('jar')

        from project(':java:timebase:s3').configurations.runtimeClasspath
        from project(':java:timebase:s3').tasks.findByName('jar')
    }
}

// New docker task
// Right now this task re-uses old task later the content of copyTimebaseServerContainerDependensies whold be moved to prepareFilesForDocker
task prepareFilesForDocker {
    dependsOn copyTimebaseServerContainerDependensies
}

task copyWebapp(type: Copy) {
    description 'Copy timebase webapp war file into timebase server resources'
    dependsOn ':java:timebase:web:war'
    mustRunAfter processResources
    mustRunAfter clean

    from (project(':java:timebase:web').file('build/libs')) {
        include '*.war'
        rename 'timebase-web(.*).war', 'timebase-web.war'
    }
    into sourceSets.main.output.resourcesDir.absolutePath + '/webapp'
}

shadowJar.dependsOn copyWebapp


def generatedMainDir = file('build/generated-src/main/src')
def lexerAndParserPackage = "com/epam/deltix/qsrv/hf/tickdb/lang/parser"
def lexerAndParserDir = file("src/main/java/com/epam/deltix/qsrv/hf/tickdb/lang/parser")

sourceSets.main.java.srcDirs generatedMainDir

task generateQqlLexer(type: JavaExec) {
    group = "QQL"
    description = "Generates QQL lexer via flex library"
    classpath = configurations.compileOnly
    main = "JFlex.Main"
    workingDir = lexerAndParserDir
    args = ["qql.flex", "-d", "$generatedMainDir/$lexerAndParserPackage"]
}

task generateQqlParser(type: JavaExec, dependsOn: generateQqlLexer) {
    group = "QQL"
    description = "Generates QQL parser via java_cup library"
    classpath = configurations.compile
    main = "java_cup.Main"
    workingDir = "$generatedMainDir/$lexerAndParserPackage"
    args = ["-package", lexerAndParserPackage,
            "-parser", "Parser",
            "-symbols", "Symbols",
            "$lexerAndParserDir/qql.cup"
    ]
}

compileJava.dependsOn generateQqlParser
