[Gradle, JS] Use Gradle's hashers
This commit is contained in:
+8
-4
@@ -2,11 +2,11 @@ package org.jetbrains.kotlin.gradle.targets.js.nodejs
|
||||
|
||||
import org.gradle.api.DefaultTask
|
||||
import org.gradle.api.artifacts.Configuration
|
||||
import org.gradle.api.file.FileCollection
|
||||
import org.gradle.api.file.FileSystemOperations
|
||||
import org.gradle.api.file.FileTree
|
||||
import org.gradle.api.provider.Provider
|
||||
import org.gradle.api.tasks.*
|
||||
import org.gradle.internal.hash.FileHasher
|
||||
import org.jetbrains.kotlin.gradle.logging.kotlinInfo
|
||||
import org.jetbrains.kotlin.gradle.plugin.statistics.KotlinBuildStatsService
|
||||
import org.jetbrains.kotlin.gradle.targets.js.calculateDirHash
|
||||
@@ -26,6 +26,10 @@ abstract class NodeJsSetupTask : DefaultTask() {
|
||||
|
||||
private val archiveOperations = ArchiveOperationsCompat(project)
|
||||
|
||||
@get:Inject
|
||||
internal open val fileHasher: FileHasher
|
||||
get() = error("Should be injected")
|
||||
|
||||
@get:Inject
|
||||
internal open val fs: FileSystemOperations
|
||||
get() = error("Should be injected")
|
||||
@@ -84,7 +88,7 @@ abstract class NodeJsSetupTask : DefaultTask() {
|
||||
val upToDate = destinationHashFile.let { file ->
|
||||
if (file.exists()) {
|
||||
file.useLines {
|
||||
it.single() == (calculateDirHash(destination).also { dirHash = it })
|
||||
it.single() == (fileHasher.calculateDirHash(destination).also { dirHash = it })
|
||||
}
|
||||
} else false
|
||||
}
|
||||
@@ -92,7 +96,7 @@ abstract class NodeJsSetupTask : DefaultTask() {
|
||||
val tmpDir = temporaryDir
|
||||
unpackNodeArchive(nodeJsDist, tmpDir)
|
||||
|
||||
if (upToDate && calculateDirHash(tmpDir.resolve(destination.name))!! == dirHash) return
|
||||
if (upToDate && fileHasher.calculateDirHash(tmpDir.resolve(destination.name))!! == dirHash) return
|
||||
|
||||
if (destination.isDirectory) {
|
||||
destination.deleteRecursively()
|
||||
@@ -110,7 +114,7 @@ abstract class NodeJsSetupTask : DefaultTask() {
|
||||
}
|
||||
|
||||
destinationHashFile.writeText(
|
||||
calculateDirHash(destination)!!
|
||||
fileHasher.calculateDirHash(destination)!!
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
+10
-8
@@ -5,9 +5,10 @@
|
||||
|
||||
package org.jetbrains.kotlin.gradle.targets.js
|
||||
|
||||
import org.gradle.internal.hash.FileHasher
|
||||
import org.gradle.internal.hash.Hashing.defaultFunction
|
||||
import org.jetbrains.kotlin.gradle.utils.appendLine
|
||||
import java.io.File
|
||||
import java.security.MessageDigest
|
||||
|
||||
fun Appendable.appendConfigsFromDir(confDir: File) {
|
||||
val files = confDir.listFiles() ?: return
|
||||
@@ -35,20 +36,21 @@ fun ByteArray.toHex(): String {
|
||||
return String(result)
|
||||
}
|
||||
|
||||
fun calculateDirHash(dir: File): String? {
|
||||
fun FileHasher.calculateDirHash(
|
||||
dir: File
|
||||
): String? {
|
||||
if (!dir.isDirectory) return null
|
||||
val md = MessageDigest.getInstance("MD5")
|
||||
|
||||
val hasher = defaultFunction().newHasher()
|
||||
dir.walk()
|
||||
.forEach { file ->
|
||||
md.update(file.toRelativeString(dir).toByteArray())
|
||||
hasher.putString(file.toRelativeString(dir))
|
||||
if (file.isFile) {
|
||||
file.inputStream().use {
|
||||
md.update(it.readBytes())
|
||||
}
|
||||
hasher.putHash(hash(file))
|
||||
}
|
||||
}
|
||||
|
||||
val digest = md.digest()
|
||||
val digest = hasher.hash().toByteArray()
|
||||
|
||||
return digest.toHex()
|
||||
}
|
||||
|
||||
+8
-3
@@ -10,6 +10,7 @@ import org.gradle.api.artifacts.Configuration
|
||||
import org.gradle.api.file.FileSystemOperations
|
||||
import org.gradle.api.provider.Provider
|
||||
import org.gradle.api.tasks.*
|
||||
import org.gradle.internal.hash.FileHasher
|
||||
import org.jetbrains.kotlin.gradle.logging.kotlinInfo
|
||||
import org.jetbrains.kotlin.gradle.plugin.statistics.KotlinBuildStatsService
|
||||
import org.jetbrains.kotlin.gradle.targets.js.calculateDirHash
|
||||
@@ -30,6 +31,10 @@ open class YarnSetupTask : DefaultTask() {
|
||||
|
||||
private val archiveOperations = ArchiveOperationsCompat(project)
|
||||
|
||||
@get:Inject
|
||||
internal open val fileHasher: FileHasher
|
||||
get() = error("Should be injected")
|
||||
|
||||
@get:Inject
|
||||
internal open val fs: FileSystemOperations
|
||||
get() = error("Should be injected")
|
||||
@@ -93,7 +98,7 @@ open class YarnSetupTask : DefaultTask() {
|
||||
val upToDate = destinationHashFile.let { file ->
|
||||
if (file.exists()) {
|
||||
file.useLines {
|
||||
it.single() == (calculateDirHash(destination).also { dirHash = it })
|
||||
it.single() == (fileHasher.calculateDirHash(destination).also { dirHash = it })
|
||||
}
|
||||
} else false
|
||||
}
|
||||
@@ -101,7 +106,7 @@ open class YarnSetupTask : DefaultTask() {
|
||||
val tmpDir = temporaryDir
|
||||
extract(yarnDist, tmpDir) // parent because archive contains name already
|
||||
|
||||
if (upToDate && calculateDirHash(tmpDir.resolve(destination.name))!! == dirHash) return
|
||||
if (upToDate && fileHasher.calculateDirHash(tmpDir.resolve(destination.name))!! == dirHash) return
|
||||
|
||||
if (destination.isDirectory) {
|
||||
destination.deleteRecursively()
|
||||
@@ -115,7 +120,7 @@ open class YarnSetupTask : DefaultTask() {
|
||||
tmpDir.deleteRecursively()
|
||||
|
||||
destinationHashFile.writeText(
|
||||
calculateDirHash(destination)!!
|
||||
fileHasher.calculateDirHash(destination)!!
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user