Skip to content

Instantly share code, notes, and snippets.

@jprinet
Last active July 22, 2022 14:21
Show Gist options
  • Save jprinet/5151fa507e26cf5382476164d11029b0 to your computer and use it in GitHub Desktop.
Save jprinet/5151fa507e26cf5382476164d11029b0 to your computer and use it in GitHub Desktop.
Capture Task inputs size
tasks.withType<JavaCompile>().configureEach {
doLast {
this.inputs.files.map { it ->
if(it.isFile) {
FileSize(it.length(), it.path)
} else {
FileSize(0, "DIR - " + it.path)
}
}.sortedBy {
it.size
}.reversed()
.forEach {
println(it)
}
}
}
class FileSize(size: Long, description: String) {
var description = description
get() = field
set(value) {
field = value
}
var size = size
get() = field
set(value) {
field = value
}
private fun Long.formatBinarySize(): String {
val kiloByteAsByte = 1.0 * 1024.0
val megaByteAsByte = 1.0 * 1024.0 * 1024.0
val gigaByteAsByte = 1.0 * 1024.0 * 1024.0 * 1024.0
val teraByteAsByte = 1.0 * 1024.0 * 1024.0 * 1024.0 * 1024.0
val petaByteAsByte = 1.0 * 1024.0 * 1024.0 * 1024.0 * 1024.0 * 1024.0
return when {
this < kiloByteAsByte -> "${this.toDouble()} B"
this >= kiloByteAsByte && this < megaByteAsByte -> "${String.format("%.2f", (this / kiloByteAsByte))} KB"
this >= megaByteAsByte && this < gigaByteAsByte -> "${String.format("%.2f", (this / megaByteAsByte))} MB"
this >= gigaByteAsByte && this < teraByteAsByte -> "${String.format("%.2f", (this / gigaByteAsByte))} GB"
this >= teraByteAsByte && this < petaByteAsByte -> "${String.format("%.2f", (this / teraByteAsByte))} TB"
else -> "Bigger than 1024 TB"
}
}
override fun toString(): String {
return "$description => ${size.formatBinarySize()}"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment