From 5583efffd00b76e7ccd19c2fcb7a78127ea4622e Mon Sep 17 00:00:00 2001 From: Alexandre Archambault Date: Tue, 31 Jan 2017 15:19:08 +0100 Subject: [PATCH] Move ivy xml generation In the hope that this might address issues with shading plugin on single core machines --- .../scala-2.10/coursier/CoursierPlugin.scala | 17 +++++++ .../main/scala-2.10/coursier/FromSbt.scala | 5 ++- .../{MakeIvyXml.scala => IvyXml.scala} | 45 +++++++++++++++++-- .../src/main/scala-2.10/coursier/Tasks.scala | 32 ------------- .../scala-2.10/coursier/ShadingPlugin.scala | 4 +- 5 files changed, 64 insertions(+), 39 deletions(-) rename plugin/src/main/scala-2.10/coursier/{MakeIvyXml.scala => IvyXml.scala} (65%) diff --git a/plugin/src/main/scala-2.10/coursier/CoursierPlugin.scala b/plugin/src/main/scala-2.10/coursier/CoursierPlugin.scala index 0778c35f9..c2c04c982 100644 --- a/plugin/src/main/scala-2.10/coursier/CoursierPlugin.scala +++ b/plugin/src/main/scala-2.10/coursier/CoursierPlugin.scala @@ -55,6 +55,21 @@ object CoursierPlugin extends AutoPlugin { ) ) + def makeIvyXmlBefore[T]( + task: TaskKey[T], + shadedConfigOpt: Option[(String, String)] + ): Setting[Task[T]] = + // not 100% sure that make writeFiles below happen before the actions triggered by task.value... + task := { + val currentProject = { + val proj = coursierProject.value + val publications = coursierPublications.value + proj.copy(publications = publications) + } + IvyXml.writeFiles(currentProject, shadedConfigOpt, ivySbt.value, streams.value.log) + task.value + } + def coursierSettings( shadedConfigOpt: Option[(String, String)], packageConfigs: Seq[(Configuration, String)] @@ -83,6 +98,8 @@ object CoursierPlugin extends AutoPlugin { withClassifiers = true, sbtClassifiers = true ), + makeIvyXmlBefore(deliverLocalConfiguration, shadedConfigOpt), + makeIvyXmlBefore(deliverConfiguration, shadedConfigOpt), update <<= Tasks.updateTask( shadedConfigOpt, withClassifiers = false diff --git a/plugin/src/main/scala-2.10/coursier/FromSbt.scala b/plugin/src/main/scala-2.10/coursier/FromSbt.scala index c162efad4..befd22c47 100644 --- a/plugin/src/main/scala-2.10/coursier/FromSbt.scala +++ b/plugin/src/main/scala-2.10/coursier/FromSbt.scala @@ -1,6 +1,7 @@ package coursier -import coursier.ivy.{ IvyXml, IvyRepository } +import coursier.ivy.IvyRepository +import coursier.ivy.IvyXml.{ mappings => ivyXmlMappings } import java.net.{ MalformedURLException, URL } @@ -71,7 +72,7 @@ object FromSbt { ) val mapping = module.configurations.getOrElse("compile") - val allMappings = IvyXml.mappings(mapping) + val allMappings = ivyXmlMappings(mapping) val attributes = if (module.explicitArtifacts.isEmpty) diff --git a/plugin/src/main/scala-2.10/coursier/MakeIvyXml.scala b/plugin/src/main/scala-2.10/coursier/IvyXml.scala similarity index 65% rename from plugin/src/main/scala-2.10/coursier/MakeIvyXml.scala rename to plugin/src/main/scala-2.10/coursier/IvyXml.scala index eb81ed8c7..b3972ea5e 100644 --- a/plugin/src/main/scala-2.10/coursier/MakeIvyXml.scala +++ b/plugin/src/main/scala-2.10/coursier/IvyXml.scala @@ -1,10 +1,49 @@ package coursier -import scala.xml.{ Node, PrefixedAttribute } +import coursier.internal.FileUtil +import org.apache.ivy.core.module.id.ModuleRevisionId -object MakeIvyXml { +import scala.collection.JavaConverters._ +import scala.xml.{Node, PrefixedAttribute} - def apply(project0: Project, shadedConfigOpt: Option[String]): Node = { +object IvyXml { + + // These are required for publish to be fine, later on. + def writeFiles( + currentProject: Project, + shadedConfigOpt: Option[(String, String)], + ivySbt: sbt.IvySbt, + log: sbt.Logger + ): Unit = { + + val ivyCacheManager = ivySbt.withIvy(log)(ivy => + ivy.getResolutionCacheManager + ) + + val ivyModule = ModuleRevisionId.newInstance( + currentProject.module.organization, + currentProject.module.name, + currentProject.version, + currentProject.module.attributes.asJava + ) + + val cacheIvyFile = ivyCacheManager.getResolvedIvyFileInCache(ivyModule) + val cacheIvyPropertiesFile = ivyCacheManager.getResolvedIvyPropertiesInCache(ivyModule) + + val printer = new scala.xml.PrettyPrinter(80, 2) + + val content0 = """""" + '\n' + + printer.format(content(currentProject, shadedConfigOpt.map(_._2))) + cacheIvyFile.getParentFile.mkdirs() + log.info(s"Writing Ivy file $cacheIvyFile") + FileUtil.write(cacheIvyFile, content0.getBytes("UTF-8")) + + // Just writing an empty file here... Are these only used? + cacheIvyPropertiesFile.getParentFile.mkdirs() + FileUtil.write(cacheIvyPropertiesFile, Array()) + } + + def content(project0: Project, shadedConfigOpt: Option[String]): Node = { val filterOutDependencies = shadedConfigOpt.toSet[String].flatMap { shadedConfig => diff --git a/plugin/src/main/scala-2.10/coursier/Tasks.scala b/plugin/src/main/scala-2.10/coursier/Tasks.scala index 33bb136e2..2dbcc116b 100644 --- a/plugin/src/main/scala-2.10/coursier/Tasks.scala +++ b/plugin/src/main/scala-2.10/coursier/Tasks.scala @@ -844,42 +844,10 @@ object Tasks { proj.copy(publications = publications) } - val ivySbt0 = ivySbt.value - val ivyCacheManager = ivySbt0.withIvy(streams.value.log)(ivy => - ivy.getResolutionCacheManager - ) - - val ivyModule = ModuleRevisionId.newInstance( - currentProject.module.organization, - currentProject.module.name, - currentProject.version, - currentProject.module.attributes.asJava - ) - val cacheIvyFile = ivyCacheManager.getResolvedIvyFileInCache(ivyModule) - val cacheIvyPropertiesFile = ivyCacheManager.getResolvedIvyPropertiesInCache(ivyModule) - val log = streams.value.log val verbosityLevel = coursierVerbosity.value - // required for publish to be fine, later on - def writeIvyFiles() = { - val printer = new scala.xml.PrettyPrinter(80, 2) - - val b = new StringBuilder - b ++= """""" - b += '\n' - b ++= printer.format(MakeIvyXml(currentProject, shadedConfigOpt.map(_._2))) - cacheIvyFile.getParentFile.mkdirs() - FileUtil.write(cacheIvyFile, b.result().getBytes("UTF-8")) - - // Just writing an empty file here... Are these only used? - cacheIvyPropertiesFile.getParentFile.mkdirs() - FileUtil.write(cacheIvyPropertiesFile, "".getBytes("UTF-8")) - } - - writeIvyFiles() - val res = { if (withClassifiers && sbtClassifiers) coursierSbtClassifiersResolution diff --git a/sbt-shading/src/main/scala-2.10/coursier/ShadingPlugin.scala b/sbt-shading/src/main/scala-2.10/coursier/ShadingPlugin.scala index 510fcdaf9..9b262a61a 100644 --- a/sbt-shading/src/main/scala-2.10/coursier/ShadingPlugin.scala +++ b/sbt-shading/src/main/scala-2.10/coursier/ShadingPlugin.scala @@ -1,6 +1,6 @@ package coursier -import coursier.ivy.IvyXml +import coursier.ivy.IvyXml.{ mappings => ivyXmlMappings } import sbt.Keys._ import sbt.{AutoPlugin, Compile, Configuration, TaskKey, inConfig} @@ -69,7 +69,7 @@ object ShadingPlugin extends AutoPlugin { .map(c => c.copy(extendsConfigs = c.extendsConfigs.filter(_.name != Shaded.name))), libraryDependencies := libraryDependencies.in(baseSbtConfiguration).value.filter { dep => val isShaded = dep.configurations.exists { mappings => - IvyXml.mappings(mappings).exists(_._1 == Shaded.name) + ivyXmlMappings(mappings).exists(_._1 == Shaded.name) } !isShaded