Last active
August 29, 2015 14:04
-
-
Save metasim/e6aa7c7ada17185e48df to your computer and use it in GitHub Desktop.
SBT plugin fragment implementing utility command to read `pom.xml` and convert `<dependency/>` elements to SBT format.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// NB: Requires SBT >= 0.13.5 | |
package pomhelpers | |
import sbt._ | |
import Keys._ | |
import sbt.complete.Parser | |
import sbt.complete.DefaultParsers._ | |
object ExtractMvnDependencies extends AutoPlugin { | |
override def trigger = allRequirements | |
override lazy val projectSettings = Seq(commands ++= Seq(pomLibs2Sbt)) | |
val Pom2SbtCommand = "pom2sbt" | |
val Pom2SbtHelp = "<pom-xml-fragment>" | |
// **How should this be constructed to handle mult-line inputs?** | |
val MultiLineCommandInputParser: Parser[String] = | |
OptSpace ~> (StringBasic | StringVerbatim) | |
lazy val pomLibs2Sbt = Command( | |
Pom2SbtCommand, | |
Help((Pom2SbtCommand, Pom2SbtHelp)))(_ => MultiLineCommandInputParser) { (state , xmlFrag) => | |
val sbtdeps = pomXml2SbtDsl(xmlFrag) | |
println(sbtdeps.mkString("\n")) | |
state | |
} | |
/** Convert POM string to SBT form. */ | |
private def pomXml2SbtDsl(xmlFrag: String): Seq[String] = { | |
import scala.xml._ | |
val deps = XML.loadString(xmlFrag) \\ "dependency" | |
deps map ((dependency) => { | |
val coords = Seq("groupId", "artifactId", "version", "scope") | |
val coordParts = coords.map(coord => (dependency \ coord).text.trim).filter(_.nonEmpty) | |
coordParts.mkString("\"", "\" % \"", "\"") | |
}) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment