Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Implement method to resolve imports #559

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 44 additions & 16 deletions stage1/resolver.scala
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ trait DependencyImplementation extends Dependency{
lib.classLoaderRecursion(
this,
(this +: transitiveDependencies).collect{
case d: ArtifactInfo => d
case d: ArtifactInfo => d
}.groupBy(
d => (d.groupId,d.artifactId)
).mapValues(_.head)
Expand Down Expand Up @@ -169,8 +169,8 @@ case class BinaryDependency( paths: Seq[File], dependencies: Seq[Dependency] )(i
case class Dependencies( dependencies: Seq[Dependency] )(implicit val logger: Logger, val transientCache: java.util.Map[AnyRef,AnyRef], val classLoaderCache: ClassLoaderCache) extends DependencyImplementation{
override def lastModified = dependencies.map(_.lastModified).maxOption.getOrElse(0)
override lazy val moduleKey = this.getClass.getName + "(" + dependencies.map(_.moduleKey).mkString(", ") + ")" // PERFORMANCE HOTSPOT
def targetClasspath = ClassPath()
def exportedClasspath = ClassPath()
def targetClasspath = ClassPath()
def exportedClasspath = ClassPath()
override def show: String = this.getClass.getSimpleName + "( " + dependencies.map(_.show).mkString(", ") + " )"
}

Expand Down Expand Up @@ -334,8 +334,24 @@ case class BoundMavenDependency(
}.flatMap(_.transitivePom) :+ this
}

private lazy val properties: Map[String, String] = (
transitivePom.flatMap{ d =>
private lazy val imports : Seq[BoundMavenDependency] =
(pomXml \ "dependencyManagement" \ "dependencies" \ "dependency").collect {
case dependencyXml if (lookup(dependencyXml, _ \ "scope").exists(_ == "import")) =>
BoundMavenDependency(
cbtLastModified: Long,
mavenCache,
MavenDependency(
lookup(dependencyXml,_ \ "groupId").get,
lookup(dependencyXml,_ \ "artifactId").get,
lookup(dependencyXml,_ \ "version").get
),
repositories,
replace
)(logger, transientCache, classLoaderCache)
}.flatMap (importPom => importPom.transitivePom :+ importPom)

private def propertiesIn(scope : Seq[BoundMavenDependency]): Map[String, String] = (
scope.flatMap{ d =>
val props = (d.pomXml \ "properties").flatMap(_.child).map{
tag => tag.label -> tag.text
}
Expand All @@ -344,14 +360,22 @@ case class BoundMavenDependency(
}
).toMap

private lazy val properties = propertiesIn(transitivePom)

// order matters
private lazy val transitiveAndImportsPom : Seq[BoundMavenDependency] =
transitivePom.map(_.imports).zip(transitivePom).map {
case (importPoms, currentPom) => importPoms.+:(currentPom)
}.flatten

private lazy val dependencyVersions: Map[String, (String,String)] =
transitivePom.flatMap(
transitiveAndImportsPom.flatMap(
p =>
(p.pomXml \ "dependencyManagement" \ "dependencies" \ "dependency").map{
xml =>
val groupId = p.lookup(xml,_ \ "groupId").get
val artifactId = p.lookup(xml,_ \ "artifactId").get
val version = p.lookup(xml,_ \ "version").get
val groupId = p.lookupIn(transitiveAndImportsPom)(xml,_ \ "groupId").get
val artifactId = p.lookupIn(transitiveAndImportsPom)(xml,_ \ "artifactId").get
val version = p.lookupIn(transitiveAndImportsPom)(xml,_ \ "version").get
artifactId -> (groupId, version)
}
).toMap
Expand All @@ -367,13 +391,13 @@ case class BoundMavenDependency(
case xml if ( (xml \ "scope").text == "" || (xml \ "scope").text == "compile" ) && (xml \ "optional").text != "true" =>
val artifactId = lookup(xml,_ \ "artifactId").get
val groupId =
lookup(xml,_ \ "groupId").getOrElse(
lookup(xml,_ \ "groupId").getOrElse{
dependencyVersions
.get(artifactId).map(_._1)
.getOrElse(
throw new Exception(s"$artifactId not found in \n$dependencyVersions")
)
)
}
val version =
lookup(xml,_ \ "version").getOrElse(
dependencyVersions
Expand All @@ -391,22 +415,22 @@ case class BoundMavenDependency(
BoundMavenDependency( cbtLastModified, mavenCache, _, repositories, replace )
).toVector
}
def lookup( xml: Node, accessor: Node => NodeSeq ): Option[String] = {
private def lookupIn(scope : Seq[BoundMavenDependency])(xml: Node, accessor: Node => NodeSeq ): Option[String] = {
// println("lookup in " + xml)
val Substitution = "\\$\\{([^\\}]+)\\}".r

def matcherFunc(matcher: scala.util.matching.Regex.Match): String = {
val path = matcher.group(1)
val ret = properties.get(path).orElse(
transitivePom.reverse.flatMap{ d =>
val ret = propertiesIn(scope).get(path).orElse(
scope.reverse.flatMap{ d =>
Some(path.split("\\.").toList).collect{
case "project" :: path =>
path.foldLeft(d.pomXml:NodeSeq){ case (xml,tag) => xml \ tag }.text
}.filter(_ != "")
}.headOption
)
.getOrElse(
throw new Exception(s"Can't find $path in \n$properties.\n\npomParents: $transitivePom\n\n pomXml:\n$pomXml" )
throw new Exception(s"Can't find $path in \n$properties.\n\nscope: ${scope.mkString("\n")}\n\n pomXml:\n$pomXml" )
)

Substitution.replaceAllIn(
Expand All @@ -422,7 +446,11 @@ case class BoundMavenDependency(
)
}
}

def lookup = lookupIn(transitivePom) _

}

object BoundMavenDependency{
private lazy val versionRangeRegex = "^[\\[\\(]([^,\\]\\)]*)(,([^\\]\\)]*))?[\\]\\)]".r
def extractVersion( versionOrRange: String ) = {
Expand Down Expand Up @@ -466,7 +494,7 @@ object BoundMavenDependency{
.last
)
deps.map{
d =>
d =>
val l = latest((d.groupId,d.artifactId))
if(d != l) logger.resolver("outdated: "++d.show)
l
Expand Down