Skip to content

Latest commit

 

History

History

gradle

import java.nio.charset.StandardCharsets
import java.security.MessageDigest

task verifyReadme {
    inputs.file 'README.md' withPropertyName 'readme'
    inputs.file '.github/README.md.sha256' withPropertyName 'readmeChecksum'

    doLast {
        if (file('.github/README.md.sha256').text != calculateReadmeChecksum()) {
            if (gradle.taskGraph.hasTask(updateReadme)) {
                throw new IllegalStateException("The README.md file was tampered with manually, " +
                        "if you want to overwrite it, add \"-x $name\" to your Gradle call")
            }
            throw new IllegalStateException("The README.md file was tampered with manually")
        }
    }
}
check.dependsOn verifyReadme

task updateReadme(dependsOn: verifyReadme) {
    def coreRuntimeClasspath = project(':javacord-core').configurations.runtimeClasspath

    inputs.property 'version', version
    inputs.file '.github/README_template.md' withPropertyName 'readmeTemplate'
    inputs.files coreRuntimeClasspath withPropertyName 'coreRuntimeClasspath' withNormalizer ClasspathNormalizer
    outputs.file 'README.md' withPropertyName 'readme'
    outputs.file '.github/README.md.sha256' withPropertyName 'readmeChecksum'

    doLast {
        def log4jVersion = coreRuntimeClasspath
                .allDependencies
                .find { (it.group == 'org.apache.logging.log4j') && (it.name == 'log4j-api') }
                .version
        copy {
            from '.github/README_template.md'
            into '.'
            rename { 'README.md' }
            expand version: version, log4jVersion: log4jVersion
        }
        file('.github/README.md.sha256').text = calculateReadmeChecksum()
    }
}

private String calculateReadmeChecksum() {
    def sha256 = MessageDigest.getInstance('SHA-256')
    def checksum = sha256.digest(file('README.md').readLines(StandardCharsets.UTF_8.toString()).join('\n').bytes)
    new BigInteger(1, checksum).toString(16).padLeft(sha256.digestLength * 2, '0')
}