Skip to content

Instantly share code, notes, and snippets.

@custommonkey
Last active August 6, 2021 14:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save custommonkey/b13e165a7404def84cd85ea3c5369157 to your computer and use it in GitHub Desktop.
Save custommonkey/b13e165a7404def84cd85ea3c5369157 to your computer and use it in GitHub Desktop.
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