Skip to content

Commit

Permalink
Stable md5 digest for bloop projects
Browse files Browse the repository at this point in the history
  • Loading branch information
oyvindberg committed May 30, 2023
1 parent 12f1595 commit 74800ac
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
package bleep
package packaging

import java.security.MessageDigest
import java.util
Expand Down Expand Up @@ -31,13 +30,16 @@ object Checksums {
Map(in) ++ res
}

def compute(content: Array[Byte], algorithm: Algorithm): Digest = {
def compute(algorithm: Algorithm)(f: MessageDigest => Unit): Digest = {
val md = MessageDigest.getInstance(algorithm.name)
md.reset()
md.update(content)
f(md)
Digest(md.digest)
}

def compute(content: Array[Byte], algorithm: Algorithm): Digest =
compute(algorithm)(_.update(content))

// byte to hex string converter
val Chars = Array[Char]('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f')

Expand Down
17 changes: 11 additions & 6 deletions bleep-core/src/scala/bleep/GenBloopFiles.scala
Original file line number Diff line number Diff line change
Expand Up @@ -108,22 +108,27 @@ object GenBloopFiles {
case class SyncToDiskWith(next: GenBloopFiles) extends GenBloopFiles {
override def apply(pre: Prebootstrapped, resolver: CoursierResolver, build: model.Build): Files = {

val currentHash = List(
build.$version,
build.explodedProjects.toVector.sortBy(_._1)
).hashCode().toString
val currentHash: Checksums.Digest =
Checksums.compute(Checksums.Algorithm.Md5) { md =>
md.update(build.$version.value.getBytes(UTF_8))
build.explodedProjects.toVector.sortBy { case (cn, _) => cn }.foreach { case (cn, p) =>
import io.circe.syntax.*
md.update(cn.value.getBytes(UTF_8))
md.update(p.asJson.noSpacesSortKeys.getBytes(UTF_8))
}
}

val oldHash = Try(Files.readString(pre.buildPaths.digestFile, UTF_8)).toOption

if (oldHash.contains(currentHash)) {
if (oldHash.contains(currentHash.hexString)) {
pre.logger.debug(s"${pre.buildPaths.bleepBloopDir} up to date")
next(pre, resolver, build)
} else {
pre.logger.warn(s"Refreshing ${pre.buildPaths.bleepBloopDir}...")

val bloopFiles = next(pre, resolver, build)

val fileMap = encodedFiles(pre.buildPaths, bloopFiles).updated(pre.buildPaths.digestFile, currentHash)
val fileMap = encodedFiles(pre.buildPaths, bloopFiles).updated(pre.buildPaths.digestFile, currentHash.hexString)

FileSync
.syncPaths(
Expand Down
1 change: 1 addition & 0 deletions bleep-tests/src/scala/bleep/packaging/ChecksumTests.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package bleep.packaging

import bleep.Checksums
import org.scalactic.TripleEqualsSupport
import org.scalatest.funsuite.AnyFunSuite

Expand Down
2 changes: 1 addition & 1 deletion liberated/sbt-ci-release

0 comments on commit 74800ac

Please sign in to comment.