diff options
author | Li Haoyi <haoyi.sg@gmail.com> | 2017-10-18 21:54:37 -0700 |
---|---|---|
committer | Li Haoyi <haoyi.sg@gmail.com> | 2017-10-18 21:54:37 -0700 |
commit | 738104aafa8ac0f0dd09e9ef015d909b3f016158 (patch) | |
tree | 63fcba527489f1ee69913745dd69336c1c58ad0d /src/main/scala/hbt/Evaluator.scala | |
parent | 538b5ac28b80285953dbea08651cf5c5afa7c0f9 (diff) | |
download | mill-738104aafa8ac0f0dd09e9ef015d909b3f016158.tar.gz mill-738104aafa8ac0f0dd09e9ef015d909b3f016158.tar.bz2 mill-738104aafa8ac0f0dd09e9ef015d909b3f016158.zip |
- Add a configurable workspace to `evaluateTargetGraph`
- Break up `Hbt.scala` into `Args.scala` `Target.scala` and `Evaluator.scala`
Diffstat (limited to 'src/main/scala/hbt/Evaluator.scala')
-rw-r--r-- | src/main/scala/hbt/Evaluator.scala | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/src/main/scala/hbt/Evaluator.scala b/src/main/scala/hbt/Evaluator.scala new file mode 100644 index 00000000..442ec633 --- /dev/null +++ b/src/main/scala/hbt/Evaluator.scala @@ -0,0 +1,63 @@ +package hbt + +import java.nio.{file => jnio} + +import sourcecode.Enclosing + +import scala.collection.mutable + +object Evaluator{ + + + def apply[T](t: Target[T], + workspacePath: jnio.Path) + (implicit enclosing: Enclosing): T = { + jnio.Files.createDirectories(workspacePath) + val targetPaths = mutable.Map.empty[Target[_], List[String]] + def rec(t: Target[_], path: List[String]): Unit = { + if (targetPaths.contains(t)) () // do nothing + else { + val currentPath = + if (!t.label.startsWith(enclosing.value)) path.reverse + else t.label.stripPrefix(enclosing.value).drop(1).split('.').toList + + targetPaths(t) = currentPath + t.inputs.zipWithIndex.foreach{case (c, i) => rec(c, i.toString :: currentPath)} + } + } + rec(t, Nil) + val targets = targetPaths.keys.toIndexedSeq + val targetIndices = targets.zipWithIndex.toMap + + val numberedEdges = + for(i <- targets.indices) + yield targets(i).inputs.map(targetIndices) + + val sortedClusters = Tarjans(numberedEdges) + val nonTrivialClusters = sortedClusters.filter(_.length > 1) + assert(nonTrivialClusters.isEmpty, nonTrivialClusters) + + val results = mutable.Map.empty[Target[_], Any] + for (cluster <- sortedClusters){ + val Seq(singletonIndex) = cluster + val target = targets(singletonIndex) + val inputResults = target.inputs.map(results) + val targetDestPath = workspacePath.resolve( + jnio.Paths.get(targetPaths(target).mkString("/")) + ) + import collection.JavaConverters._ + if (jnio.Files.exists(targetDestPath)){ + jnio.Files.walk(targetDestPath).iterator() + .asScala + .toArray + .reverseIterator + .map(jnio.Files.deleteIfExists) + } + + results(target) = target.evaluate( + new Args(inputResults.toIndexedSeq, targetDestPath) + ) + } + results(t).asInstanceOf[T] + } +} |