mirror of https://github.com/sbt/sbt.git
Cleanup Input/Output/CacheStore
This commit is contained in:
parent
c6e793b03c
commit
1368f5f9db
|
|
@ -1,23 +1,17 @@
|
||||||
package sbt.internal.util
|
package sbt.internal.util
|
||||||
|
|
||||||
|
import java.io.{ File, InputStream, OutputStream }
|
||||||
|
import sbt.io.syntax.fileToRichFile
|
||||||
|
import sbt.io.{ IO, Using }
|
||||||
import sjsonnew.{ IsoString, JsonReader, JsonWriter, SupportConverter }
|
import sjsonnew.{ IsoString, JsonReader, JsonWriter, SupportConverter }
|
||||||
|
|
||||||
import java.io.{ File, InputStream, OutputStream }
|
/** A `CacheStore` is used by the caching infrastructure to persist cached information. */
|
||||||
|
|
||||||
import sbt.io.{ IO, Using }
|
|
||||||
import sbt.io.syntax.fileToRichFile
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A `CacheStore` is used by the caching infrastructure to persist cached information.
|
|
||||||
*/
|
|
||||||
trait CacheStore extends Input with Output {
|
trait CacheStore extends Input with Output {
|
||||||
/** Delete the persisted information. */
|
/** Delete the persisted information. */
|
||||||
def delete(): Unit
|
def delete(): Unit
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** Factory that can derive new stores. */
|
||||||
* Factory that can derive new stores.
|
|
||||||
*/
|
|
||||||
trait CacheStoreFactory {
|
trait CacheStoreFactory {
|
||||||
/** Create a new store. */
|
/** Create a new store. */
|
||||||
def derive(identifier: String): CacheStore
|
def derive(identifier: String): CacheStore
|
||||||
|
|
@ -26,74 +20,32 @@ trait CacheStoreFactory {
|
||||||
def sub(identifier: String): CacheStoreFactory
|
def sub(identifier: String): CacheStoreFactory
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** A factory that creates new stores persisted in `base`. */
|
||||||
* A factory that creates new stores persisted in `base`.
|
|
||||||
*/
|
|
||||||
class DirectoryStoreFactory[J: IsoString](base: File, converter: SupportConverter[J]) extends CacheStoreFactory {
|
class DirectoryStoreFactory[J: IsoString](base: File, converter: SupportConverter[J]) extends CacheStoreFactory {
|
||||||
|
|
||||||
IO.createDirectory(base)
|
IO.createDirectory(base)
|
||||||
|
|
||||||
override def derive(identifier: String): CacheStore =
|
def derive(identifier: String): CacheStore = new FileBasedStore(base / identifier, converter)
|
||||||
new FileBasedStore(base / identifier, converter)
|
|
||||||
|
|
||||||
override def sub(identifier: String): CacheStoreFactory =
|
def sub(identifier: String): CacheStoreFactory = new DirectoryStoreFactory(base / identifier, converter)
|
||||||
new DirectoryStoreFactory(base / identifier, converter)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** A `CacheStore` that persists information in `file`. */
|
||||||
* A `CacheStore` that persists information in `file`.
|
|
||||||
*/
|
|
||||||
class FileBasedStore[J: IsoString](file: File, converter: SupportConverter[J]) extends CacheStore {
|
class FileBasedStore[J: IsoString](file: File, converter: SupportConverter[J]) extends CacheStore {
|
||||||
|
|
||||||
IO.touch(file, setModified = false)
|
IO.touch(file, setModified = false)
|
||||||
|
|
||||||
override def delete(): Unit =
|
def read[T: JsonReader]() = Using.fileInputStream(file)(stream => new PlainInput(stream, converter).read())
|
||||||
IO.delete(file)
|
|
||||||
|
|
||||||
override def read[T: JsonReader](): T =
|
def write[T: JsonWriter](value: T) =
|
||||||
Using.fileInputStream(file) { stream =>
|
Using.fileOutputStream(append = false)(file)(stream => new PlainOutput(stream, converter).write(value))
|
||||||
val input = new PlainInput(stream, converter)
|
|
||||||
input.read()
|
|
||||||
}
|
|
||||||
|
|
||||||
override def read[T: JsonReader](default: => T): T =
|
|
||||||
try read[T]()
|
|
||||||
catch { case _: Exception => default }
|
|
||||||
|
|
||||||
override def write[T: JsonWriter](value: T): Unit =
|
|
||||||
Using.fileOutputStream(append = false)(file) { stream =>
|
|
||||||
val output = new PlainOutput(stream, converter)
|
|
||||||
output.write(value)
|
|
||||||
}
|
|
||||||
|
|
||||||
override def close(): Unit = ()
|
|
||||||
|
|
||||||
|
def delete() = IO.delete(file)
|
||||||
|
def close() = ()
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** A store that reads from `inputStream` and writes to `outputStream`. */
|
||||||
* A store that reads from `inputStream` and writes to `outputStream
|
|
||||||
*/
|
|
||||||
class StreamBasedStore[J: IsoString](inputStream: InputStream, outputStream: OutputStream, converter: SupportConverter[J]) extends CacheStore {
|
class StreamBasedStore[J: IsoString](inputStream: InputStream, outputStream: OutputStream, converter: SupportConverter[J]) extends CacheStore {
|
||||||
|
def read[T: JsonReader]() = new PlainInput(inputStream, converter).read()
|
||||||
override def delete(): Unit = ()
|
def write[T: JsonWriter](value: T) = new PlainOutput(outputStream, converter).write(value)
|
||||||
|
def delete() = ()
|
||||||
override def read[T: JsonReader](): T = {
|
def close() = { inputStream.close(); outputStream.close() }
|
||||||
val input = new PlainInput(inputStream, converter)
|
}
|
||||||
input.read()
|
|
||||||
}
|
|
||||||
|
|
||||||
override def read[T: JsonReader](default: => T): T =
|
|
||||||
try read[T]()
|
|
||||||
catch { case _: Exception => default }
|
|
||||||
|
|
||||||
override def write[T: JsonWriter](value: T): Unit = {
|
|
||||||
val output = new PlainOutput(outputStream, converter)
|
|
||||||
output.write(value)
|
|
||||||
}
|
|
||||||
|
|
||||||
override def close(): Unit = {
|
|
||||||
inputStream.close()
|
|
||||||
outputStream.close()
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -1,20 +1,18 @@
|
||||||
package sbt.internal.util
|
package sbt.internal.util
|
||||||
|
|
||||||
import sbt.io.{ IO, Using }
|
|
||||||
|
|
||||||
import java.io.{ Closeable, InputStream }
|
import java.io.{ Closeable, InputStream }
|
||||||
|
import scala.util.control.NonFatal
|
||||||
import scala.util.{ Failure, Success }
|
|
||||||
|
|
||||||
import sjsonnew.{ IsoString, JsonReader, SupportConverter }
|
import sjsonnew.{ IsoString, JsonReader, SupportConverter }
|
||||||
|
import sbt.io.{ IO, Using }
|
||||||
|
|
||||||
trait Input extends Closeable {
|
trait Input extends Closeable {
|
||||||
def read[T: JsonReader](): T
|
def read[T: JsonReader](): T
|
||||||
def read[T: JsonReader](default: => T): T
|
def read[T: JsonReader](default: => T): T = try read[T]() catch { case NonFatal(_) => default }
|
||||||
}
|
}
|
||||||
|
|
||||||
class PlainInput[J: IsoString](input: InputStream, converter: SupportConverter[J]) extends Input {
|
class PlainInput[J: IsoString](input: InputStream, converter: SupportConverter[J]) extends Input {
|
||||||
val isoFormat: IsoString[J] = implicitly
|
val isoFormat: IsoString[J] = implicitly
|
||||||
|
|
||||||
private def readFully(): String = {
|
private def readFully(): String = {
|
||||||
Using.streamReader(input, IO.utf8) { reader =>
|
Using.streamReader(input, IO.utf8) { reader =>
|
||||||
val builder = new StringBuilder()
|
val builder = new StringBuilder()
|
||||||
|
|
@ -28,18 +26,7 @@ class PlainInput[J: IsoString](input: InputStream, converter: SupportConverter[J
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override def read[T: JsonReader](): T = {
|
def read[T: JsonReader]() = converter.fromJson(isoFormat.from(readFully())).get
|
||||||
val string = readFully()
|
|
||||||
val json = isoFormat.from(string)
|
|
||||||
converter.fromJson(json) match {
|
|
||||||
case Success(value) => value
|
|
||||||
case Failure(ex) => throw ex
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override def read[T: JsonReader](default: => T): T =
|
def close() = input.close()
|
||||||
try read[T]()
|
|
||||||
catch { case _: Exception => default }
|
|
||||||
|
|
||||||
override def close(): Unit = input.close()
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,8 @@
|
||||||
package sbt.internal.util
|
package sbt.internal.util
|
||||||
|
|
||||||
import sbt.io.Using
|
|
||||||
|
|
||||||
import java.io.{ Closeable, OutputStream }
|
import java.io.{ Closeable, OutputStream }
|
||||||
|
|
||||||
import scala.util.{ Failure, Success }
|
|
||||||
|
|
||||||
import sjsonnew.{ IsoString, JsonWriter, SupportConverter }
|
import sjsonnew.{ IsoString, JsonWriter, SupportConverter }
|
||||||
|
import sbt.io.Using
|
||||||
|
|
||||||
trait Output extends Closeable {
|
trait Output extends Closeable {
|
||||||
def write[T: JsonWriter](value: T): Unit
|
def write[T: JsonWriter](value: T): Unit
|
||||||
|
|
@ -14,19 +10,16 @@ trait Output extends Closeable {
|
||||||
|
|
||||||
class PlainOutput[J: IsoString](output: OutputStream, converter: SupportConverter[J]) extends Output {
|
class PlainOutput[J: IsoString](output: OutputStream, converter: SupportConverter[J]) extends Output {
|
||||||
val isoFormat: IsoString[J] = implicitly
|
val isoFormat: IsoString[J] = implicitly
|
||||||
override def write[T: JsonWriter](value: T): Unit = {
|
|
||||||
converter.toJson(value) match {
|
def write[T: JsonWriter](value: T) = {
|
||||||
case Success(js) =>
|
val js = converter.toJson(value).get
|
||||||
val asString = isoFormat.to(js)
|
val asString = isoFormat.to(js)
|
||||||
Using.bufferedOutputStream(output) { writer =>
|
Using.bufferedOutputStream(output) { writer =>
|
||||||
val out = new java.io.PrintWriter(writer)
|
val out = new java.io.PrintWriter(writer)
|
||||||
out.print(asString)
|
out.print(asString)
|
||||||
out.flush()
|
out.flush()
|
||||||
}
|
|
||||||
case Failure(ex) =>
|
|
||||||
throw ex
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override def close(): Unit = output.close()
|
def close() = output.close()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue