Skip to content

Commit

Permalink
naive JAR hashing
Browse files Browse the repository at this point in the history
We need a nightly containing sbt/sbt#5344
if we want children projects to be insensitive to repackaging.
  • Loading branch information
github-brice-jaglin committed May 26, 2020
1 parent 48e398a commit ea6a105
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
37 changes: 37 additions & 0 deletions src/main/scala/sbtmemoize/MemoizePlugin.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,46 @@
package sbtmemoize

import sbt.Keys._
import sbt._
import sjsonnew.support.murmurhash.Hasher
import sjsonnew.{Builder, HashWriter}

object MemoizePlugin extends AutoPlugin {

override def trigger = noTrigger

object autoImport {
val memoizeCacheKey =
taskKey[Int]("Compute the cache key for the current state of the project")
}

import autoImport._

override def projectSettings: Seq[Def.Setting[_]] =
Seq(Compile, Test).flatMap(inConfig(_)(memoizeConfigSettings))

lazy val memoizeConfigSettings: Seq[Def.Setting[_]] =
Seq(
memoizeCacheKey := {
// like FileInfo.hash but without the path (which is environment-specific)
val hashWriter: HashWriter[Seq[File]] = new HashWriter[Seq[File]] {
override def write[J](obj: Seq[File], builder: Builder[J]): Unit = {
import sjsonnew.BasicJsonProtocol._
builder.beginArray()
obj.foreach { file =>
builder.beginObject()
builder.addField("hash", file.hash)
builder.endObject()
}
builder.endArray()
}
}

val srcs = sources.value
val deps = dependencyClasspathAsJars.value.map(_.data)

Hasher.hash(srcs ++ deps)(hashWriter).get
}
)

}
3 changes: 2 additions & 1 deletion src/sbt-test/sbt-memoize/simple/test
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
> compile
> compile
> show memoizeCacheKey

0 comments on commit ea6a105

Please sign in to comment.