Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Unpack schema files
val avro = project
.settings(
avroSchemaFiles := target.value / "avro_src",
avroSchemaJar := "com.example" %% "schema" % "0.0.0",
Compile / avroSourceDirectories += avroSchemaFiles.value,
Compile / sourceGenerators +=
Def.sequential(unpackSchemaFilesTask, Compile / avroScalaGenerate).taskValue
)
import sbt.Keys._
import sbt.{Def, _}
/**
* We want to generate case classes from schema files in a jar. We download
* and unpack the jar into a directory for the sbt avro plugin to generate
* the case classes from.
*/
trait UnpackSchemaFiles {
val avroSchemaFiles = settingKey[File]("Avro schema files")
val avroSchemaJar = settingKey[ModuleID]("Jar containing avro schema")
val unpackSchemaFilesTask = Def.task {
val log = streams.value.log
val avro = avroSchemaFiles.value
val schemaJar = avroSchemaJar.value
log.info(s"Downloading $schemaJar to ${target.value}")
dependencyResolution.value.retrieve(
schemaJar,
None,
target.value,
streams.value.log
) match {
case Left(err) => log.error(s"$err")
case Right(files) =>
files.foreach { jar =>
log.info(s"Unpacking $jar")
IO.unzip(jar, avro, filter = _.endsWith(".avsc")).foreach { avsc =>
log.info(s"Unpacked $avsc to $avro")
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment