Get credentials from SBT key `credentials` if available

This commit is contained in:
Alexandre Archambault 2016-05-22 21:43:06 +02:00
parent 84d75655fa
commit 3045ebbfd0
No known key found for this signature in database
GPG Key ID: 14640A6839C263A9
7 changed files with 76 additions and 4 deletions

View File

@ -19,6 +19,7 @@ object CoursierPlugin extends AutoPlugin {
val coursierSourceRepositories = Keys.coursierSourceRepositories
val coursierResolvers = Keys.coursierResolvers
val coursierSbtResolvers = Keys.coursierSbtResolvers
val coursierUseSbtCredentials = Keys.coursierUseSbtCredentials
val coursierCredentials = Keys.coursierCredentials
val coursierFallbackDependencies = Keys.coursierFallbackDependencies
val coursierCache = Keys.coursierCache
@ -62,6 +63,7 @@ object CoursierPlugin extends AutoPlugin {
coursierSourceRepositories := Nil,
coursierResolvers <<= Tasks.coursierResolversTask,
coursierSbtResolvers <<= externalResolvers in updateSbtClassifiers,
coursierUseSbtCredentials := true,
coursierCredentials := Map.empty,
coursierFallbackDependencies <<= Tasks.coursierFallbackDependenciesTask,
coursierCache := Cache.default,

View File

@ -18,6 +18,7 @@ object Keys {
val coursierSourceRepositories = SettingKey[Seq[File]]("coursier-source-repositories")
val coursierResolvers = TaskKey[Seq[Resolver]]("coursier-resolvers")
val coursierSbtResolvers = TaskKey[Seq[Resolver]]("coursier-sbt-resolvers")
val coursierUseSbtCredentials = SettingKey[Boolean]("coursier-use-sbt-credentials")
val coursierCredentials = TaskKey[Map[String, Credentials]]("coursier-credentials")
val coursierCache = SettingKey[File]("coursier-cache")

View File

@ -5,7 +5,7 @@ import java.net.URL
import java.nio.file.Files
import java.util.concurrent.Executors
import coursier.core.Publication
import coursier.core.{ Authentication, Publication }
import coursier.ivy.IvyRepository
import coursier.Keys._
import coursier.Structure._
@ -19,6 +19,7 @@ import sbt.Keys._
import scala.collection.mutable
import scala.collection.JavaConverters._
import scala.util.Try
import scalaz.{ \/-, -\/ }
import scalaz.concurrent.{ Task, Strategy }
@ -371,7 +372,18 @@ object Tasks {
"ivy.home" -> (new File(sys.props("user.home")).toURI.getPath + ".ivy2")
) ++ sys.props
val credentials = coursierCredentials.value
val useSbtCredentials = coursierUseSbtCredentials.value
val authenticationByHost =
if (useSbtCredentials) {
val cred = sbt.Keys.credentials.value.map(sbt.Credentials.toDirect)
cred.map { c =>
c.host -> Authentication(c.userName, c.passwd)
}.toMap
} else
Map.empty[String, Authentication]
val authenticationByRepositoryId = coursierCredentials.value.mapValues(_.authentication)
val sourceRepositories0 = sourceRepositories.map {
base =>
@ -392,6 +404,36 @@ object Tasks {
)
}
def withAuthenticationByHost(repo: Repository, credentials: Map[String, Authentication]): Repository = {
def httpHost(s: String) =
if (s.startsWith("http://") || s.startsWith("https://"))
Try(Cache.url(s).getHost).toOption
else
None
repo match {
case m: MavenRepository =>
if (m.authentication.isEmpty)
httpHost(m.root).flatMap(credentials.get).fold(m) { auth =>
m.copy(authentication = Some(auth))
}
else
m
case i: IvyRepository =>
if (i.authentication.isEmpty) {
val base = i.pattern.takeWhile(c => c != '[' && c != '(' && c != '$')
httpHost(base).flatMap(credentials.get).fold(i) { auth =>
i.copy(authentication = Some(auth))
}
} else
i
case _ =>
repo
}
}
val repositories =
Seq(globalPluginsRepo, interProjectRepo) ++
sourceRepositories0 ++
@ -400,9 +442,9 @@ object Tasks {
resolver,
ivyProperties,
log,
credentials.get(resolver.name).map(_.authentication)
authenticationByRepositoryId.get(resolver.name)
)
} ++
}.map(withAuthenticationByHost(_, authenticationByHost)) ++
fallbackDependenciesRepositories
def resolution = {

View File

@ -0,0 +1,14 @@
scalaVersion := "2.11.8"
resolvers += "authenticated" at "http://localhost:8080"
credentials += Credentials("", "localhost", "user", "pass")
coursierCachePolicies := {
if (sys.props("os.name").startsWith("Windows"))
coursierCachePolicies.value
else
Seq(coursier.CachePolicy.ForceDownload)
}
libraryDependencies += "com.abc" % "test" % "0.1"

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 @@
object Main extends App

View File

@ -0,0 +1 @@
> coursierResolution