try last 5 commits to look for remote cache

In case there are a few local commits ahead of the remote cache, this would still grab the close point in the history to resume the build.
This commit is contained in:
Eugene Yokota 2020-06-10 12:55:30 -04:00
parent 585f8399ba
commit 6dd39e7ab8
1 changed files with 56 additions and 29 deletions

View File

@ -21,6 +21,7 @@ import sbt.internal.librarymanagement._
import sbt.io.IO import sbt.io.IO
import sbt.io.syntax._ import sbt.io.syntax._
import sbt.internal.inc.JarUtils import sbt.internal.inc.JarUtils
import sbt.util.Logger
object RemoteCache { object RemoteCache {
final val cachedCompileClassifier = "cached-compile" final val cachedCompileClassifier = "cached-compile"
@ -28,9 +29,17 @@ object RemoteCache {
def gitCommitId: String = def gitCommitId: String =
scala.sys.process.Process("git rev-parse --short HEAD").!!.trim scala.sys.process.Process("git rev-parse --short HEAD").!!.trim
def gitCommitIds(n: Int): List[String] =
scala.sys.process
.Process("git log -n " + n.toString + " --format=%H")
.!!
.linesIterator
.toList
.map(_.take(10))
lazy val globalSettings: Seq[Def.Setting[_]] = Seq( lazy val globalSettings: Seq[Def.Setting[_]] = Seq(
remoteCacheId := gitCommitId, remoteCacheId := gitCommitId,
remoteCacheIdCandidates := gitCommitIds(5),
pushRemoteCacheTo :== None, pushRemoteCacheTo :== None,
) )
@ -40,7 +49,7 @@ object RemoteCache {
val m = moduleName.value val m = moduleName.value
val id = remoteCacheId.value val id = remoteCacheId.value
val c = (projectID / crossVersion).value val c = (projectID / crossVersion).value
val v = s"0.0.0-$id" val v = toVersion(id)
ModuleID(o, m, v).cross(c) ModuleID(o, m, v).cross(c)
}, },
pushRemoteCacheConfiguration / publishMavenStyle := true, pushRemoteCacheConfiguration / publishMavenStyle := true,
@ -71,29 +80,34 @@ object RemoteCache {
val is = (pushRemoteCache / ivySbt).value val is = (pushRemoteCache / ivySbt).value
val t = crossTarget.value / "cache-download" val t = crossTarget.value / "cache-download"
val p = remoteCacheProjectId.value val p = remoteCacheProjectId.value
val id = remoteCacheId.value val ids = remoteCacheIdCandidates.value
val compileAf = (Compile / compileAnalysisFile).value val compileAf = (Compile / compileAnalysisFile).value
val compileOutput = (Compile / classDirectory).value val compileOutput = (Compile / classDirectory).value
val testAf = (Test / compileAnalysisFile).value val testAf = (Test / compileAnalysisFile).value
val testOutput = (Test / classDirectory).value val testOutput = (Test / classDirectory).value
val testStreams = (Test / test / streams).value val testStreams = (Test / test / streams).value
val testResult = Defaults.succeededFile(testStreams.cacheDirectory) val testResult = Defaults.succeededFile(testStreams.cacheDirectory)
var found = false
val deps = Vector(p.classifier(cachedCompileClassifier), p.classifier(cachedTestClasifier)) ids foreach {
val mconfig = dummyModule(smi, deps) id: String =>
val m = new is.Module(mconfig) val v = toVersion(id)
dr.retrieve(m, t, s.log) match { val modId = p.withRevision(v)
case Right(xs0) => if (found) ()
val xs = xs0.distinct else
xs.find(_.toString.endsWith(s"$id-$cachedCompileClassifier.jar")) foreach { jar: File => pullFromMavenRepo0(modId, smi, is, dr, t, s.log) match {
extractCache(jar, compileOutput, compileAf, None) case Right(xs0) =>
} val xs = xs0.distinct
xs.find(_.toString.endsWith(s"$id-$cachedTestClasifier.jar")) foreach { jar: File => xs.find(_.toString.endsWith(s"$v-$cachedCompileClassifier.jar")) foreach {
extractCache(jar, testOutput, testAf, Some(testResult)) jar: File =>
} extractCache(jar, compileOutput, compileAf, None)
() }
case Left(unresolvedWarning) => xs.find(_.toString.endsWith(s"$v-$cachedTestClasifier.jar")) foreach { jar: File =>
s.log.info(s"remote cache not found for ${id}") extractCache(jar, testOutput, testAf, Some(testResult))
}
found = true
case Left(unresolvedWarning) =>
s.log.info(s"remote cache not found for ${v}")
}
} }
}, },
remoteCachePom := { remoteCachePom := {
@ -170,6 +184,30 @@ object RemoteCache {
) )
) )
private def toVersion(v: String): String = s"0.0.0-$v"
private def pullFromMavenRepo0(
modId: ModuleID,
smi: Option[ScalaModuleInfo],
is: IvySbt,
dr: DependencyResolution,
cacheDir: File,
log: Logger
): Either[UnresolvedWarning, Vector[File]] = {
def dummyModule(deps: Vector[ModuleID]): ModuleDescriptorConfiguration = {
val module = ModuleID("com.example.temp", "fake", "0.1.0-SNAPSHOT")
val info = ModuleInfo("fake", "", None, None, Vector(), "", None, None, Vector())
ModuleDescriptorConfiguration(module, info)
.withScalaModuleInfo(smi)
.withDependencies(deps)
}
val deps =
Vector(modId.classifier(cachedCompileClassifier), modId.classifier(cachedTestClasifier))
val mconfig = dummyModule(deps)
val m = new is.Module(mconfig)
dr.retrieve(m, cacheDir, log)
}
private def extractCache( private def extractCache(
jar: File, jar: File,
output: File, output: File,
@ -193,17 +231,6 @@ object RemoteCache {
() ()
} }
private def dummyModule(
smi: Option[ScalaModuleInfo],
deps: Vector[ModuleID]
): ModuleDescriptorConfiguration = {
val module = ModuleID("com.example.temp", "fake", "0.1.0-SNAPSHOT")
val info = ModuleInfo("fake", "", None, None, Vector(), "", None, None, Vector())
ModuleDescriptorConfiguration(module, info)
.withScalaModuleInfo(smi)
.withDependencies(deps)
}
private def defaultArtifactTasks: Seq[TaskKey[File]] = private def defaultArtifactTasks: Seq[TaskKey[File]] =
Seq(remoteCachePom, Compile / packageCache, Test / packageCache) Seq(remoteCachePom, Compile / packageCache, Test / packageCache)