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

Follow up refactor of importTree in RunScript #1749

Merged
merged 1 commit into from
Feb 22, 2022
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion main/core/src/mill/eval/Evaluator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ class Evaluator private (
| env = $env,
| failFast = $failFast,
| threadCount = $threadCount,
| importTree = $importTree,
| importTree = $importTree
|)""".stripMargin
}

Expand Down
52 changes: 27 additions & 25 deletions main/src/mill/main/RunScript.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package mill.main
import java.nio.file.NoSuchFileException
import ammonite.runtime.SpecialClassLoader
import ammonite.util.Util.CodeSource
import ammonite.util.{Name, Res, Util}
import ammonite.util.{Name, Res, ScriptOutput, Util}
import mill.define
import mill.define._
import mill.eval.{Evaluator, EvaluatorPaths}
Expand Down Expand Up @@ -66,29 +66,6 @@ object RunScript {
val eval =
for (rootModule <- evaluateRootModule(wd, path, interp, log))
yield {
val importTreeMap = mutable.Map.empty[String, Seq[String]]
interp.alreadyLoadedFiles.foreach { case (a, b) =>
val filePath = AmmoniteUtils.normalizeAmmoniteImportPath(a.filePathPrefix)
val importPaths = b.blockInfo.flatMap { b =>
val relativePath = b.hookInfo.trees.map { t =>
val prefix = t.prefix
val mappings = t.mappings.toSeq.flatMap(_.map(_._1))
prefix ++ mappings
}
relativePath.collect {
case "$file" :: tail =>
val concatenated = filePath.init ++ tail
AmmoniteUtils.normalizeAmmoniteImportPath(concatenated)
}
}
def toCls(segments: Seq[String]): String = segments.mkString(".")
val key = toCls(filePath)
val toAppend = importPaths.map(toCls)
importTreeMap(key) = importTreeMap.getOrElse(key, Seq.empty) ++ toAppend
}

val importTree = GraphUtils.linksToScriptNodeGraph(importTreeMap)

EvaluatorState(
rootModule,
rootModule.getClass.getClassLoader.asInstanceOf[
Expand All @@ -97,7 +74,7 @@ object RunScript {
mutable.Map.empty[Segments, (Int, Any)],
interp.watchedValues.toSeq,
systemProperties.keySet,
importTree
importTree(interp.alreadyLoadedFiles)
)
}
(eval, interp.watchedValues)
Expand Down Expand Up @@ -132,6 +109,31 @@ object RunScript {
sig.forall { case (p, l) => p.poll() == l }
}

private def importTree(alreadyLoadedFiles: collection.Map[CodeSource, ScriptOutput.Metadata]): Seq[ScriptNode] = {
val importTreeMap = mutable.Map.empty[String, Seq[String]]
alreadyLoadedFiles.foreach { case (a, b) =>
val filePath = AmmoniteUtils.normalizeAmmoniteImportPath(a.filePathPrefix)
val importPaths = b.blockInfo.flatMap { b =>
val relativePath = b.hookInfo.trees.map { t =>
val prefix = t.prefix
val mappings = t.mappings.toSeq.flatMap(_.map(_._1))
prefix ++ mappings
}
relativePath.collect {
case "$file" :: tail =>
val concatenated = filePath.init ++ tail
AmmoniteUtils.normalizeAmmoniteImportPath(concatenated)
}
}
def toCls(segments: Seq[String]): String = segments.mkString(".")
val key = toCls(filePath)
val toAppend = importPaths.map(toCls)
importTreeMap(key) = importTreeMap.getOrElse(key, Seq.empty) ++ toAppend
}

GraphUtils.linksToScriptNodeGraph(importTreeMap)
}

def evaluateRootModule(
wd: os.Path,
path: os.Path,
Expand Down