add allCredentials to emulate credential registration

Fixes #4802

For Ivy integration sbt uses credential task in a peculiar way. 9fa25de84c/main/src/main/scala/sbt/Defaults.scala (L2271-L2275)

This lets the build user put `credential` task in various places, like metabuild or root project, but they would all act as if they were scoped globally. This PR adds `allCredentials` task to emulate the behavior to pass credentials into lm-coursier.
This commit is contained in:
Eugene Yokota 2019-07-11 14:15:33 -04:00
parent d9af39c90c
commit c31e0b6b55
4 changed files with 21 additions and 1 deletions

View File

@ -2364,6 +2364,7 @@ object Classpaths {
),
ivySbt := ivySbt0.value,
ivyModule := { val is = ivySbt.value; new is.Module(moduleSettings.value) },
allCredentials := LMCoursier.allCredentialsTask.value,
transitiveUpdate := transitiveUpdateTask.value,
updateCacheName := {
val binVersion = scalaBinaryVersion.value

View File

@ -376,6 +376,7 @@ object Keys {
val packagedArtifacts = taskKey[Map[Artifact, File]]("Packages all artifacts for publishing and maps the Artifact definition to the generated file.").withRank(CTask)
val publishMavenStyle = settingKey[Boolean]("Configures whether to generate and publish a pom (true) or Ivy file (false).").withRank(BSetting)
val credentials = taskKey[Seq[Credentials]]("The credentials to use for updating and publishing.").withRank(BMinusTask)
val allCredentials = taskKey[Seq[Credentials]]("Aggregated credentials.").withRank(DTask)
val makePom = taskKey[File]("Generates a pom for publishing when publishing Maven-style.").withRank(BPlusTask)
val deliver = taskKey[File]("Generates the Ivy file for publishing to a repository.").withRank(BTask)

View File

@ -204,7 +204,7 @@ object CoursierInputsTasks {
val credentialsTask = Def.task {
val log = streams.value.log
val creds = sbt.Keys.credentials.value
val creds = sbt.Keys.allCredentials.value
.flatMap {
case dc: IvyDirectCredentials => List(dc)
case fc: FileCredentials =>

View File

@ -9,6 +9,7 @@ package sbt
package coursierint
import java.io.File
import scala.collection.mutable
import lmcoursier.definitions.{
Classifier,
Configuration => CConfiguration,
@ -19,11 +20,15 @@ import lmcoursier._
import lmcoursier.credentials.Credentials
import Keys._
import sbt.librarymanagement._
import sbt.librarymanagement.ivy.{ Credentials => IvyCredentials }
import sbt.util.Logger
import sbt.io.syntax._
import xsbti.AppConfiguration
object LMCoursier {
private[sbt] val credentialRegistry: mutable.Map[(String, String), IvyCredentials] =
mutable.Map.empty
def defaultCacheLocation: File =
sys.props.get("sbt.coursier.home") match {
case Some(home) => new File(home).getAbsoluteFile / "cache"
@ -188,4 +193,17 @@ object LMCoursier {
def publicationsSetting(packageConfigs: Seq[(Configuration, CConfiguration)]): Def.Setting[_] = {
csrPublications := CoursierArtifactsTasks.coursierPublicationsTask(packageConfigs: _*).value
}
private[sbt] def registerCredentials(creds: IvyCredentials): Unit = {
val d = IvyCredentials.toDirect(creds)
credentialRegistry((d.host, d.realm)) = d
}
// This emulates Ivy's credential registration which basically keeps mutating global registry
def allCredentialsTask: Def.Initialize[Task[Seq[IvyCredentials]]] = Def.task {
(Keys.credentials in ThisBuild).value foreach registerCredentials
(Keys.credentials in LocalRootProject).value foreach registerCredentials
Keys.credentials.value foreach registerCredentials
credentialRegistry.values.toVector
}
}