Skip to content

Commit

Permalink
Fix ordering of avsc files to make it work with referencing typedefs
Browse files Browse the repository at this point in the history
  • Loading branch information
eikek committed Feb 9, 2024
1 parent 8bdc665 commit 54f4389
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions project/AvroSchemaDownload.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import sbtavrohugger.SbtAvrohugger
import sbtavrohugger.SbtAvrohugger.autoImport.*
import java.io.File
import org.eclipse.jgit.api.ResetCommand.ResetType
import avrohugger.filesorter.AvscFileSorter

object AvroSchemaDownload extends AutoPlugin {

Expand All @@ -17,11 +18,13 @@ object AvroSchemaDownload extends AutoPlugin {
val schemaTargetDirectory = settingKey[File]("The directory to download into")
val schemaDownloadRepository = taskKey[Seq[File]]("Download the repository")
val schemaClearDownload = taskKey[Unit]("Removes all downloaded files")
val schemaEnableDownload = settingKey[Boolean]("Whether to enable downloading schema repository")
}

import autoImport._

override def projectSettings = AvroCodeGen.avroHuggerSettings ++ Seq(
schemaEnableDownload := true,
schemaRepository := "https://github.com/SwissDataScienceCenter/renku-schema",
schemaRef := Some("main"),
schemaTargetDirectory := (Compile / target).value / "renku-avro-schemas",
Expand All @@ -34,14 +37,20 @@ object AvroSchemaDownload extends AutoPlugin {
val repo = schemaRepository.value
val refspec = schemaRef.value
val output = schemaTargetDirectory.value
synchronizeSchemaFiles(logger, repo, refspec, output)
val enabled = schemaEnableDownload.value
if (enabled) {
synchronizeSchemaFiles(logger, repo, refspec, output)
} else {
logger.info("Downloading avro schema files is disabled.")
}
Seq(output)
},
Compile / avroScalaCustomNamespace := Map("*" -> "io.renku.messages"),
Compile / avroScalaSpecificCustomNamespace := Map("*" -> "io.renku.messages"),
Compile / avroSourceDirectories := Seq(
schemaTargetDirectory.value
),
Compile / avroSourceDirectories :=
AvscFileSorter.sortSchemaFiles(
// need to do this weird ordering so dependend files are read in first
(schemaTargetDirectory.value ** "*.avsc").get.reverse
)
,
Compile / sourceGenerators += Def
.sequential(
schemaDownloadRepository,
Expand Down Expand Up @@ -104,7 +113,7 @@ object AvroSchemaDownload extends AutoPlugin {

def prependPackage(file: File) = {
val content = IO.read(file)
if (!content.startsWith(pkgLine)) {
if (!content.startsWith("package ")) {
logger.info(s"Add package to: $file")
IO.write(file, s"$pkgLine;\n\n") // scala & java ...
IO.append(file, content)
Expand Down

0 comments on commit 54f4389

Please sign in to comment.