mirror of https://github.com/sbt/sbt.git
Move implicit FileStamp JsonFormats into object
I realized it was probably not ideal to have these implicit JsonFormats defined directly in the FileStamp object because they might inadvertently be brought into scope with a wildcard import.
This commit is contained in:
parent
2990e08c5d
commit
fb15065438
|
|
@ -605,6 +605,7 @@ object Defaults extends BuildCommon {
|
|||
s"inc_compile$extra.zip"
|
||||
},
|
||||
externalHooks := {
|
||||
import sbt.nio.FileStamp.Formats.seqPathFileStampJsonFormatter
|
||||
val current =
|
||||
(unmanagedSources / inputFileStamps).value ++ (managedSources / outputFileStamps).value
|
||||
val previous = (externalHooks / inputFileStamps).previous
|
||||
|
|
|
|||
|
|
@ -65,115 +65,22 @@ private[sbt] object FileStamp {
|
|||
final case class LastModified private[sbt] (time: Long) extends FileStamp
|
||||
final case class Error(exception: IOException) extends FileStamp
|
||||
|
||||
implicit val pathJsonFormatter: JsonFormat[Seq[Path]] = new JsonFormat[Seq[Path]] {
|
||||
override def write[J](obj: Seq[Path], builder: Builder[J]): Unit = {
|
||||
builder.beginArray()
|
||||
obj.foreach { path =>
|
||||
builder.writeString(path.toString)
|
||||
}
|
||||
builder.endArray()
|
||||
}
|
||||
|
||||
override def read[J](jsOpt: Option[J], unbuilder: Unbuilder[J]): Seq[Path] =
|
||||
jsOpt match {
|
||||
case Some(js) =>
|
||||
val size = unbuilder.beginArray(js)
|
||||
val res = (1 to size) map { _ =>
|
||||
Paths.get(unbuilder.readString(unbuilder.nextElement))
|
||||
}
|
||||
unbuilder.endArray()
|
||||
res
|
||||
case None =>
|
||||
deserializationError("Expected JsArray but found None")
|
||||
}
|
||||
}
|
||||
|
||||
implicit val fileJsonFormatter: JsonFormat[Seq[File]] = new JsonFormat[Seq[File]] {
|
||||
override def write[J](obj: Seq[File], builder: Builder[J]): Unit = {
|
||||
builder.beginArray()
|
||||
obj.foreach { file =>
|
||||
builder.writeString(file.toString)
|
||||
}
|
||||
builder.endArray()
|
||||
}
|
||||
|
||||
override def read[J](jsOpt: Option[J], unbuilder: Unbuilder[J]): Seq[File] =
|
||||
jsOpt match {
|
||||
case Some(js) =>
|
||||
val size = unbuilder.beginArray(js)
|
||||
val res = (1 to size) map { _ =>
|
||||
new File(unbuilder.readString(unbuilder.nextElement))
|
||||
}
|
||||
unbuilder.endArray()
|
||||
res
|
||||
case None =>
|
||||
deserializationError("Expected JsArray but found None")
|
||||
}
|
||||
}
|
||||
implicit val fileJson: JsonFormat[File] = new JsonFormat[File] {
|
||||
override def read[J](jsOpt: Option[J], unbuilder: Unbuilder[J]): File =
|
||||
fileJsonFormatter.read(jsOpt, unbuilder).head
|
||||
override def write[J](obj: File, builder: Builder[J]): Unit =
|
||||
fileJsonFormatter.write(obj :: Nil, builder)
|
||||
}
|
||||
implicit val pathJson: JsonFormat[Path] = new JsonFormat[Path] {
|
||||
override def read[J](jsOpt: Option[J], unbuilder: Unbuilder[J]): Path =
|
||||
pathJsonFormatter.read(jsOpt, unbuilder).head
|
||||
override def write[J](obj: Path, builder: Builder[J]): Unit =
|
||||
pathJsonFormatter.write(obj :: Nil, builder)
|
||||
}
|
||||
implicit val fileStampJsonFormatter: JsonFormat[Seq[(Path, FileStamp)]] =
|
||||
new JsonFormat[Seq[(Path, FileStamp)]] {
|
||||
override def write[J](obj: Seq[(Path, FileStamp)], builder: Builder[J]): Unit = {
|
||||
val (hashes, lastModifiedTimes) = obj.partition(_._2.isInstanceOf[Hash])
|
||||
builder.beginObject()
|
||||
builder.addField("hashes", hashes.asInstanceOf[Seq[(Path, Hash)]])(fileHashJsonFormatter)
|
||||
builder.addField(
|
||||
"lastModifiedTimes",
|
||||
lastModifiedTimes.asInstanceOf[Seq[(Path, LastModified)]]
|
||||
)(
|
||||
fileLastModifiedJsonFormatter
|
||||
)
|
||||
builder.endObject()
|
||||
}
|
||||
|
||||
override def read[J](jsOpt: Option[J], unbuilder: Unbuilder[J]): Seq[(Path, FileStamp)] =
|
||||
jsOpt match {
|
||||
case Some(js) =>
|
||||
unbuilder.beginObject(js)
|
||||
val hashes = unbuilder.readField("hashes")(fileHashJsonFormatter)
|
||||
val lastModifieds =
|
||||
unbuilder.readField("lastModifiedTimes")(fileLastModifiedJsonFormatter)
|
||||
unbuilder.endObject()
|
||||
hashes ++ lastModifieds
|
||||
case None =>
|
||||
deserializationError("Expected JsObject but found None")
|
||||
}
|
||||
}
|
||||
val fileHashJsonFormatter: JsonFormat[Seq[(Path, Hash)]] =
|
||||
new JsonFormat[Seq[(Path, Hash)]] {
|
||||
override def write[J](obj: Seq[(Path, Hash)], builder: Builder[J]): Unit = {
|
||||
object Formats {
|
||||
implicit val seqPathJsonFormatter: JsonFormat[Seq[Path]] = new JsonFormat[Seq[Path]] {
|
||||
override def write[J](obj: Seq[Path], builder: Builder[J]): Unit = {
|
||||
builder.beginArray()
|
||||
obj.foreach {
|
||||
case (p, h) =>
|
||||
builder.beginArray()
|
||||
builder.writeString(p.toString)
|
||||
builder.writeString(h.hex)
|
||||
builder.endArray()
|
||||
obj.foreach { path =>
|
||||
builder.writeString(path.toString)
|
||||
}
|
||||
builder.endArray()
|
||||
}
|
||||
|
||||
override def read[J](jsOpt: Option[J], unbuilder: Unbuilder[J]): Seq[(Path, Hash)] =
|
||||
override def read[J](jsOpt: Option[J], unbuilder: Unbuilder[J]): Seq[Path] =
|
||||
jsOpt match {
|
||||
case Some(js) =>
|
||||
val size = unbuilder.beginArray(js)
|
||||
val res = (1 to size) map { _ =>
|
||||
unbuilder.beginArray(unbuilder.nextElement)
|
||||
val path = Paths.get(unbuilder.readString(unbuilder.nextElement))
|
||||
val hash = FileStamp.hash(unbuilder.readString(unbuilder.nextElement))
|
||||
unbuilder.endArray()
|
||||
path -> hash
|
||||
Paths.get(unbuilder.readString(unbuilder.nextElement))
|
||||
}
|
||||
unbuilder.endArray()
|
||||
res
|
||||
|
|
@ -181,30 +88,22 @@ private[sbt] object FileStamp {
|
|||
deserializationError("Expected JsArray but found None")
|
||||
}
|
||||
}
|
||||
val fileLastModifiedJsonFormatter: JsonFormat[Seq[(Path, LastModified)]] =
|
||||
new JsonFormat[Seq[(Path, LastModified)]] {
|
||||
override def write[J](obj: Seq[(Path, LastModified)], builder: Builder[J]): Unit = {
|
||||
|
||||
implicit val seqFileJsonFormatter: JsonFormat[Seq[File]] = new JsonFormat[Seq[File]] {
|
||||
override def write[J](obj: Seq[File], builder: Builder[J]): Unit = {
|
||||
builder.beginArray()
|
||||
obj.foreach {
|
||||
case (p, lm) =>
|
||||
builder.beginArray()
|
||||
builder.writeString(p.toString)
|
||||
builder.writeLong(lm.time)
|
||||
builder.endArray()
|
||||
obj.foreach { file =>
|
||||
builder.writeString(file.toString)
|
||||
}
|
||||
builder.endArray()
|
||||
}
|
||||
|
||||
override def read[J](jsOpt: Option[J], unbuilder: Unbuilder[J]): Seq[(Path, LastModified)] =
|
||||
override def read[J](jsOpt: Option[J], unbuilder: Unbuilder[J]): Seq[File] =
|
||||
jsOpt match {
|
||||
case Some(js) =>
|
||||
val size = unbuilder.beginArray(js)
|
||||
val res = (1 to size) map { _ =>
|
||||
unbuilder.beginArray(unbuilder.nextElement)
|
||||
val path = Paths.get(unbuilder.readString(unbuilder.nextElement))
|
||||
val hash = FileStamp.LastModified(unbuilder.readLong(unbuilder.nextElement))
|
||||
unbuilder.endArray()
|
||||
path -> hash
|
||||
new File(unbuilder.readString(unbuilder.nextElement))
|
||||
}
|
||||
unbuilder.endArray()
|
||||
res
|
||||
|
|
@ -212,6 +111,111 @@ private[sbt] object FileStamp {
|
|||
deserializationError("Expected JsArray but found None")
|
||||
}
|
||||
}
|
||||
implicit val fileJsonFormatter: JsonFormat[File] = new JsonFormat[File] {
|
||||
override def read[J](jsOpt: Option[J], unbuilder: Unbuilder[J]): File =
|
||||
seqFileJsonFormatter.read(jsOpt, unbuilder).head
|
||||
|
||||
override def write[J](obj: File, builder: Builder[J]): Unit =
|
||||
seqFileJsonFormatter.write(obj :: Nil, builder)
|
||||
}
|
||||
implicit val pathJsonFormatter: JsonFormat[Path] = new JsonFormat[Path] {
|
||||
override def read[J](jsOpt: Option[J], unbuilder: Unbuilder[J]): Path =
|
||||
seqPathJsonFormatter.read(jsOpt, unbuilder).head
|
||||
|
||||
override def write[J](obj: Path, builder: Builder[J]): Unit =
|
||||
seqPathJsonFormatter.write(obj :: Nil, builder)
|
||||
}
|
||||
implicit val seqPathFileStampJsonFormatter: JsonFormat[Seq[(Path, FileStamp)]] =
|
||||
new JsonFormat[Seq[(Path, FileStamp)]] {
|
||||
override def write[J](obj: Seq[(Path, FileStamp)], builder: Builder[J]): Unit = {
|
||||
val (hashes, lastModifiedTimes) = obj.partition(_._2.isInstanceOf[Hash])
|
||||
builder.beginObject()
|
||||
builder.addField("hashes", hashes.asInstanceOf[Seq[(Path, Hash)]])(
|
||||
seqPathHashJsonFormatter
|
||||
)
|
||||
builder.addField(
|
||||
"lastModifiedTimes",
|
||||
lastModifiedTimes.asInstanceOf[Seq[(Path, LastModified)]]
|
||||
)(seqPathLastModifiedJsonFormatter)
|
||||
builder.endObject()
|
||||
}
|
||||
|
||||
override def read[J](jsOpt: Option[J], unbuilder: Unbuilder[J]): Seq[(Path, FileStamp)] =
|
||||
jsOpt match {
|
||||
case Some(js) =>
|
||||
unbuilder.beginObject(js)
|
||||
val hashes = unbuilder.readField("hashes")(seqPathHashJsonFormatter)
|
||||
val lastModifieds =
|
||||
unbuilder.readField("lastModifiedTimes")(seqPathLastModifiedJsonFormatter)
|
||||
unbuilder.endObject()
|
||||
hashes ++ lastModifieds
|
||||
case None =>
|
||||
deserializationError("Expected JsObject but found None")
|
||||
}
|
||||
}
|
||||
private[sbt] val seqPathHashJsonFormatter: JsonFormat[Seq[(Path, Hash)]] =
|
||||
new JsonFormat[Seq[(Path, Hash)]] {
|
||||
override def write[J](obj: Seq[(Path, Hash)], builder: Builder[J]): Unit = {
|
||||
builder.beginArray()
|
||||
obj.foreach {
|
||||
case (p, h) =>
|
||||
builder.beginArray()
|
||||
builder.writeString(p.toString)
|
||||
builder.writeString(h.hex)
|
||||
builder.endArray()
|
||||
}
|
||||
builder.endArray()
|
||||
}
|
||||
|
||||
override def read[J](jsOpt: Option[J], unbuilder: Unbuilder[J]): Seq[(Path, Hash)] =
|
||||
jsOpt match {
|
||||
case Some(js) =>
|
||||
val size = unbuilder.beginArray(js)
|
||||
val res = (1 to size) map { _ =>
|
||||
unbuilder.beginArray(unbuilder.nextElement)
|
||||
val path = Paths.get(unbuilder.readString(unbuilder.nextElement))
|
||||
val hash = FileStamp.hash(unbuilder.readString(unbuilder.nextElement))
|
||||
unbuilder.endArray()
|
||||
path -> hash
|
||||
}
|
||||
unbuilder.endArray()
|
||||
res
|
||||
case None =>
|
||||
deserializationError("Expected JsArray but found None")
|
||||
}
|
||||
}
|
||||
private[sbt] val seqPathLastModifiedJsonFormatter: JsonFormat[Seq[(Path, LastModified)]] =
|
||||
new JsonFormat[Seq[(Path, LastModified)]] {
|
||||
override def write[J](obj: Seq[(Path, LastModified)], builder: Builder[J]): Unit = {
|
||||
builder.beginArray()
|
||||
obj.foreach {
|
||||
case (p, lm) =>
|
||||
builder.beginArray()
|
||||
builder.writeString(p.toString)
|
||||
builder.writeLong(lm.time)
|
||||
builder.endArray()
|
||||
}
|
||||
builder.endArray()
|
||||
}
|
||||
|
||||
override def read[J](jsOpt: Option[J], unbuilder: Unbuilder[J]): Seq[(Path, LastModified)] =
|
||||
jsOpt match {
|
||||
case Some(js) =>
|
||||
val size = unbuilder.beginArray(js)
|
||||
val res = (1 to size) map { _ =>
|
||||
unbuilder.beginArray(unbuilder.nextElement)
|
||||
val path = Paths.get(unbuilder.readString(unbuilder.nextElement))
|
||||
val hash = FileStamp.LastModified(unbuilder.readLong(unbuilder.nextElement))
|
||||
unbuilder.endArray()
|
||||
path -> hash
|
||||
}
|
||||
unbuilder.endArray()
|
||||
res
|
||||
case None =>
|
||||
deserializationError("Expected JsArray but found None")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private implicit class EitherOps(val e: Either[FileStamp, FileStamp]) extends AnyVal {
|
||||
def value: Option[FileStamp] = if (e == null) None else Some(e.fold(identity, identity))
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ import sbt.internal.Clean.ToSeqPath
|
|||
import sbt.internal.Continuous.FileStampRepository
|
||||
import sbt.internal.util.{ AttributeKey, SourcePosition }
|
||||
import sbt.internal.{ Clean, Continuous, DynamicInput, SettingsGraph }
|
||||
import sbt.nio.FileStamp.{ fileStampJsonFormatter, pathJsonFormatter, _ }
|
||||
import sbt.nio.FileStamp.Formats._
|
||||
import sbt.nio.FileStamper.{ Hash, LastModified }
|
||||
import sbt.nio.Keys._
|
||||
import sbt.nio.file.ChangedFiles
|
||||
|
|
|
|||
|
|
@ -11,7 +11,8 @@ import java.nio.file.{ Path, Paths }
|
|||
|
||||
import org.scalatest.FlatSpec
|
||||
import sbt.nio.FileStamp
|
||||
import sbt.nio.FileStamp._
|
||||
import sbt.nio.FileStamp.Formats
|
||||
import sjsonnew.JsonFormat
|
||||
import sjsonnew.support.scalajson.unsafe.Converter
|
||||
|
||||
class FileStampJsonSpec extends FlatSpec {
|
||||
|
|
@ -20,8 +21,10 @@ class FileStampJsonSpec extends FlatSpec {
|
|||
Paths.get("foo") -> FileStamp.hash("bar"),
|
||||
Paths.get("bar") -> FileStamp.hash("buzz")
|
||||
)
|
||||
val json = Converter.toJsonUnsafe(hashes)(fileHashJsonFormatter)
|
||||
val deserialized = Converter.fromJsonUnsafe(json)(fileHashJsonFormatter)
|
||||
implicit val formatter: JsonFormat[Seq[(Path, FileStamp.Hash)]] =
|
||||
Formats.seqPathHashJsonFormatter
|
||||
val json = Converter.toJsonUnsafe(hashes)
|
||||
val deserialized = Converter.fromJsonUnsafe(json)
|
||||
assert(hashes == deserialized)
|
||||
}
|
||||
"file last modified times" should "be serializable" in {
|
||||
|
|
@ -29,8 +32,10 @@ class FileStampJsonSpec extends FlatSpec {
|
|||
Paths.get("foo") -> FileStamp.LastModified(1234),
|
||||
Paths.get("bar") -> FileStamp.LastModified(5678)
|
||||
)
|
||||
val json = Converter.toJsonUnsafe(lastModifiedTimes)(fileLastModifiedJsonFormatter)
|
||||
val deserialized = Converter.fromJsonUnsafe(json)(fileLastModifiedJsonFormatter)
|
||||
implicit val formatter: JsonFormat[Seq[(Path, FileStamp.LastModified)]] =
|
||||
Formats.seqPathLastModifiedJsonFormatter
|
||||
val json = Converter.toJsonUnsafe(lastModifiedTimes)
|
||||
val deserialized = Converter.fromJsonUnsafe(json)
|
||||
assert(lastModifiedTimes == deserialized)
|
||||
}
|
||||
"both" should "be serializable" in {
|
||||
|
|
@ -43,8 +48,9 @@ class FileStampJsonSpec extends FlatSpec {
|
|||
Paths.get("bar") -> FileStamp.LastModified(5678)
|
||||
)
|
||||
val both: Seq[(Path, FileStamp)] = hashes ++ lastModifiedTimes
|
||||
val json = Converter.toJsonUnsafe(both)(fileStampJsonFormatter)
|
||||
val deserialized = Converter.fromJsonUnsafe(json)(fileStampJsonFormatter)
|
||||
import Formats.seqPathFileStampJsonFormatter
|
||||
val json = Converter.toJsonUnsafe(both)
|
||||
val deserialized = Converter.fromJsonUnsafe(json)
|
||||
assert(both == deserialized)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -34,11 +34,11 @@ package object sbt
|
|||
implicit def filesToFinder(cc: Traversable[File]): sbt.io.PathFinder =
|
||||
sbt.io.PathFinder.strict(cc)
|
||||
implicit val fileStampJsonFormatter: JsonFormat[Seq[(NioPath, FileStamp)]] =
|
||||
FileStamp.fileStampJsonFormatter
|
||||
implicit val pathJsonFormatter: JsonFormat[Seq[NioPath]] = FileStamp.pathJsonFormatter
|
||||
implicit val fileJsonFormatter: JsonFormat[Seq[File]] = FileStamp.fileJsonFormatter
|
||||
implicit val singlePathJsonFormatter: JsonFormat[NioPath] = FileStamp.pathJson
|
||||
implicit val singleFileJsonFormatter: JsonFormat[File] = FileStamp.fileJson
|
||||
FileStamp.Formats.seqPathFileStampJsonFormatter
|
||||
implicit val pathJsonFormatter: JsonFormat[Seq[NioPath]] = FileStamp.Formats.seqPathJsonFormatter
|
||||
implicit val fileJsonFormatter: JsonFormat[Seq[File]] = FileStamp.Formats.seqFileJsonFormatter
|
||||
implicit val singlePathJsonFormatter: JsonFormat[NioPath] = FileStamp.Formats.pathJsonFormatter
|
||||
implicit val singleFileJsonFormatter: JsonFormat[File] = FileStamp.Formats.fileJsonFormatter
|
||||
// others
|
||||
|
||||
object CompileOrder {
|
||||
|
|
|
|||
Loading…
Reference in New Issue