/**
 * Compares the current project dependencies with the ones stored in the dependencies.lock file
 * Fails if the dependencies do no match and outputs the difference
 */
task compareDependencies {
    dependsOn "generateCurrentDependencies"

    def lockFile = new File(rootProject.rootDir, "dependencies.lock")

    doFirst {
        if (!lockFile.exists()) throw new IllegalStateException("${lockFile.path} file is missing. You may want to execute the 'generateDependenciesLockFile' task")
    }

    doLast {
        println "Comparing current project dependencies with the one locked in the ${lockFile.path} file..."

        def tmpLockFile = generateCurrentDependencies.outputs.getFiles().getSingleFile()
        if (lockFile.text != tmpLockFile.text) {
            println "diff ${lockFile.path} ${tmpLockFile.path}".execute().text
            throw new IllegalStateException("""Project dependencies and lock dependencies are different. You may want to :
                                                |- check why there is a difference
                                                |- or execute the 'generateDependenciesLockFile' task to overwrite the current file""".stripMargin())
        }

        println "Dependencies match, all good !"
    }
}