Better handling of optional dependencies

This commit is contained in:
Alexandre Archambault 2017-02-04 16:25:42 +01:00
parent cce332eb55
commit 766ccbf5a9
7 changed files with 73 additions and 10 deletions

View File

@ -351,6 +351,7 @@ object Resolution {
private val mavenScopes = {
val base = Map[String, Set[String]](
"compile" -> Set("compile"),
"optional" -> Set("compile", "optional"),
"provided" -> Set(),
"runtime" -> Set("compile", "runtime"),
"test" -> Set()
@ -463,10 +464,16 @@ object Resolution {
default
else
keepOpt.fold(default) { keep =>
if (keep(config))
// really keeping the from.configuration, with its fallback config part
Seq(dep.copy(configuration = from.configuration))
else
if (keep(config)) {
val depConfig =
if (actualConfig == "optional")
defaultConfiguration
else
// really keeping the from.configuration, with its fallback config part
from.configuration
Seq(dep.copy(configuration = depConfig))
} else
Nil
}
}

View File

@ -287,12 +287,12 @@ final case class MavenRepository(
for {
str <- fetch(projectArtifact(module, version, versioningValue))
rawListFilesPage <- fetch(artifactFor(listFilesUrl))
proj <- EitherT(F.point[String \/ Project](parseRawPom(str)))
proj0 <- EitherT(F.point[String \/ Project](parseRawPom(str)))
} yield {
val files = WebPage.listFiles(listFilesUrl, rawListFilesPage)
val versioning = proj
val versioning = proj0
.snapshotVersioning
.flatMap(versioning =>
mavenVersioning(versioning, "", "")
@ -300,7 +300,7 @@ final case class MavenRepository(
val prefix = s"${module.name}-${versioning.getOrElse(version)}"
val packagingTpeMap = proj.packagingOpt
val packagingTpeMap = proj0.packagingOpt
.map { packaging =>
(MavenSource.typeDefaultClassifier(packaging), MavenSource.typeExtension(packaging)) -> packaging
}
@ -323,9 +323,14 @@ final case class MavenRepository(
)
}
val proj = Pom.addOptionalDependenciesInConfig(
proj0.copy(configurations = defaultConfigurations),
Set("", "compile"),
"optional"
)
proj.copy(
actualVersionOpt = Some(version),
configurations = defaultConfigurations,
publications = foundPublications
)
}

View File

@ -451,4 +451,24 @@ object Pom {
} yield modVers :+ modVer
}
}
def addOptionalDependenciesInConfig(
proj: Project,
fromConfigs: Set[String],
optionalConfig: String
): Project = {
val optionalDeps = proj.dependencies.collect {
case (conf, dep) if dep.optional && fromConfigs(conf) =>
optionalConfig -> dep.copy(optional = false)
}
val configurations = proj.configurations +
(optionalConfig -> (proj.configurations.getOrElse(optionalConfig, Nil) ++ fromConfigs.filter(_.nonEmpty)).distinct)
proj.copy(
configurations = configurations,
dependencies = proj.dependencies ++ optionalDeps
)
}
}

View File

@ -1,6 +1,6 @@
package coursier
import java.io.{ OutputStreamWriter, File }
import java.io.{ File, InputStream, OutputStreamWriter }
import java.net.URL
import java.util.concurrent.{ ExecutorService, Executors }
@ -123,10 +123,14 @@ object Tasks {
allDependencies <- allDependenciesTask
} yield {
val configMap = configurations
.map { cfg => cfg.name -> cfg.extendsConfigs.map(_.name) }
.toMap
FromSbt.project(
projId,
allDependencies,
configurations.map { cfg => cfg.name -> cfg.extendsConfigs.map(_.name) }.toMap,
configMap,
sv,
sbv
)

View File

@ -0,0 +1,5 @@
org.scala-lang:scala-compiler:2.11.8:compile
org.scala-lang:scala-library:2.11.8:compile
org.scala-lang:scala-reflect:2.11.8:compile
org.scala-lang.modules:scala-parser-combinators_2.11:1.0.4:compile
org.scala-lang.modules:scala-xml_2.11:1.0.4:compile

View File

@ -0,0 +1,6 @@
jline:jline:2.12.1:compile
org.scala-lang:scala-compiler:2.11.8:optional
org.scala-lang:scala-library:2.11.8:compile
org.scala-lang:scala-reflect:2.11.8:compile
org.scala-lang.modules:scala-parser-combinators_2.11:1.0.4:compile
org.scala-lang.modules:scala-xml_2.11:1.0.4:compile

View File

@ -465,6 +465,22 @@ object CentralTests extends TestSuite {
"0.5.0"
)
}
'scalaCompilerJLine - {
// optional should bring jline
* - resolutionCheck(
Module("org.scala-lang", "scala-compiler"),
"2.11.8"
)
* - resolutionCheck(
Module("org.scala-lang", "scala-compiler"),
"2.11.8",
configuration = "optional"
)
}
}
}