From 1951da25bb960f98ef3568d1d7478172503c58d7 Mon Sep 17 00:00:00 2001 From: andrea Date: Wed, 24 Oct 2018 15:14:43 +0100 Subject: [PATCH] rework again configurations management --- .../src/main/scala/coursier/SbtBootJars.scala | 18 +++++++++++ .../CoursierDependencyResolution.scala | 31 ++++++++++++------- .../coursier/ResolutionSpec.scala | 25 +++++++-------- .../lmScriptedTest/use-sbt-in-meta/build.sbt | 25 +++++++++++++++ .../project/Dependencies.scala | 6 ++++ .../lmScriptedTest/use-sbt-in-meta/test | 1 + 6 files changed, 81 insertions(+), 25 deletions(-) create mode 100644 coursier/src/main/scala/coursier/SbtBootJars.scala create mode 100644 scripted-test/src/sbt-test/lmScriptedTest/use-sbt-in-meta/build.sbt create mode 100644 scripted-test/src/sbt-test/lmScriptedTest/use-sbt-in-meta/project/Dependencies.scala create mode 100755 scripted-test/src/sbt-test/lmScriptedTest/use-sbt-in-meta/test diff --git a/coursier/src/main/scala/coursier/SbtBootJars.scala b/coursier/src/main/scala/coursier/SbtBootJars.scala new file mode 100644 index 000000000..1d9df09cd --- /dev/null +++ b/coursier/src/main/scala/coursier/SbtBootJars.scala @@ -0,0 +1,18 @@ +package coursier + +import java.io.File + +object SbtBootJars { + def apply( + scalaOrg: String, + scalaVersion: String, + jars: Seq[File] + ): Map[(Module, String), File] = + jars.collect { + case jar if jar.getName.endsWith(".jar") => + val name = jar.getName.stripSuffix(".jar") + val mod = Module(scalaOrg, name) + + (mod, scalaVersion) -> jar + }.toMap +} diff --git a/coursier/src/main/scala/sbt/librarymanagement/coursier/CoursierDependencyResolution.scala b/coursier/src/main/scala/sbt/librarymanagement/coursier/CoursierDependencyResolution.scala index 04ec6e49c..5ac543f9e 100644 --- a/coursier/src/main/scala/sbt/librarymanagement/coursier/CoursierDependencyResolution.scala +++ b/coursier/src/main/scala/sbt/librarymanagement/coursier/CoursierDependencyResolution.scala @@ -156,7 +156,7 @@ private[sbt] class CoursierDependencyResolution(coursierConfiguration: CoursierC } val dependencies = module.directDependencies.map(toCoursierDependency).flatten.toSet - val start = Resolution(dependencies) + val start = Resolution(dependencies.map(_._1)) val authentication = None // TODO: get correct value val ivyConfiguration = ivyProperties // TODO: is it enough? @@ -191,7 +191,10 @@ private[sbt] class CoursierDependencyResolution(coursierConfiguration: CoursierC .unsafeRun() .toMap - toUpdateReport(resolution, module0.configurations, localArtifacts, log) + toUpdateReport(resolution, + (module0.configurations ++ dependencies.map(_._2)).distinct, + localArtifacts, + log) } if (resolution.isDone && @@ -221,7 +224,7 @@ private[sbt] class CoursierDependencyResolution(coursierConfiguration: CoursierC t } - private def toCoursierDependency(moduleID: ModuleID): Seq[Dependency] = { + private def toCoursierDependency(moduleID: ModuleID) = { val attributes = if (moduleID.explicitArtifacts.isEmpty) Seq(Attributes("", "")) @@ -234,21 +237,23 @@ private[sbt] class CoursierDependencyResolution(coursierConfiguration: CoursierC val mapping = moduleID.configurations.getOrElse("compile") - // import _root_.coursier.ivy.IvyXml.{ mappings => ivyXmlMappings } - // val allMappings = ivyXmlMappings(mapping) + import _root_.coursier.ivy.IvyXml.{ mappings => ivyXmlMappings } + val allMappings = ivyXmlMappings(mapping) + for { + (from, to) <- allMappings attr <- attributes } yield { Dependency( Module(moduleID.organization, moduleID.name, extraAttrs), moduleID.revision, - configuration = mapping, + configuration = to, attributes = attr, exclusions = moduleID.exclusions.map { rule => (rule.organization, rule.name) }.toSet, transitive = moduleID.isTransitive - ) + ) -> from } } @@ -274,16 +279,17 @@ private[sbt] class CoursierDependencyResolution(coursierConfiguration: CoursierC val depsByConfig = { val deps = resolution.dependencies.toVector - (configurations ++ - Seq(ScalaTool, CompilerPlugin, Component).map(_.name)).map((_, deps)).toMap + configurations + .map((_, deps)) + .toMap } - val configurations0 = extractConfigurationTree + val configurations0 = extractConfigurationTree(configurations) val configResolutions = (depsByConfig.keys ++ configurations0.keys).map(k => (k, resolution)).toMap - val sbtBootJarOverrides = Map.empty[(Module, String), File] // TODO: get correct values + val sbtBootJarOverrides = Map.empty[(Module, String), File] val classifiers = None // TODO: get correct values if (artifactErrors.isEmpty) { @@ -315,10 +321,11 @@ private[sbt] class CoursierDependencyResolution(coursierConfiguration: CoursierC // Key is the name of the configuration (i.e. `compile`) and the values are the name itself plus the // names of the configurations that this one depends on. - private val extractConfigurationTree: ConfigurationDependencyTree = { + private def extractConfigurationTree(available: Seq[String]): ConfigurationDependencyTree = { (Configurations.default ++ Configurations.defaultInternal ++ Seq(ScalaTool, CompilerPlugin, Component)) + .filter(c => available.contains(c.name)) .map(c => (c.name, c.extendsConfigs.map(_.name) :+ c.name)) .toMap .mapValues(_.toSet) diff --git a/coursier/src/test/scala/sbt/librarymanagement/coursier/ResolutionSpec.scala b/coursier/src/test/scala/sbt/librarymanagement/coursier/ResolutionSpec.scala index dce781136..a118ccc10 100644 --- a/coursier/src/test/scala/sbt/librarymanagement/coursier/ResolutionSpec.scala +++ b/coursier/src/test/scala/sbt/librarymanagement/coursier/ResolutionSpec.scala @@ -33,7 +33,7 @@ class ResolutionSpec extends BaseCoursierSpecification { resolution should be('right) val r = resolution.right.get - r.configurations.map(_.configuration) should have size 11 + r.configurations.map(_.configuration) should have size 3 val compileConfig = r.configurations.find(_.configuration == Compile.toConfigRef).get compileConfig.modules should have size 2 @@ -55,24 +55,23 @@ class ResolutionSpec extends BaseCoursierSpecification { val r = resolution.right.get val componentConfig = r.configurations.find(_.configuration == Component.toConfigRef).get - componentConfig.modules should have size 1 + componentConfig.modules should have size 2 componentConfig.modules.head.artifacts should have size 1 componentConfig.modules.head.artifacts.head._1.classifier should contain("sources") } - // TODO: fix this test - // it should "resolve sbt jars" in { - // val dependencies = - // Vector(("org.scala-sbt" % "sbt" % "1.1.0" % "provided")) - // val coursierModule = module(stubModule, dependencies, Some("2.12.4")) - // val resolution = - // lmEngine.update(coursierModule, UpdateConfiguration(), UnresolvedWarningConfiguration(), log) + it should "resolve sbt jars" in { + val dependencies = + Vector(("org.scala-sbt" % "sbt" % "1.1.0" % "provided")) + val coursierModule = module(stubModule, dependencies, Some("2.12.4")) + val resolution = + lmEngine.update(coursierModule, UpdateConfiguration(), UnresolvedWarningConfiguration(), log) - // val r = resolution.right.get + val r = resolution.right.get - // val modules = r.configurations.flatMap(_.modules) - // modules.map(_.module.name) should contain("main_2.12") - // } + val modules = r.configurations.flatMap(_.modules) + modules.map(_.module.name) should contain("main_2.12") + } it should "resolve with default resolvers" in { val dependencies = diff --git a/scripted-test/src/sbt-test/lmScriptedTest/use-sbt-in-meta/build.sbt b/scripted-test/src/sbt-test/lmScriptedTest/use-sbt-in-meta/build.sbt new file mode 100644 index 000000000..d200f6743 --- /dev/null +++ b/scripted-test/src/sbt-test/lmScriptedTest/use-sbt-in-meta/build.sbt @@ -0,0 +1,25 @@ + +{ + def writePluginsSbt(str: String) = { + val pluginsSbt = file(".") / "project" / "plugins.sbt" + if (!pluginsSbt.exists) + IO.write( + pluginsSbt, + str + ) + } + val dr = sys.props.get("dependency.resolution") match { + case Some("ivy") => + """dependencyResolution := sbt.librarymanagement.ivy.IvyDependencyResolution(ivyConfiguration.value)""" + case Some("coursier") => + """dependencyResolution := sbt.librarymanagement.coursier.CoursierDependencyResolution(sbt.librarymanagement.coursier.CoursierConfiguration())""" + case _ => sys.error("""|The system property 'dependency.resolution' is not defined. + |Specify this property using the scriptedLaunchOpts -D.""".stripMargin) + } + + writePluginsSbt(dr) + addCommandAlias( + "setDependencyResolution", + s"""set $dr""" + ) +} diff --git a/scripted-test/src/sbt-test/lmScriptedTest/use-sbt-in-meta/project/Dependencies.scala b/scripted-test/src/sbt-test/lmScriptedTest/use-sbt-in-meta/project/Dependencies.scala new file mode 100644 index 000000000..587f8ac76 --- /dev/null +++ b/scripted-test/src/sbt-test/lmScriptedTest/use-sbt-in-meta/project/Dependencies.scala @@ -0,0 +1,6 @@ +import sbt._ +import Keys._ + +object Dependencies { + +} diff --git a/scripted-test/src/sbt-test/lmScriptedTest/use-sbt-in-meta/test b/scripted-test/src/sbt-test/lmScriptedTest/use-sbt-in-meta/test new file mode 100755 index 000000000..a0f164c64 --- /dev/null +++ b/scripted-test/src/sbt-test/lmScriptedTest/use-sbt-in-meta/test @@ -0,0 +1 @@ +> reload