import org.flywaydb.gradle.task.*

buildscript {
    dependencies {
        classpath "com.mysql:mysql-connector-j:$mysqlVersion"
        classpath "org.flywaydb:flyway-mysql:$flywayVersion"
    }
}

plugins {
    id 'org.springframework.boot' version "${springBootVersion}"
    id "org.flywaydb.flyway" version "${flywayVersion}"
    id 'com.github.dreamhead.todo'
}

style {
    excludePackages = [
    ]
    excludeClasses = [
            "com.github.dreamhead.todo.api.Bootstrap"
    ]
}

coverage {
    excludePackages = [
    ]
    excludeClasses = [
            "com.github.dreamhead.todo.api.Bootstrap"
    ]
}

dependencies {
    implementation(project(":todo-core"))
    implementation("org.springframework.boot:spring-boot-starter:$springBootVersion")
    implementation("org.springframework.boot:spring-boot-starter-web:$springBootVersion")
    implementation("org.springframework.boot:spring-boot-starter-data-jpa:$springBootVersion")
    runtimeOnly("com.mysql:mysql-connector-j:$mysqlVersion")
    testImplementation("org.springframework.boot:spring-boot-starter-test:$springBootVersion")
    testImplementation("io.cucumber:cucumber-java8:$cucumberVersion")
    testImplementation("io.cucumber:cucumber-junit:$cucumberVersion")
    testImplementation("io.cucumber:cucumber-junit-platform-engine:$cucumberVersion")
    testImplementation("io.cucumber:cucumber-spring:$cucumberVersion")
}

configurations {
    cucumberRuntime {
        extendsFrom testImplementation, runtimeOnly
    }
}

task cucumber() {
    dependsOn assemble, testClasses
    doLast {
        javaexec {
            main = "io.cucumber.core.cli.Main"
            classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
            args = ['--plugin', 'pretty', '--glue', 'com.github.dreamhead.todo.api.stepdef', 'src/test/resources']
        }
    }
}

task migrateToDev(type: FlywayMigrateTask) {
    url = 'jdbc:mysql://localhost:3306/todo_dev?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true'
    user = 'todo'
    password = 'geektime'
    locations = ["filesystem:$rootDir/gradle/config/migration"]
}

task migrateToTest(type: FlywayMigrateTask) {
    url = 'jdbc:mysql://localhost:3306/todo_test?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true'
    user = 'todo'
    password = 'geektime'
    locations = ["filesystem:$rootDir/gradle/config/migration"]
}

task migrate {
}

migrate.dependsOn migrateToDev, migrateToTest

springBoot {
    mainClass = 'com.github.dreamhead.todo.api.Bootstrap'
}

bootJar {
    archiveClassifier = 'boot'

    // https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-1.4-Release-Notes#jersey-classpath-scanning-limitations
    requiresUnpack '**/todo-*.jar'

    launchScript()
}