diff --git a/src/scala/com/github/johnynek/bazel_deps/IO.scala b/src/scala/com/github/johnynek/bazel_deps/IO.scala index 0cb3dca..6c94a9b 100644 --- a/src/scala/com/github/johnynek/bazel_deps/IO.scala +++ b/src/scala/com/github/johnynek/bazel_deps/IO.scala @@ -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 = @@ -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]] @@ -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)) @@ -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) diff --git a/src/scala/com/github/johnynek/bazel_deps/MakeDeps.scala b/src/scala/com/github/johnynek/bazel_deps/MakeDeps.scala index 9bf8cff..3a02e20 100644 --- a/src/scala/com/github/johnynek/bazel_deps/MakeDeps.scala +++ b/src/scala/com/github/johnynek/bazel_deps/MakeDeps.scala @@ -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