plugins {
    id "java"
    id "war"
    id "org.springframework.boot" version "2.0.2.RELEASE"
    id "io.spring.dependency-management" version "1.0.5.RELEASE"
    id "org.flywaydb.flyway" version "5.0.7"
    id "com.moowork.node" version "1.2.0"
}

final def jUnitVersion = '5.1.0'

repositories {
    jcenter()
    mavenCentral()
    mavenLocal()
    maven {
        url "https://plugins.gradle.org/m2/"
    }
    maven { url "http://archiva.infoclinika.net/repository/release/" }
    maven { url "https://nexus.bedatadriven.com/content/groups/public" }
}

wrapper {
    gradleVersion = '4.6'
}

sourceCompatibility = 1.8

allprojects {
    group = 'com.infoclinika'
    version = '1.0'
}

dependencies {
    compile group: 'com.infoclinika', name: 'analysis-platform', version: '1.1.27'
    compile group: 'com.infoclinika', name: 'storage-cloud', version: '1.0.14'
    compile group: 'com.amazonaws', name: 'aws-java-sdk-core', version: '1.11.295'
    compile group: 'com.amazonaws', name: 'aws-java-sdk-s3', version: '1.11.295'

    compile group: 'com.google.code.findbugs', name: 'jsr305', version: '3.0.2'
    compile group: 'org.apache.commons', name: 'commons-math3', version: '3.6.1'
    compile group: 'th.co.geniustree.springdata.jpa', name: 'specification-with-projections', version: '2.0.0'

    compile group: 'org.renjin', name: 'renjin-script-engine', version: '0.9.2709'
    compile group: 'org.apache.commons', name: 'commons-collections4', version: '4.3'

    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.springframework.boot:spring-boot-starter-security')
    compile('org.springframework.boot:spring-boot-starter-log4j2')
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('org.springframework.boot:spring-boot-starter-mail')

    runtime group: 'mysql', name: 'mysql-connector-java', version: '6.0.6'

    compile group: 'com.univocity', name: 'univocity-parsers', version: '2.6.3'

    // jaxb-api is required for Java 9+ environment
    compile group: 'javax.xml.bind', name: 'jaxb-api', version: '2.3.0'

    testCompile('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'junit'
    }
    testCompile('org.springframework.security:spring-security-test')

    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: jUnitVersion
    testRuntime group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: jUnitVersion
    testCompile 'org.mockito:mockito-core:2.+'
    testCompile group: 'com.h2database', name: 'h2', version: '1.4.197'
}

configurations {
    all {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
    }
}

sourceSets {
    main {
        resources {
            exclude '/ui/bower_components'
            exclude '/ui/node_modules'
            exclude '/ui/test'
            exclude '/ui/src'
            exclude '/ui/img'
            exclude '/ui/static'
            exclude '**/ui/*.json'
        }
    }
}

node {
    version = '8.11.2'
    npmVersion = '6.1.0'
    download = true
    nodeModulesDir = file("${project.projectDir}/src/main/resources/ui")
}

npm_install {
    args = ['--unsafe-perm']
}

task buildUi(type: NpmTask) {
    dependsOn 'npm_install'
    args = ['run', 'polymer-build']
}

bootWar.dependsOn(buildUi)

test.onlyIf { ! Boolean.getBoolean('api') && ! Boolean.getBoolean('ui') }
nodeSetup.onlyIf { ! Boolean.getBoolean('api') }
npmSetup.onlyIf { ! Boolean.getBoolean('api') }
npm_install.onlyIf { ! Boolean.getBoolean('api') }
bootWar.onlyIf { ! Boolean.getBoolean('api') }
buildUi.onlyIf { ! Boolean.getBoolean('api') }
flywayInfo.onlyIf { ! Boolean.getBoolean('api') && ! Boolean.getBoolean('ui') }

task bootRunApi(type: org.springframework.boot.gradle.tasks.run.BootRun, dependsOn: 'build') {

    group = 'Application'

    doFirst() {
        main = bootJar.mainClassName
        classpath = sourceSets.main.runtimeClasspath
    }

}

task bootRunUi(type: org.springframework.boot.gradle.tasks.run.BootRun, dependsOn: 'build') {

    group = 'Application'

    doFirst() {
        main = bootJar.mainClassName
        classpath = sourceSets.main.runtimeClasspath
    }
}


flywayInfo.dependsOn(flywayMigrate)
build.dependsOn(flywayInfo)

test {
    useJUnitPlatform()

    testLogging {
        events "passed", "skipped", "failed"
    }
}
