clean up optional dependency handling for make-pom. fixes #162

This commit is contained in:
Mark Harrah 2011-08-26 23:27:03 -04:00
parent 565cd3802f
commit 950bf200dc
3 changed files with 67 additions and 15 deletions

View File

@ -9,6 +9,7 @@ package sbt;
import java.io.{BufferedWriter, File, OutputStreamWriter, FileOutputStream}
import scala.xml.{Node => XNode, NodeSeq, PrettyPrinter, XML}
import Configurations.Optional
import org.apache.ivy.{core, plugins, Ivy}
import core.settings.IvySettings
@ -123,32 +124,37 @@ class MakePom
<groupId>{mrid.getOrganisation}</groupId>
<artifactId>{mrid.getName}</artifactId>
<version>{mrid.getRevision}</version>
{ scope(dependency)}
{ optional(dependency) }
{ scopeAndOptional(dependency)}
</dependency>
}
def scope(dependency: DependencyDescriptor): NodeSeq =
scope(getScope(dependency.getModuleConfigurations))
def scope(scope: String): NodeSeq = if(scope ne null) <scope>{scope}</scope> else NodeSeq.Empty
def optional(dependency: DependencyDescriptor) =
if(isOptional(dependency.getModuleConfigurations)) <optional>true</optional> else NodeSeq.Empty
def scopeAndOptional(dependency: DependencyDescriptor): NodeSeq =
{
val (scope, opt) = getScopeAndOptional(dependency.getModuleConfigurations)
scopeElem(scope) ++ optionalElem(opt)
}
def scopeElem(scope: Option[String]): NodeSeq = scope match {
case Some(s) => <scope>{s}</scope>
case None => NodeSeq.Empty
}
def optionalElem(opt: Boolean) = if(opt) <optional>true</optional> else NodeSeq.Empty
def moduleDescriptor(module: ModuleDescriptor) = module.getModuleRevisionId
def getScope(confs: Array[String]) =
def getScopeAndOptional(confs: Array[String]): (Option[String], Boolean) =
{
Configurations.defaultMavenConfigurations.find(conf => confs.contains(conf.name)) match
val (opt, notOptional) = confs.partition(_ == Optional.name)
val defaultNotOptional = Configurations.defaultMavenConfigurations.find(notOptional contains _.name)
val scope = defaultNotOptional match
{
case Some(conf) => conf.name
case Some(conf) => Some(conf.name)
case None =>
if(confs.isEmpty || confs(0) == Configurations.Default.name)
null
if(notOptional.isEmpty || notOptional(0) == Configurations.Default.name)
None
else
confs(0)
Option(notOptional(0))
}
(scope, !opt.isEmpty)
}
def isOptional(confs: Array[String]) = confs.isEmpty || (confs.length == 1 && confs(0) == Configurations.Optional.name)
def makeRepositories(settings: IvySettings, includeAll: Boolean, filterRepositories: MavenRepository => Boolean) =
{

View File

@ -0,0 +1,45 @@
import sbt._
import Keys._
object PomTest extends Build
{
lazy val custom = config("custom")
lazy val root = Project("root", file("root")) configs(custom) settings(
TaskKey[Unit]("check-pom") <<= checkPom,
libraryDependencies ++= Seq(
"a" % "a" % "1.0",
"b" % "b" % "1.0" % "runtime,optional",
"c" % "c" % "1.0" % "optional",
"d" % "d" % "1.0" % "test",
"e" % "e" % "1.0" % "custom",
"f" % "f" % "1.0" % "custom,optional,runtime",
"g" % "g" % "1.0" % "custom,runtime"
)
)
def checkPom = makePom map { pom =>
val expected = Seq(
("a", Some("compile"), false),
("b", Some("runtime"), true),
("c", None, true),
("d", Some("test"), false),
("e", Some("custom"), false),
("f", Some("runtime"), true),
("g", Some("runtime"), false)
)
val loaded = xml.XML.loadFile(pom)
val deps = loaded \\ "dependency"
expected foreach { case (id, scope, opt) =>
val dep = deps.find(d => (d \ "artifactId").text == id).getOrElse( error("Dependency '" + id + "' not written to pom:\n" + loaded))
val actualOpt = java.lang.Boolean.parseBoolean( (dep \\ "optional").text )
println("Actual: " + actualOpt + ", opt: " + opt)
assert(opt == actualOpt, "Invalid 'optional' section '" + (dep \\ "optional") + "', expected optional=" + opt)
val actualScope = (dep \\ "scope") match { case Seq() => None; case x => Some(x.text) }
assert(actualScope == scope, "Invalid 'scope' section '" + (dep \\ "scope") + "', expected scope=" + scope)
}
}
}

View File

@ -0,0 +1 @@
> check-pom