wrapper {
    gradleVersion = '4.5.1'
    distributionType = Wrapper.DistributionType.ALL
}

buildscript {
    repositories {
        mavenLocal()
        jcenter()
        mavenCentral()
        gradlePluginPortal()
    }
    dependencies {
        classpath 'org.springframework.boot:spring-boot-gradle-plugin:1.5.10.RELEASE'
        classpath 'com.ofg:uptodate-gradle-plugin:1.6.3'
        classpath 'se.transmode.gradle:gradle-docker:1.2'
        classpath "org.springframework.cloud:spring-cloud-contract-gradle-plugin:${springCloudContractVersion}"
        classpath 'com.blogspot.toomuchcoding:gradle-test-profiler:0.3.1'
        classpath 'gradle.plugin.com.gorylenko.gradle-git-properties:gradle-git-properties:1.4.20'

        if (project.hasProperty('coverage')) {
            classpath 'org.kt3k.gradle.plugin:coveralls-gradle-plugin:2.8.2'
            classpath 'org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.6.1'
        }
    }
}

apply plugin: 'groovy'
apply plugin: 'org.springframework.boot'

ext['selenium.version'] = seleniumVersion

group = mavenProjectGroupId
version = findProperty('currentVersion') ?: 'LOCAL'

sourceCompatibility = '1.8'

apply from: 'gradle/cli.gradle'
apply from: 'gradle/test.gradle'
apply from: 'gradle/misc.gradle'
apply from: 'gradle/release.gradle'
apply from: 'gradle/contracts.gradle'

repositories {
    mavenLocal()
    jcenter()
}

dependencies {
    compile 'org.springframework.boot:spring-boot-starter-web'
    compile 'org.springframework.boot:spring-boot-starter-actuator'
    compile 'org.springframework.boot:spring-boot-starter-data-jpa'
    compile 'org.springframework:spring-context-support'
    compile 'org.codehaus.groovy:groovy-all'
    compile 'com.fasterxml.jackson.core:jackson-databind'
    compile 'com.google.guava:guava:20.0'
    compile 'org.apache.commons:commons-lang3:3.7'
    compile 'org.apache.tomcat.embed:tomcat-embed-el'
    compile 'com.jayway.jsonpath:json-path-assert:2.4.0'
    compile 'org.yaml:snakeyaml'
    compile 'org.hibernate:hibernate-validator'
    compile 'org.aspectj:aspectjrt'
    compile 'org.codehaus.gpars:gpars:1.2.1'
    compile 'net.sf.ehcache:ehcache'
    compile "com.ofg:micro-infra-spring-boot-starter:${microInfraSpringVersion}"
    compile "com.ofg:activity-tracker-boot-starter:${microInfraSpringVersion}"

    runtime 'cglib:cglib-nodep:3.2.6'
    runtime 'org.objenesis:objenesis:2.6'
    runtime 'org.aspectj:aspectjweaver'
    runtime 'com.h2database:h2'

    testCompile 'org.awaitility:awaitility:3.0.0'
    testCompile 'org.springframework.boot:spring-boot-starter-test'
    testCompile "com.ofg:micro-infra-spring-test:${microInfraSpringVersion}"
    testCompile "org.gebish:geb-core:${gebVersion}"
    testCompile "org.gebish:geb-spock:${gebVersion}"
    testCompile 'org.spockframework:spock-spring'
    testCompile "org.seleniumhq.selenium:selenium-firefox-driver:${seleniumVersion}"
    testCompile "org.seleniumhq.selenium:selenium-chrome-driver:${seleniumVersion}"
    testCompile "org.seleniumhq.selenium:selenium-support:${seleniumVersion}"
    testCompile "io.rest-assured:rest-assured:${restAssuredVersion}"
    testCompile "io.rest-assured:spring-mock-mvc:${restAssuredVersion}"

    // for easier contract development - ide auto-completion support
    testCompileOnly "org.springframework.cloud:spring-cloud-starter-contract-verifier:${springCloudContractVersion}"
}

bootRun {
    description = "Starts application with dev profile, use task rule 'bootRun<Profiles>' to change profile"

    systemProperty 'CONFIG_FOLDER', environment['CONFIG_FOLDER']
    systemProperty 'APP_ENV', environment['APP_ENV'] ?: 'dev'
    systemProperty 'server.port', findProperty('server.port') ?: defaultApplicationPort
    systemProperty 'spring.profiles.active', findProperty('spring.profiles.active') ?: 'dev,springCloud'
    systemProperties System.properties as Map<String, ?> // Allows to pass command-line properties (using -D switch)

    doFirst {
        if (!systemProperties['CONFIG_FOLDER']) {
            logger.error('CONFIG_FOLDER system variable not defined. Please set it in the system or pass via -D switch.')
        }
    }
}

tasks.addRule("Pattern: bootRun<Profiles>: Starts application with given comma separated profiles, eg. 'bootRunProd', 'bootRunProd,smokeTests'") { String taskName ->
    if (taskName.startsWith('bootRun')) {
        task(taskName) {
            bootRun.systemProperty 'spring.profiles.active', (taskName - 'bootRun').uncapitalize()
            finalizedBy bootRun
        }
    }
}
