import org.gradle.internal.os.OperatingSystem

configurations {
    liquibase
}

dependencies {
    liquibase "org.liquibase.ext:liquibase-hibernate5:${liquibase_hibernate5_version}"
}

if (OperatingSystem.current().isWindows()) {
    task pathingLiquibaseJar(type: Jar) {
        dependsOn configurations.liquibase
        appendix = 'pathingLiquibase'

        doFirst {
            manifest {
                attributes 'Class-Path':
                    sourceSets.main.runtimeClasspath.plus(configurations.liquibase)
                    .collect {
                        it.toURL().toString().replaceFirst(/file:\/+/, '/')
                    }.join(' ')
            }
        }
    }
}

task liquibaseDiffChangelog(dependsOn: compileJava, type: JavaExec) {
    group = "liquibase"

    if (OperatingSystem.current().isWindows()) {
        dependsOn pathingLiquibaseJar
        doFirst {
            classpath = files(pathingLiquibaseJar.archivePath)
        }
    } else {
        classpath sourceSets.main.runtimeClasspath
        classpath configurations.liquibase
    }
    main = "liquibase.integration.commandline.Main"

    args "--changeLogFile=src/main/resources/config/liquibase/changelog/" + buildTimestamp() +"_changelog.xml"
    args "--referenceUrl=hibernate:spring:com.apress.jhblog.domain?dialect=&hibernate.physical_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy&hibernate.implicit_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy"
    args "--username=temp"
    args "--password="
    args "--url="
    args "--driver="
    args "diffChangeLog"
}

def buildTimestamp() {
    def date = new Date()
    def formattedDate = date.format('yyyyMMddHHmmss')
    return formattedDate
}
