From 16ad68fa3aa5d1ccd55865c3d06fec142cf9d630 Mon Sep 17 00:00:00 2001 From: Josh Suereth Date: Thu, 8 Dec 2011 14:05:56 -0500 Subject: Added local cache for project jars. .desired.sha1 files now resolve into a local repository before being copied into the main repo. If the local repository exists and has a file, then there is no download necessary. --- project/ShaResolve.scala | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) (limited to 'project/ShaResolve.scala') diff --git a/project/ShaResolve.scala b/project/ShaResolve.scala index 2b7fafd576..4d9579b07d 100644 --- a/project/ShaResolve.scala +++ b/project/ShaResolve.scala @@ -12,23 +12,24 @@ import scala.collection.{ mutable, immutable } object ShaResolve { import dispatch.{Http,url} val remote_urlbase="http://typesafe.artifactoryonline.com/typesafe/scala-sha-bootstrap/org/scala-lang/bootstrap" - + val pullBinaryLibs = TaskKey[Unit]("pull-binary-libs", "Pulls binary libs by the SHA key.") val pushBinaryLibs = TaskKey[Unit]("push-binary-libs", "Pushes binary libs whose SHA has changed.") - + val binaryLibCache = SettingKey[File]("binary-lib-cache", "Location of the cache of binary libs for this scala build.") def settings: Seq[Setting[_]] = Seq( - pullBinaryLibs in ThisBuild <<= (baseDirectory, streams) map resolveLibs + binaryLibCache in ThisBuild := file(System.getProperty("user.home")) / ".sbt" / "cache" / "scala", + pullBinaryLibs in ThisBuild <<= (baseDirectory, binaryLibCache, streams) map resolveLibs ) - def resolveLibs(dir: File, s: TaskStreams): Unit = { + def resolveLibs(dir: File, cacheDir: File, s: TaskStreams): Unit = { for { (file, name) <- dir ** "*.desired.sha1" x relativeTo(dir) uri = name.dropRight(13) jar = dir / uri if !jar.exists || !isValidSha(file) sha = getShaFromShafile(file) - } pullFile(jar, sha + "/" + uri, s) + } pullFile(jar, sha + "/" + uri, cacheDir, s) } def getShaFromShafile(file: File): String = (IO read file split "\\s" headOption) getOrElse error("No SHA found for " + file) @@ -41,11 +42,18 @@ object ShaResolve { } - def pullFile(file: File, uri: String, s: TaskStreams): Unit = { - val url = remote_urlbase + "/" + uri - val fous = new java.io.FileOutputStream(file) - s.log.info("Pulling [" + url + "] to [" + file + "]") - try Http(dispatch.url(url) >>> fous) finally fous.close() + def pullFile(file: File, uri: String, cacheDir: File, s: TaskStreams): Unit = { + val cachedFile = cacheDir / uri + if (!cachedFile.exists) { + // Ensure the directory for the cache exists. + cachedFile.getParentFile.mkdirs() + val url = remote_urlbase + "/" + uri + val fous = new java.io.FileOutputStream(cachedFile) + s.log.info("Pulling [" + cachedFile + "] to cache") + try Http(dispatch.url(url) >>> fous) finally fous.close() + } + s.log.info("Pulling [" + file + "] from local cache") + IO.copyFile(cachedFile, file) } def pushFile(file: File, uri: String, user: String, pw: String): Unit = { -- cgit v1.2.3