From 60a7d3796fe543b2eedc023236a8cbc01b90cf97 Mon Sep 17 00:00:00 2001 From: Alexandre Archambault Date: Wed, 17 Jun 2015 20:22:38 +0200 Subject: [PATCH] Remove dependency on http4s --- .../src/main/scala/coursier/core/Remote.scala | 67 ++++++++++++------- .../coursier/core/compatibility/package.scala | 3 - .../scala/coursier/repository/package.scala | 8 +-- .../core/compatibility/DateTime.scala | 0 project/Coursier.scala | 2 +- 5 files changed, 47 insertions(+), 33 deletions(-) rename {core-js => core}/src/main/scala/coursier/core/compatibility/DateTime.scala (100%) diff --git a/core-jvm/src/main/scala/coursier/core/Remote.scala b/core-jvm/src/main/scala/coursier/core/Remote.scala index 03110d01d..79c6c56c7 100644 --- a/core-jvm/src/main/scala/coursier/core/Remote.scala +++ b/core-jvm/src/main/scala/coursier/core/Remote.scala @@ -4,8 +4,6 @@ package core import java.io._ import java.net.URL -import org.http4s.{EntityDecoder, Status, Uri} - import scala.annotation.tailrec import scala.io.Codec import scalaz._, Scalaz._ @@ -18,7 +16,7 @@ trait ArtifactDownloaderLogger { def downloadedArtifact(url: String, success: Boolean): Unit } -case class ArtifactDownloader(root: Uri, cache: File, logger: Option[ArtifactDownloaderLogger] = None) { +case class ArtifactDownloader(root: String, cache: File, logger: Option[ArtifactDownloaderLogger] = None) { var bufferSize = 1024*1024 def artifact(module: Module, @@ -52,7 +50,7 @@ case class ArtifactDownloader(root: Uri, cache: File, logger: Option[ArtifactDow // - what if someone is trying to write this file at the same time? (no locking of any kind yet) // - ... - val urlStr = (root resolve Uri(path = relPath.mkString("./", "/", ""))).renderString + val urlStr = root + relPath.mkString("/") Task { try { @@ -107,8 +105,39 @@ trait RemoteLogger { def puttingInCache(f: File): Unit } -case class Remote(root: Uri, cache: Option[File] = None, logger: Option[RemoteLogger] = None) extends Repository { - lazy val client = org.http4s.client.blaze.defaultClient +object Remote { + + def readFullySync(is: InputStream) = { + val buffer = new ByteArrayOutputStream() + val data = Array.ofDim[Byte](16384) + + var nRead = is.read(data, 0, data.length) + while (nRead != -1) { + buffer.write(data, 0, nRead) + nRead = is.read(data, 0, data.length) + } + + buffer.flush() + buffer.toByteArray + } + + def readFully(is: => InputStream) = + Task { + \/.fromTryCatchNonFatal { + val is0 = is + val b = + try readFullySync(is) + finally is0.close() + + new String(b, "UTF-8") + } .leftMap(_.getMessage) + } + +} + +case class Remote(root: String, + cache: Option[File] = None, + logger: Option[RemoteLogger] = None) extends Repository { def find(module: Module, cachePolicy: CachePolicy): EitherT[Task, String, Project] = { @@ -141,19 +170,13 @@ case class Remote(root: Uri, cache: Option[File] = None, logger: Option[RemoteLo } def remote = { - val uri = root resolve Uri(path = relPath.mkString("./", "/", "")) - val log = Task(logger.foreach(_.downloading(uri.renderString))) - val get = log.flatMap(_ => client(uri)) + val urlStr = root + relPath.mkString("/") + val url = new URL(urlStr) - get.flatMap{ resp => - val success = resp.status == Status.Ok - logger.foreach(_.downloaded(uri.renderString, success)) + def log = Task(logger.foreach(_.downloading(urlStr))) + def get = Remote.readFully(url.openStream()) - if (success) - EntityDecoder.text.decode(resp).run.map(_.leftMap(_.sanitized)) - else - Task.now(-\/(s"Unhandled or bad status ${resp.status.code} from repository (${resp.status.code} ${resp.status.reason})")) - } + log.flatMap(_ => get) } def save(s: String) = { @@ -199,14 +222,10 @@ case class Remote(root: Uri, cache: Option[File] = None, logger: Option[RemoteLo } def remote = { - val respTask = client(root resolve Uri(path = relPath.mkString("./", "/", ""))) + val urlStr = root + relPath.mkString("/") + val url = new URL(urlStr) - respTask.flatMap{ resp => - if (resp.status == Status.Ok) - EntityDecoder.text.decode(resp).run.map(_.leftMap(_.sanitized)) - else - Task.now(-\/(s"Unhandled or bad status ${resp.status.code} from repository (${resp.status.code} ${resp.status.reason})")) - } + Remote.readFully(url.openStream()) } def save(s: String) = { diff --git a/core-jvm/src/main/scala/coursier/core/compatibility/package.scala b/core-jvm/src/main/scala/coursier/core/compatibility/package.scala index 53760b155..a1e051060 100644 --- a/core-jvm/src/main/scala/coursier/core/compatibility/package.scala +++ b/core-jvm/src/main/scala/coursier/core/compatibility/package.scala @@ -2,9 +2,6 @@ package coursier.core package object compatibility { - type DateTime = org.http4s.DateTime - val DateTime: org.http4s.DateTime.type = org.http4s.DateTime - implicit class RichChar(val c: Char) extends AnyVal { def letterOrDigit = c.isLetterOrDigit def letter = c.isLetter diff --git a/core-jvm/src/main/scala/coursier/repository/package.scala b/core-jvm/src/main/scala/coursier/repository/package.scala index 8c2da5da3..59d758a39 100644 --- a/core-jvm/src/main/scala/coursier/repository/package.scala +++ b/core-jvm/src/main/scala/coursier/repository/package.scala @@ -1,15 +1,13 @@ package coursier -import org.http4s.Http4s._ - package object repository { type Remote = core.Remote val Remote: core.Remote.type = core.Remote - val mavenCentral = Remote(uri("https://repo1.maven.org/maven2/")) + val mavenCentral = Remote("https://repo1.maven.org/maven2/") - val sonatypeReleases = Remote(uri("https://oss.sonatype.org/content/repositories/releases/")) - val sonatypeSnapshots = Remote(uri("https://oss.sonatype.org/content/repositories/snapshots/")) + val sonatypeReleases = Remote("https://oss.sonatype.org/content/repositories/releases/") + val sonatypeSnapshots = Remote("https://oss.sonatype.org/content/repositories/snapshots/") } diff --git a/core-js/src/main/scala/coursier/core/compatibility/DateTime.scala b/core/src/main/scala/coursier/core/compatibility/DateTime.scala similarity index 100% rename from core-js/src/main/scala/coursier/core/compatibility/DateTime.scala rename to core/src/main/scala/coursier/core/compatibility/DateTime.scala diff --git a/project/Coursier.scala b/project/Coursier.scala index abe820738..d5d6f6ad3 100644 --- a/project/Coursier.scala +++ b/project/Coursier.scala @@ -72,7 +72,7 @@ object CoursierBuild extends Build { .settings(commonSettings ++ commonCoreSettings: _*) .settings( libraryDependencies ++= Seq( - "org.http4s" %% "http4s-blazeclient" % "0.7.0", + "org.scalaz" %% "scalaz-concurrent" % "7.1.2", "com.lihaoyi" %% "utest" % "0.3.0" % "test" ) ++ { if (scalaVersion.value.startsWith("2.10.")) Seq()