Minor fix in fallback dependencies in plugin

This commit is contained in:
Alexandre Archambault 2016-10-30 20:06:41 +01:00
parent 92a8ea2ab5
commit e49fbef4e4
5 changed files with 99 additions and 7 deletions

View File

@ -1,9 +1,68 @@
package coursier
import java.net.URL
import java.io.{ File, FileNotFoundException, IOException }
import java.net.{ HttpURLConnection, URL, URLConnection }
import scalaz.{ EitherT, Monad }
object FallbackDependenciesRepository {
def exists(url: URL): Boolean = {
// Sometimes HEAD attempts fail even though standard GETs are fine.
// E.g. https://github.com/NetLogo/NetLogo/releases/download/5.3.1/NetLogo.jar
// returning 403s. Hence the second attempt below.
val firstAttemptOpt = url.getProtocol match {
case "file" =>
Some(new File(url.getPath).exists()) // FIXME Escaping / de-escaping needed here?
case "http" | "https" =>
// HEAD request attempt, adapted from http://stackoverflow.com/questions/22541629/android-how-can-i-make-an-http-head-request/22545275#22545275
var conn: HttpURLConnection = null
try {
conn = url
.openConnection()
.asInstanceOf[HttpURLConnection]
conn.setRequestMethod("HEAD")
conn.getInputStream.close()
Some(true)
} catch {
case _: FileNotFoundException =>
Some(false)
case _: IOException => // error other than not found
None
} finally {
if (conn != null)
conn.disconnect()
}
case _ =>
None
}
firstAttemptOpt.getOrElse {
var conn: URLConnection = null
try {
conn = url.openConnection()
// NOT setting request type to HEAD here.
conn.getInputStream.close()
true
} catch {
case _: IOException =>
false
} finally {
conn match {
case conn0: HttpURLConnection => conn0.disconnect()
case _ =>
}
}
}
}
}
case class FallbackDependenciesRepository(
fallbacks: Map[(Module, String), (URL, Boolean)]
) extends Repository {
@ -44,12 +103,10 @@ case class FallbackDependenciesRepository(
else {
val (dirUrlStr, fileName) = urlStr.splitAt(idx + 1)
fetch(Artifact(dirUrlStr, Map.empty, Map.empty, Attributes("", ""), changing = true, None)).flatMap { listing =>
val files = coursier.core.compatibility.listWebPageFiles(dirUrlStr, listing)
if (files.contains(fileName)) {
// Not sure F.point will make that run like Task.apply would have
// if F = Task
EitherT.right(F.point(FallbackDependenciesRepository.exists(url))).flatMap { exists =>
if (exists) {
val proj = Project(
module,
version,

View File

@ -0,0 +1,10 @@
scalaVersion := "2.11.8"
coursierCachePolicies := {
if (sys.props("os.name").startsWith("Windows"))
coursierCachePolicies.value
else
Seq(coursier.CachePolicy.ForceDownload)
}
libraryDependencies += "ccl.northwestern.edu" % "netlogo" % "5.3.1" % "provided" from s"https://github.com/NetLogo/NetLogo/releases/download/5.3.1/NetLogo.jar"

View File

@ -0,0 +1,11 @@
{
val pluginVersion = sys.props.getOrElse(
"plugin.version",
throw new RuntimeException(
"""|The system property 'plugin.version' is not defined.
|Specify this property using the scriptedLaunchOpts -D.""".stripMargin
)
)
addSbtPlugin("io.get-coursier" % "sbt-coursier" % pluginVersion)
}

View File

@ -0,0 +1,11 @@
import java.io.File
import java.nio.file.Files
object Main extends App {
// Not using directly the NetLogo 5.x lib, which seems to depend on Scala 2.9
// Can't find a way to check that NetLogo.jar is in the classpath
// These don't work, even with fork := true:
// assert(Thread.currentThread.getContextClassLoader.getResource("org/nlogo/nvm/Task.class") != null)
// Thread.currentThread.getContextClassLoader.getResource("org/nlogo/nvm/Task.class")
Files.write(new File("output").toPath, "OK".getBytes("UTF-8"))
}

View File

@ -0,0 +1,3 @@
$ delete output
> run
$ exists output