diff options
Diffstat (limited to 'src/main/scala/forge')
-rw-r--r-- | src/main/scala/forge/Args.scala | 3 | ||||
-rw-r--r-- | src/main/scala/forge/Evaluator.scala | 37 | ||||
-rw-r--r-- | src/main/scala/forge/Target.scala | 10 | ||||
-rw-r--r-- | src/main/scala/forge/package.scala | 18 |
4 files changed, 23 insertions, 45 deletions
diff --git a/src/main/scala/forge/Args.scala b/src/main/scala/forge/Args.scala index c7cc455c..e73af6c1 100644 --- a/src/main/scala/forge/Args.scala +++ b/src/main/scala/forge/Args.scala @@ -1,8 +1,7 @@ package forge -import java.nio.{file => jnio} -class Args(val args: IndexedSeq[_], val dest: jnio.Path){ +class Args(val args: IndexedSeq[_], val dest: ammonite.ops.Path){ def length = args.length def apply[T](index: Int): T = { if (index >= 0 && index < args.length) args(index).asInstanceOf[T] diff --git a/src/main/scala/forge/Evaluator.scala b/src/main/scala/forge/Evaluator.scala index ed63f199..42bac78f 100644 --- a/src/main/scala/forge/Evaluator.scala +++ b/src/main/scala/forge/Evaluator.scala @@ -1,18 +1,16 @@ package forge -import java.nio.charset.Charset -import java.nio.{file => jnio} import play.api.libs.json.{JsValue, Json} import scala.collection.mutable import scala.io.Codec - -class Evaluator(workspacePath: jnio.Path, +import ammonite.ops._ +class Evaluator(workspacePath: Path, labeling: Map[Target[_], Seq[String]]){ def evaluate(targets: OSet[Target[_]]): Evaluator.Results = { - jnio.Files.createDirectories(workspacePath) + mkdir(workspacePath) val sortedGroups = Evaluator.groupAroundNamedTargets( Evaluator.topoSortedTransitiveTargets(targets), @@ -34,14 +32,13 @@ class Evaluator(workspacePath: jnio.Path, results: collection.Map[Target[_], Any]) = { val (inputsHash, terminals) = partitionGroupInputOutput(group, results) - val primeLabel = labeling(terminals.items(0)).mkString("/") + val primeLabel = labeling(terminals.items(0)) + + val targetDestPath = workspacePath / primeLabel + val metadataPath = targetDestPath / up / (targetDestPath.last + ".forge.json") - val targetDestPath = workspacePath.resolve(jnio.Paths.get(primeLabel)) - val metadataPath = targetDestPath.resolveSibling( - targetDestPath.getFileName.toString + ".forge.json" - ) val cached = for{ - json <- util.Try(Json.parse(jnio.Files.newInputStream(metadataPath))).toOption + json <- util.Try(Json.parse(read.getInputStream(metadataPath))).toOption (hash, terminalResults) <- Json.fromJson[(Int, Seq[JsValue])](json).asOpt if hash == inputsHash && !group.exists(_.dirty) } yield (hash, terminalResults) @@ -60,11 +57,9 @@ class Evaluator(workspacePath: jnio.Path, } - jnio.Files.write( + write.over( metadataPath, Json.prettyPrint(Json.toJson((inputsHash, terminalResults))).getBytes(Codec.UTF8.charSet), - jnio.StandardOpenOption.CREATE, - jnio.StandardOpenOption.TRUNCATE_EXISTING ) (newResults, newEvaluated) @@ -85,9 +80,9 @@ class Evaluator(workspacePath: jnio.Path, def evaluateGroup(group: OSet[Target[_]], results: collection.Map[Target[_], Any], terminals: OSet[Target[_]], - targetDestPath: jnio.Path) = { + targetDestPath: Path) = { - deleteRec(targetDestPath) + rm(targetDestPath) val terminalResults = mutable.Buffer.empty[JsValue] val newEvaluated = mutable.Buffer.empty[Target[_]] val newResults = mutable.Map.empty[Target[_], Any] @@ -112,16 +107,6 @@ class Evaluator(workspacePath: jnio.Path, (newResults, newEvaluated, terminalResults) } - def deleteRec(path: jnio.Path) = { - if (jnio.Files.exists(path)){ - import collection.JavaConverters._ - jnio.Files.walk(path).iterator() - .asScala - .toArray - .reverseIterator - .map(jnio.Files.deleteIfExists) - } - } } diff --git a/src/main/scala/forge/Target.scala b/src/main/scala/forge/Target.scala index 144fc0e9..aa0371bc 100644 --- a/src/main/scala/forge/Target.scala +++ b/src/main/scala/forge/Target.scala @@ -1,7 +1,7 @@ package forge -import java.nio.{file => jnio} +import ammonite.ops.mkdir import play.api.libs.json.{Format, JsValue, Json} abstract class Target[T](implicit formatter: Format[T]) extends Target.Ops[T]{ /** @@ -79,8 +79,8 @@ object Target{ val inputs = List(source1, source1) } - def path(path: jnio.Path) = new Path(path) - class Path(path: jnio.Path) extends Target[jnio.Path]{ + def path(path: ammonite.ops.Path) = new Path(path) + class Path(path: ammonite.ops.Path) extends Target[ammonite.ops.Path]{ def evaluate(args: Args) = path val inputs = Nil } @@ -88,7 +88,7 @@ object Target{ command: Args => Seq[String]) extends Target[Subprocess.Result] { def evaluate(args: Args) = { - jnio.Files.createDirectories(args.dest) + mkdir(args.dest) import ammonite.ops._ implicit val path = ammonite.ops.Path(args.dest, pwd) val output = %%(command(args)) @@ -97,6 +97,6 @@ object Target{ } } object Subprocess{ - case class Result(result: ammonite.ops.CommandResult, dest: jnio.Path) + case class Result(result: ammonite.ops.CommandResult, dest: ammonite.ops.Path) } } diff --git a/src/main/scala/forge/package.scala b/src/main/scala/forge/package.scala index fe7e1965..bcbdc70f 100644 --- a/src/main/scala/forge/package.scala +++ b/src/main/scala/forge/package.scala @@ -1,21 +1,15 @@ import play.api.libs.json._ -import java.nio.{file => jnio} -import ammonite.ops.Bytes +import ammonite.ops.{Bytes, Path} package object forge { -// implicit object jsValueFormat extends Format[JsValue]{ -// def reads(json: JsValue) = JsSuccess(json) -// def writes(o: jnio.Path) = JsString(o.toAbsolutePath.toString) -// } - - implicit object pathFormat extends Format[jnio.Path]{ + implicit object pathFormat extends Format[ammonite.ops.Path]{ def reads(json: JsValue) = json match{ - case JsString(v) => JsSuccess(jnio.Paths.get(v)) + case JsString(v) => JsSuccess(Path(v)) case _ => JsError("Paths must be a String") } - def writes(o: jnio.Path) = JsString(o.toAbsolutePath.toString) + def writes(o: Path) = JsString(o.toString) } implicit object bytesFormat extends Format[Bytes]{ @@ -46,6 +40,6 @@ package object forge { } } - implicit val crFormat = Json.format[ammonite.ops.CommandResult] - implicit val tsFormat = Json.format[Target.Subprocess.Result] + implicit val crFormat: Format[ammonite.ops.CommandResult] = Json.format + implicit val tsFormat: Format[Target.Subprocess.Result] = Json.format } |