Skip to content

Commit

Permalink
Optionally compress the output
Browse files Browse the repository at this point in the history
  • Loading branch information
ianoc committed Sep 23, 2022
1 parent 3247241 commit 24bdb3f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
23 changes: 22 additions & 1 deletion src/scala/com/github/johnynek/bazel_deps/IO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,26 @@ import cats.implicits._
*/

object IO {
private[this] val logger = LoggerFactory.getLogger("IO")

val charset = "UTF-8"
val pathSeparator = File.separator

private[this] val logger = LoggerFactory.getLogger("IO")

case class Path(parts: List[String]) {
def child(p: String): Path = Path(parts ++ List(p))
def parent: Path = Path(parts.dropRight(1))
def sibling(p: String): Path = Path(parts.dropRight(1) ++ List(p))
def asString: String = parts.mkString(pathSeparator)
def extension: String = {
val fileName = parts.last
val segments = fileName.split("\\.")
if(segments.length == 1) {
fileName
} else {
segments.tail.mkString(".")
}
}
}

def path(s: String): Path =
Expand All @@ -49,6 +59,7 @@ object IO {
* data longer than needed if that is desired.
*/
case class WriteFile(f: Path, data: Eval[String]) extends Ops[Unit]
case class WriteGzipFile(f: Path, data: Eval[String]) extends Ops[Unit]
case class Failed(err: Throwable) extends Ops[Nothing]
case class ReadFile(path: Path) extends Ops[Option[String]]

Expand Down Expand Up @@ -81,6 +92,9 @@ object IO {
def writeUtf8(f: Path, s: => String): Result[Unit] =
liftF[Ops, Unit](WriteFile(f, Eval.always(s)))

def writeGzipUtf8(f: Path, s: => String): Result[Unit] =
liftF[Ops, Unit](WriteGzipFile(f, Eval.always(s)))

// Reads the contents of `f`, returning None if file doesn't exist
def readUtf8(f: Path): Result[Option[String]] =
liftF[Ops, Option[String]](ReadFile(f))
Expand Down Expand Up @@ -163,6 +177,13 @@ object IO {
try os.write(d.value.getBytes(charset))
finally { os.close() }
}
case WriteGzipFile(f, d) =>
Try {
import java.util.zip.GZIPOutputStream;
val os = new GZIPOutputStream(new FileOutputStream(fileFor(f)))
try os.write(d.value.getBytes(charset))
finally { os.close() }
}
case ReadFile(f) =>
Try({
val ff = fileFor(f)
Expand Down
6 changes: 5 additions & 1 deletion src/scala/com/github/johnynek/bazel_deps/MakeDeps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,11 @@ object MakeDeps {
_ <- if (b) IO.const(false) else IO.mkdirs(resolvedJsonOutputPath.parent)
allArtifacts = AllArtifacts(artifacts.sortBy(_.artifact))
artifactsJson = allArtifacts.asJson
_ <- IO.writeUtf8(resolvedJsonOutputPath, artifactsJson.spaces2)
_ <- if(resolvedJsonOutputPath.extension.endsWith(".gz")) {
IO.writeGzipUtf8(resolvedJsonOutputPath, artifactsJson.spaces2)
} else {
IO.writeUtf8(resolvedJsonOutputPath, artifactsJson.spaces2)
}
} yield ()

// Here we actually run the whole thing
Expand Down

0 comments on commit 24bdb3f

Please sign in to comment.