Last active
August 6, 2021 14:57
-
-
Save custommonkey/b13e165a7404def84cd85ea3c5369157 to your computer and use it in GitHub Desktop.
Unpack schema files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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