diff options
Diffstat (limited to 'project/ShaResolve.scala')
-rw-r--r-- | project/ShaResolve.scala | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/project/ShaResolve.scala b/project/ShaResolve.scala new file mode 100644 index 0000000000..2b7fafd576 --- /dev/null +++ b/project/ShaResolve.scala @@ -0,0 +1,57 @@ +import sbt._ + +import Build._ +import Keys._ +import Project.Initialize +import scala.collection.{ mutable, immutable } + + + + +/** Helpers to resolve SHA artifacts from typesafe repo. */ +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.") + + + def settings: Seq[Setting[_]] = Seq( + pullBinaryLibs in ThisBuild <<= (baseDirectory, streams) map resolveLibs + ) + + def resolveLibs(dir: 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) + } + + def getShaFromShafile(file: File): String = (IO read file split "\\s" headOption) getOrElse error("No SHA found for " + file) + + + def isValidSha(file: File): Boolean = + try (Process(Seq("shasum", "-p", "--check", file.getAbsolutePath), Some(file.getParentFile)).!! contains "OK") + catch { + case t: Exception => false + } + + + 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 pushFile(file: File, uri: String, user: String, pw: String): Unit = { + val url = remote_urlbase + "/" + uri + val sender = dispatch.url(url).PUT.as(user,pw) <<< (file, "application/java-archive") + // TODO - output to logger. + Http(sender >>> System.out) + } +} |