summaryrefslogtreecommitdiff
path: root/project/ShaResolve.scala
blob: 82139ee591fefee3a57d425c5a4dd7372523b593 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import sbt._

import Build._
import Keys._
import Project.Initialize
import scala.collection.{ mutable, immutable }
import scala.collection.parallel.CompositeThrowable



/** 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.")
  val binaryLibCache = SettingKey[File]("binary-lib-cache", "Location of the cache of binary libs for this scala build.")

  def settings: Seq[Setting[_]] = Seq(
    binaryLibCache in ThisBuild := file(System.getProperty("user.home")) / ".sbt" / "cache" / "scala",
    pullBinaryLibs in ThisBuild <<= (baseDirectory, binaryLibCache, streams) map resolveLibs
  )

  def resolveLibs(dir: File, cacheDir: File, s: TaskStreams): Unit = loggingParallelExceptions(s) {
     val files = (dir / "test" / "files" ** "*.desired.sha1") +++ (dir / "lib" ** "*.desired.sha1")
     for {
       (file, name) <- (files x relativeTo(dir)).par
       uri = name.dropRight(13)       
       jar = dir / uri
       if !jar.exists || !isValidSha(file)
       sha = getShaFromShafile(file)
     } pullFile(jar, sha + "/" + uri, cacheDir, s)
  }

  @inline final def loggingParallelExceptions[U](s: TaskStreams)(f: => U): U = try f catch {
    case t: CompositeThrowable =>
      s.log.error("Error during parallel execution, GET READ FOR STACK TRACES!!")
      t.throwables foreach (t2 => s.log.trace(t2))
      throw t
  }

  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, 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 = {
    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)
  }
}