diff options
author | Li Haoyi <haoyi.sg@gmail.com> | 2017-11-15 14:03:38 -0800 |
---|---|---|
committer | Li Haoyi <haoyi.sg@gmail.com> | 2017-11-15 14:03:38 -0800 |
commit | 28c099486199c8fbf1c1f5247278a0484e8a747b (patch) | |
tree | 9de10743b6cbecf32cffae78fa77ed5646e7ff8c /core/src | |
parent | 51a433c8acff390a0109d92de222c9f8361b0bcf (diff) | |
download | mill-28c099486199c8fbf1c1f5247278a0484e8a747b.tar.gz mill-28c099486199c8fbf1c1f5247278a0484e8a747b.tar.bz2 mill-28c099486199c8fbf1c1f5247278a0484e8a747b.zip |
Strip out `play-json` dependency in favor of `upickle`, which we are forced to use anyway due to Ammonite. Saves us from classloading play-json and Jackson, shaving off a few hundred ms from the cold no-op runtime
Diffstat (limited to 'core/src')
-rw-r--r-- | core/src/main/scala/mill/Main.scala | 2 | ||||
-rw-r--r-- | core/src/main/scala/mill/discover/Discovered.scala | 9 | ||||
-rw-r--r-- | core/src/main/scala/mill/eval/Evaluator.scala | 18 | ||||
-rw-r--r-- | core/src/main/scala/mill/eval/PathRef.scala | 5 | ||||
-rw-r--r-- | core/src/main/scala/mill/package.scala | 1 | ||||
-rw-r--r-- | core/src/main/scala/mill/util/JsonFormatters.scala | 57 | ||||
-rw-r--r-- | core/src/main/scala/mill/util/OSet.scala | 14 |
7 files changed, 40 insertions, 66 deletions
diff --git a/core/src/main/scala/mill/Main.scala b/core/src/main/scala/mill/Main.scala index f3af640f..22f84d32 100644 --- a/core/src/main/scala/mill/Main.scala +++ b/core/src/main/scala/mill/Main.scala @@ -8,7 +8,7 @@ import mill.define.Task import mill.discover._ import mill.eval.Evaluator import mill.util.OSet -import play.api.libs.json.Format + import scala.annotation.tailrec import ammonite.main.Scripts.pathScoptRead diff --git a/core/src/main/scala/mill/discover/Discovered.scala b/core/src/main/scala/mill/discover/Discovered.scala index 3595f21f..ca1479b9 100644 --- a/core/src/main/scala/mill/discover/Discovered.scala +++ b/core/src/main/scala/mill/discover/Discovered.scala @@ -2,7 +2,6 @@ package mill.discover import mill.define.{Target, Task} import mill.discover.Router.{EntryPoint, Result} -import play.api.libs.json.Format import scala.language.experimental.macros import scala.reflect.macros.blackbox.Context @@ -16,11 +15,11 @@ class Discovered[T](val targets: Seq[LabelInfo[T, _]], case class Hierarchy[T](path: Seq[String], node: T => Any, children: List[Hierarchy[T]]) case class Labelled[T](target: Task[T], - format: Format[T], + format: upickle.default.ReadWriter[T], segments: Seq[String]) case class LabelInfo[T, V](path: Seq[String], - format: Format[V], + format: upickle.default.ReadWriter[V], run: T => Task[V]) extends Info[T, V] case class CommandInfo[T, V](path: Seq[String], @@ -42,14 +41,14 @@ object Discovered { } yield path inconsistent } - def makeTuple[T, V](path: Seq[String], func: T => Task[V])(implicit f: Format[V]) = { + def makeTuple[T, V](path: Seq[String], func: T => Task[V])(implicit f: upickle.default.ReadWriter[V]) = { LabelInfo(path, f, func) } def mapping[T: Discovered](t: T): Map[Task[_], Labelled[_]] = { implicitly[Discovered[T]].apply(t) - .map(x => x._3 -> Labelled(x._3.asInstanceOf[Task[Any]], x._2.asInstanceOf[Format[Any]], x._1)) + .map(x => x._3 -> Labelled(x._3.asInstanceOf[Task[Any]], x._2.asInstanceOf[upickle.default.ReadWriter[Any]], x._1)) .toMap } diff --git a/core/src/main/scala/mill/eval/Evaluator.scala b/core/src/main/scala/mill/eval/Evaluator.scala index 6e103ace..74cb77d3 100644 --- a/core/src/main/scala/mill/eval/Evaluator.scala +++ b/core/src/main/scala/mill/eval/Evaluator.scala @@ -4,7 +4,7 @@ import ammonite.ops._ import mill.define.Task import mill.discover.Labelled import mill.util.{Args, MultiBiMap, OSet} -import play.api.libs.json.{Format, JsValue, Json} + import scala.collection.mutable class Evaluator(workspacePath: Path, @@ -61,15 +61,15 @@ class Evaluator(workspacePath: Path, val cached = for{ metadataPath <- metadataPath - json <- scala.util.Try(Json.parse(read.getInputStream(metadataPath))).toOption - (cachedHash, terminalResult) <- Json.fromJson[(Int, JsValue)](json).asOpt + json <- scala.util.Try(upickle.json.read(read(metadataPath))).toOption + (cachedHash, terminalResult) <- scala.util.Try(upickle.default.readJs[(Int, upickle.Js.Value)](json)).toOption if cachedHash == inputsHash } yield terminalResult cached match{ case Some(terminalResult) => val newResults = mutable.LinkedHashMap.empty[Task[_], Any] - newResults(terminal) = labeling(terminal).format.reads(terminalResult).get + newResults(terminal) = labeling(terminal).format.read(terminalResult) (newResults, Nil) case _ => @@ -83,9 +83,7 @@ class Evaluator(workspacePath: Path, metadataPath.foreach( write.over( _, - Json.prettyPrint( - Json.toJson(inputsHash -> terminalResult) - ), + upickle.default.write(inputsHash -> terminalResult, indent = 4) ) ) @@ -99,7 +97,7 @@ class Evaluator(workspacePath: Path, targetDestPath: Option[Path]) = { targetDestPath.foreach(rm) - var terminalResult: JsValue = null + var terminalResult: upickle.Js.Value = null val newEvaluated = mutable.Buffer.empty[Task[_]] val newResults = mutable.LinkedHashMap.empty[Task[_], Any] for (target <- group.items if !results.contains(target)) { @@ -114,8 +112,8 @@ class Evaluator(workspacePath: Path, for(targetLabel <- labeling.get(target)){ terminalResult = targetLabel .format - .asInstanceOf[Format[Any]] - .writes(res.asInstanceOf[Any]) + .asInstanceOf[upickle.default.ReadWriter[Any]] + .write(res.asInstanceOf[Any]) } newResults(target) = res } diff --git a/core/src/main/scala/mill/eval/PathRef.scala b/core/src/main/scala/mill/eval/PathRef.scala index 5f7efe89..fa373b5a 100644 --- a/core/src/main/scala/mill/eval/PathRef.scala +++ b/core/src/main/scala/mill/eval/PathRef.scala @@ -8,7 +8,6 @@ import java.security.MessageDigest import ammonite.ops.Path import mill.util.JsonFormatters -import play.api.libs.json.{Format, Json} /** @@ -55,6 +54,6 @@ case class PathRef(path: ammonite.ops.Path){ } object PathRef{ - private implicit val pathFormat: Format[Path] = JsonFormatters.pathFormat - implicit def jsonFormatter: Format[PathRef] = Json.format + private implicit val pathFormat: upickle.default.ReadWriter[Path] = JsonFormatters.pathReadWrite + implicit def jsonFormatter: upickle.default.ReadWriter[PathRef] = upickle.default.macroRW } diff --git a/core/src/main/scala/mill/package.scala b/core/src/main/scala/mill/package.scala index 3d713b2a..81d87426 100644 --- a/core/src/main/scala/mill/package.scala +++ b/core/src/main/scala/mill/package.scala @@ -5,4 +5,5 @@ package object mill extends JsonFormatters{ type T[T] = define.Task[T] val PathRef = mill.eval.PathRef type PathRef = mill.eval.PathRef + } diff --git a/core/src/main/scala/mill/util/JsonFormatters.scala b/core/src/main/scala/mill/util/JsonFormatters.scala index f3420448..bce23468 100644 --- a/core/src/main/scala/mill/util/JsonFormatters.scala +++ b/core/src/main/scala/mill/util/JsonFormatters.scala @@ -1,47 +1,28 @@ package mill.util import ammonite.ops.{Bytes, Path} -import play.api.libs.json._ + object JsonFormatters extends JsonFormatters trait JsonFormatters { - implicit object pathFormat extends Format[ammonite.ops.Path]{ - def reads(json: JsValue) = json match{ - case JsString(v) => JsSuccess(Path(v)) - case _ => JsError("Paths must be a String") - } - def writes(o: Path) = JsString(o.toString) - } + implicit def readWriter[T: upickle.default.Reader: upickle.default.Writer] = + upickle.default.ReadWriter( + implicitly[upickle.default.Writer[T]].write, + implicitly[upickle.default.Reader[T]].read, + ) + implicit val pathReadWrite = upickle.default.ReadWriter[ammonite.ops.Path]( + o => upickle.Js.Str(o.toString()), + {case upickle.Js.Str(json) => Path(json)}, + ) - implicit object bytesFormat extends Format[Bytes]{ - def reads(json: JsValue) = json match{ - case JsString(v) => JsSuccess( - new Bytes(javax.xml.bind.DatatypeConverter.parseBase64Binary(v)) - ) - case _ => JsError("Bytes must be a String") - } - def writes(o: Bytes) = { - JsString(javax.xml.bind.DatatypeConverter.printBase64Binary(o.array)) - } - } + implicit val bytesReadWrite = upickle.default.ReadWriter[Bytes]( + o => upickle.Js.Str(javax.xml.bind.DatatypeConverter.printBase64Binary(o.array)), + {case upickle.Js.Str(json) => new Bytes(javax.xml.bind.DatatypeConverter.parseBase64Binary(json))} + ) - implicit def EitherFormat[T: Format, V: Format] = new Format[Either[T, V]]{ - def reads(json: JsValue) = json match{ - case JsObject(struct) => - (struct.get("type"), struct.get("value")) match{ - case (Some(JsString("Left")), Some(v)) => implicitly[Reads[T]].reads(v).map(Left(_)) - case (Some(JsString("Right")), Some(v)) => implicitly[Reads[V]].reads(v).map(Right(_)) - case _ => JsError("Either object layout is unknown") - } - case _ => JsError("Either must be an Object") - } - def writes(o: Either[T, V]) = o match{ - case Left(v) => Json.obj("type" -> "Left", "value" -> implicitly[Writes[T]].writes(v)) - case Right(v) => Json.obj("type" -> "Right", "value" -> implicitly[Writes[V]].writes(v)) - } - } - implicit lazy val crFormat: Format[ammonite.ops.CommandResult] = Json.format - implicit lazy val modFormat: Format[coursier.Module] = Json.format - implicit lazy val depFormat: Format[coursier.Dependency]= Json.format - implicit lazy val attrFormat: Format[coursier.Attributes] = Json.format + implicit lazy val crFormat: upickle.default.ReadWriter[ammonite.ops.CommandResult] = + upickle.default.macroRW + implicit lazy val modFormat: upickle.default.ReadWriter[coursier.Module] = upickle.default.macroRW + implicit lazy val depFormat: upickle.default.ReadWriter[coursier.Dependency]= upickle.default.macroRW + implicit lazy val attrFormat: upickle.default.ReadWriter[coursier.Attributes] = upickle.default.macroRW } diff --git a/core/src/main/scala/mill/util/OSet.scala b/core/src/main/scala/mill/util/OSet.scala index a1f59a22..82f5299b 100644 --- a/core/src/main/scala/mill/util/OSet.scala +++ b/core/src/main/scala/mill/util/OSet.scala @@ -1,7 +1,6 @@ package mill.util -import play.api.libs.json._ import scala.collection.mutable @@ -22,14 +21,11 @@ trait OSet[V] extends TraversableOnce[V]{ } object OSet{ - implicit def jsonFormat[T: Format]: Format[OSet[T]] = new Format[OSet[T]] { - def writes(o: OSet[T]) = JsArray(o.items.map(implicitly[Format[T]].writes)) - - def reads(json: JsValue) = json match{ - case x: JsArray => implicitly[Format[Seq[T]]].reads(x).map(OSet.from) - case _ => JsError("OSet needs to be an Array") - } - } + implicit def jsonFormat[T: upickle.default.ReadWriter]: upickle.default.ReadWriter[OSet[T]] = + upickle.default.ReadWriter[OSet[T]] ( + oset => upickle.default.writeJs(oset.toList), + {case json => OSet.from(upickle.default.readJs[Seq[T]](json))} + ) def apply[V](items: V*) = from(items) def from[V](items: TraversableOnce[V]): OSet[V] = { |