buildscript {
    repositories {
        mavenCentral()
        maven { url "https://repo.spring.io/milestone" }
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:$springBootVersion")
        classpath "com.avast.gradle:gradle-docker-compose-plugin:$dockerComposePluginVersion"
    }
}

apply plugin: 'docker-compose'
apply plugin: WaitForMySqlPlugin


dockerCompose {
    environment.put "EVENTUATE_COMMON_VERSION", eventuateCommonImageVersion
    environment.put "EVENTUATE_MESSAGING_KAFKA_IMAGE_VERSION", eventuateMessagingKafkaImageVersion
    environment.put "EVENTUATE_CDC_VERSION", eventuateCdcImageVersion
    environment.put "EVENTUATE_JAVA_BASE_IMAGE_VERSION", eventuateExamplesBaseImageVersion
    environment.put "SERVICE_IMAGE_VERSION", version

    projectName = "redisconf-2019"
    removeOrphans = true

    if (project.hasProperty("microservices"))
        startedServices = microservices.split(",")
    else
        startedServices = ['mysql', 'redis', 'cdc-service', 'customer-service', 'order-service', 'order-history-service']

    databases {
        startedServices = ['mysql', 'redis']
        projectName = "redisconf-2019"
    }

    infrastructure {
        startedServices = ['cdc-service']
        projectName = "redisconf-2019"
    }

    javaDevelopment {
        useComposeFiles = ["docker-compose-java-development.yml"]
    }

}

databasesComposeUp.finalizedBy(waitForMySql)
infrastructureComposeUp.dependsOn(databasesComposeUp)
composeUp.dependsOn(infrastructureComposeUp)


task prepareJavaDevelopmentDockerImage(type:Exec) {
    commandLine './prepare-java-development-docker-image.sh'
}

javaDevelopmentComposeBuild.dependsOn(prepareJavaDevelopmentDockerImage)

subprojects.each {
    if (it.name.endsWith("-service")) {
        composeUp.dependsOn("${it.name}:assemble")
        composeBuild.dependsOn("${it.name}:assemble")
    }
}

subprojects {

    apply plugin: "java"

    targetCompatibility = '8'
    sourceCompatibility = '8'

    repositories {
        mavenCentral()
        eventuateMavenRepoUrl.split(',').each { repoUrl -> maven { url repoUrl } }
    }

    // Deadlock on H2 shutdown - multiple shutdown hooks for appctx
    test {
        forkEvery 1
    }

    dependencies {
        implementation(platform("io.eventuate.platform:eventuate-platform-dependencies:$eventuatePlatformVersion"))
        implementation(platform("org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"))

        constraints {
            implementation "org.springdoc:springdoc-openapi-ui:$springDocOpenApiUiVersion"
        }

        testImplementation "junit:junit:4.13.2"

    }
}

task testRedisCli(type:Exec) {
    commandLine "sh", "-c", "echo keys '*' | ./redis-cli.sh -i"
}

task testMySqlCli(type:Exec) {
    commandLine "sh", "-c", "echo 'show databases;' | ./mysql-cli.sh -i"
}

task buildAndStartServices(type: GradleBuild) {
    tasks = ["testClasses", "assemble", "composeUp"]
}


task cleanupBeforeStarting(type: GradleBuild) {
    tasks = ["composeDown"]
}

task buildAndTestAll(type: GradleBuild) {
    List<String> t = ["testClasses"]

    if (!project.hasProperty("buildAndTestAllUseExisting") || !buildAndTestAllUseExisting) {
        t.add("cleanupBeforeStarting")
    } else
        println "Using existing containers"

    t.add("infrastructureComposeUp")
    t.addAll(subprojects.collect { it.name }.findAll { it != "end-to-end-tests" }.collect { ":" + it + ":build" })
    t.add("composeUp")

    t.add("testRedisCli")
    t.add("testMySqlCli")

    t.addAll([":end-to-end-tests:cleanTest", ":end-to-end-tests:test"])

    if (!project.hasProperty("buildAndTestAllKeepRunning") || !buildAndTestAllKeepRunning)
      t.add("composeDown")
    else
      println "Containers left running"

    tasks = t
}
