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