Skip to content

Instantly share code, notes, and snippets.

@pgilad
Last active January 28, 2019 07:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pgilad/204e6d93b3ca3382bed6f7922eef073f to your computer and use it in GitHub Desktop.
Save pgilad/204e6d93b3ca3382bed6f7922eef073f to your computer and use it in GitHub Desktop.
Create a revision for file
fun digest(file: File, algorithm: String): String {
val contents = file.readBytes()
val messageDigest = MessageDigest.getInstance(algorithm)
val digestBytes = messageDigest.digest(contents)
return String.format("%032x", BigInteger(1, digestBytes)).substring(0..9)
}
project(":jetpack").configure {
apply(plugin = "com.github.johnrengelman.shadow")
tasks.register("rev") {
group = "build"
description = "Append revision to output jar"
val shadowJar by tasks.getting(ShadowJar::class)
dependsOn(shadowJar)
inputs.files(shadowJar)
outputs.dir(shadowJar.destinationDir)
doLast {
copy {
val singleFile = shadowJar.outputs.files.singleFile
val hash = digest(file(singleFile), "SHA-256")
from(singleFile)
into(shadowJar.destinationDir)
include("*-all.jar")
rename("(.+)-all.jar", "$1-$hash-all.jar")
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment