public
Last active

McPom

  • Download Gist
McPom.scala
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
//Usage:
//override def pomPostProcess(node: Node): Node = mcPom(moduleConfigurations)(super.pomPostProcess(node))
 
trait McPom { self: DefaultProject =>
import scala.xml._
 
def mcPom(mcs: Set[ModuleConfiguration])(node: Node): Node = {
//make sure we have a trailing slash so we deduplicate URLs properly
def cleanUrl(url: String) = url match {
case null => ""
case "" => ""
case u if u endsWith "/" => u
case u => u + "/"
}
//Parse out the old repos so we can deduplicate
val oldRepos = (node \\ "project" \ "repositories" \ "repository").
map( n => cleanUrl((n \ "url").text) -> (n \ "name").text).toList
//Get our MavenRepositories from our MCs
val newRepos = mcs.filter(_.resolver.isInstanceOf[MavenRepository]).map(m => {
val r = m.resolver.asInstanceOf[MavenRepository]
cleanUrl(r.root) -> r.name
})
//Create the XML, goes via Map to ensure that we don't have duplicate URLs
val repos = Map((oldRepos ++ newRepos):_*).map( pair =>
<repository>
<id>{pair._2.toSeq.filter(_.isLetterOrDigit).mkString}</id>
<name>{pair._2}</name>
<url>{pair._1}</url>
</repository>
)
//a recursive tree rewriter for our XML
def rewrite(pf:PartialFunction[Node,Node])(ns: Seq[Node]): Seq[Node] = for(subnode <- ns) yield subnode match {
case e: Elem =>
if (pf isDefinedAt e) pf(e)
else Elem(e.prefix, e.label, e.attributes, e.scope, rewrite(pf)(e.child):_*)
case other => other
}
//If there are no repos in the pom - create entry, or else add to existing
val rule: PartialFunction[Node,Node] = if ((node \\ "project" \ "repositories" ).isEmpty) {
case Elem(prefix, "project", attribs, scope, children @ _*) =>
Elem(prefix, "project", attribs, scope, children ++ <repositories>{repos}</repositories>:_*)
} else {
case Elem(prefix, "repositories", attribs, scope, children @ _*) =>
Elem(prefix, "repositories", attribs, scope, repos.toList:_*)
}
//Work that magic!
rewrite(rule)(node.theSeq)(0)
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.