Skip to content

Commit

Permalink
Fix test and solidify implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
RustedBones committed Sep 12, 2024
1 parent 11a406d commit c497656
Show file tree
Hide file tree
Showing 41 changed files with 195 additions and 223 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
run: sbt '++ ${{ matrix.scala }}' compile test scripted

- name: Compress target directories
run: tar cf targets.tar target project/target
run: tar cf targets.tar target api/target bridge/target plugin/target project/target

- name: Upload target directories
uses: actions/upload-artifact@v4
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,16 @@ import com.github.sbt.avro.test.{TestSpecificRecord, TestSpecificRecordParent}
import org.apache.avro.compiler.specific.SpecificCompiler.FieldVisibility
import org.apache.avro.generic.GenericData.StringType
import org.specs2.mutable.Specification
import sbt.util.Logger

import java.io.File
import java.nio.file.Files

class SbtAvroSpec extends Specification {
// val builder = DefaultSchemaParserBuilder.default()
class AvroCompilerBridgeSpec extends Specification {
val sourceDir = new File(getClass.getClassLoader.getResource("avro").toURI)


val targetDir = Files.createTempDirectory("sbt-avro").toFile
val targetDir = Files.createTempDirectory("sbt-avro-compiler-bridge").toFile
val packageDir = new File(targetDir, "com/github/sbt/avro/test")
val logger = Logger.Null

"It should be possible to compile types depending on others if source files are provided in right order" >> {
val fullyQualifiedNames = Seq(
Expand Down Expand Up @@ -64,14 +61,12 @@ class SbtAvroSpec extends Specification {
compiler.compileAvscs(
refs.toArray,
targetDir,
// log = logger,
StringType.CharSequence,
FieldVisibility.PRIVATE,
true,
false,
false,
true,
// builder = builder
)

aJavaFile.isFile must beTrue
Expand All @@ -97,14 +92,12 @@ class SbtAvroSpec extends Specification {
classOf[TestSpecificRecord]
),
targetDir,
// log = logger,
StringType.CharSequence,
FieldVisibility.PRIVATE,
true,
false,
false,
true,
// builder = builder
true
)

val record = new File(packageDir, "TestSpecificRecord.java")
Expand Down
34 changes: 22 additions & 12 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
ThisBuild / dynverSonatypeSnapshots := true
ThisBuild / version := {
val orig = (ThisBuild / version).value
if (orig.endsWith("-SNAPSHOT")) "3.5.1-SNAPSHOT" else orig
if (orig.endsWith("-SNAPSHOT")) "4.0.0-SNAPSHOT" else orig
}

// metadata
Expand Down Expand Up @@ -46,7 +46,18 @@ ThisBuild / githubWorkflowPublish := Seq(WorkflowStep.Sbt(
)
))

lazy val `avro-compiler-api`: Project = project
lazy val `sbt-avro-parent`: Project = project
.in(file("."))
.settings(
publish / skip := true
)
.aggregate(
`sbt-avro-compiler-api`,
`sbt-avro-compiler-bridge`,
`sbt-avro`
)

lazy val `sbt-avro-compiler-api`: Project = project
.in(file("api"))
.settings(
crossPaths := false,
Expand All @@ -56,32 +67,31 @@ lazy val `avro-compiler-api`: Project = project
)
)

lazy val `avro-compiler-bridge`: Project = project
lazy val `sbt-avro-compiler-bridge`: Project = project
.in(file("bridge"))
.dependsOn(`avro-compiler-api`)
.dependsOn(`sbt-avro-compiler-api`)
.settings(
crossPaths := false,
autoScalaLibrary := false,
libraryDependencies ++= Seq(
Dependencies.Provided.AvroCompiler
Dependencies.Provided.AvroCompiler,
Dependencies.Test.Specs2Core,
)
)

lazy val `sbt-avro`: Project = project
.in(file("plugin"))
.dependsOn(
`avro-compiler-api`,
`avro-compiler-bridge` % "test"
`sbt-avro-compiler-api`,
`sbt-avro-compiler-bridge` % "test"
)
.enablePlugins(SbtPlugin)
.enablePlugins(BuildInfoPlugin, SbtPlugin)
.settings(
description := "Sbt plugin for compiling Avro sources",
buildInfoKeys := Seq[BuildInfoKey](name, version),
buildInfoPackage := "com.github.sbt.avro",
pluginCrossBuild / sbtVersion := "1.3.0",
Compile / scalacOptions ++= Seq("-deprecation"),
libraryDependencies ++= Seq(
Dependencies.Test.Specs2Core,
Dependencies.Test.AvroCompiler
),
scriptedLaunchOpts ++= Seq(
"-Xmx1024M",
"-Dplugin.version=" + version.value,
Expand Down
32 changes: 17 additions & 15 deletions plugin/src/main/scala/com/github/sbt/avro/SbtAvro.scala
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package com.github.sbt.avro

import sbt.Keys.*
import sbt.*
import CrossVersion.partialVersion
import sbt.{Def, *}
import Path.relativeTo
import sbt.librarymanagement.DependencyFilter

import java.io.File
import java.net.URLClassLoader
import java.util.jar.JarFile
import scala.collection.JavaConverters.*

/**
* Simple plugin for generating the Java sources for Avro schemas and protocols.
Expand Down Expand Up @@ -37,13 +34,13 @@ object SbtAvro extends AutoPlugin {
val avroFieldVisibility = settingKey[String]("Field visibility for the properties. Possible values: private, public. Default: public.")
val avroIncludes = settingKey[Seq[File]]("Avro schema includes.")
val avroOptionalGetters = settingKey[Boolean]("Generate getters that return Optional for nullable fields. Default: false.")
val avroSpecificRecords = settingKey[Seq[Class[_]]]("List of avro records to recompile with current avro version and settings.")
val avroSpecificRecords = settingKey[Seq[String]]("List of avro records to recompile with current avro version and settings. Classes must be part of the Avro library dependencies.")
// val avroSchemaParserBuilder = settingKey[SchemaParserBuilder](".avsc schema parser builder")
val avroSource = settingKey[File]("Default Avro source directory.")
val avroStringType = settingKey[String]("Type for representing strings. Possible values: CharSequence, String, Utf8. Default: CharSequence.")
val avroUnpackDependencies = taskKey[Seq[File]]("Unpack avro dependencies.")
val avroUseNamespace = settingKey[Boolean]("Validate that directory layout reflects namespaces, i.e. src/main/avro/com/myorg/MyRecord.avsc. Default: false.")
val avroVersion = settingKey[String]("")
val avroVersion = settingKey[String]("Avro version to use in the project. default: 1.12.0")

val avroGenerate = taskKey[Seq[File]]("Generate Java sources for Avro schemas.")
val packageAvro = taskKey[File]("Produces an avro artifact, such as a jar containing avro schemas.")
Expand All @@ -64,8 +61,9 @@ object SbtAvro extends AutoPlugin {
ivyConfigurations ++= Seq(Avro),
avroVersion := "1.12.0",
libraryDependencies ++= Seq(
"com.github.sbt" % "avro-compiler-bridge" % "3.5.1-SNAPSHOT" % Avro,
"com.github.sbt" % "sbt-avro-compiler-bridge" % BuildInfo.version % Avro,
"org.apache.avro" % "avro-compiler" % avroVersion.value % Avro,
"org.apache.avro" % "avro" % avroVersion.value,
)
)

Expand Down Expand Up @@ -102,11 +100,11 @@ object SbtAvro extends AutoPlugin {

import autoImport._

def packageAvroMappings = Def.task {
def packageAvroMappings: Def.Initialize[Task[Seq[(File, String)]]] = Def.task {
(avroSource.value ** AvroFilter) pair relativeTo(avroSource.value)
}

override def trigger: PluginTrigger = allRequirements
override def trigger: PluginTrigger = noTrigger

override def requires: Plugins = sbt.plugins.JvmPlugin

Expand Down Expand Up @@ -188,7 +186,6 @@ object SbtAvro extends AutoPlugin {
val useNs = avroUseNamespace.value
val createSetters = avroCreateSetters.value
val optionalGetters = avroOptionalGetters.value
// val builder = avroSchemaParserBuilder.value

val cachedCompile = {
import sbt.util.CacheStoreFactory
Expand All @@ -203,7 +200,7 @@ object SbtAvro extends AutoPlugin {
val inCache = Difference.inputs(cacheStoreFactory.make("in-cache"), FileInfo.lastModified)
val outCache = Difference.outputs(cacheStoreFactory.make("out-cache"), FileInfo.exists)

(inputs: Set[File], records: Seq[Class[_]]) =>
(inputs: Set[File], records: Seq[String]) =>
lastCache { lastCache =>
inCache(inputs) { inReport =>
outCache { outReport =>
Expand All @@ -212,10 +209,8 @@ object SbtAvro extends AutoPlugin {
// - no previous cache and we have records to recompile
// - input files have changed
// - output files are missing
val avdls = srcDirs.flatMap(d => (d ** AvroAvdlFilter).get)
val avscs = srcDirs.flatMap(d => (d ** AvroAvscFilter).get.map(avsc => new AvroFileRef(d, avsc.relativeTo(d).get.toString)))
val avprs = srcDirs.flatMap(d => (d ** AvroAvrpFilter).get)

// TODO Cache class loader
val avroClassLoader = new URLClassLoader(
"AvroClassLoader",
(Avro / managedClasspath).value.map(_.data.toURI.toURL).toArray,
Expand All @@ -228,9 +223,16 @@ object SbtAvro extends AutoPlugin {
.newInstance()
.asInstanceOf[AvroCompiler]

val recs = records.map(avroClassLoader.loadClass)
val avdls = srcDirs.flatMap(d => (d ** AvroAvdlFilter).get)
val avscs = srcDirs.flatMap(d => (d ** AvroAvscFilter).get.map(avsc => new AvroFileRef(d, avsc.relativeTo(d).get.toString)))
val avprs = srcDirs.flatMap(d => (d ** AvroAvrpFilter).get)

out.log

compiler
.compileAvroSchema(
records.toArray,
recs.toArray,
avdls.toArray,
avscs.toArray,
avprs.toArray,
Expand Down
14 changes: 0 additions & 14 deletions plugin/src/sbt-test/sbt-avro/avscparser/build.sbt

This file was deleted.

This file was deleted.

This file was deleted.

7 changes: 0 additions & 7 deletions plugin/src/sbt-test/sbt-avro/avscparser/project/plugins.sbt

This file was deleted.

11 changes: 0 additions & 11 deletions plugin/src/sbt-test/sbt-avro/avscparser/src/main/avro/a.avsc

This file was deleted.

This file was deleted.

10 changes: 0 additions & 10 deletions plugin/src/sbt-test/sbt-avro/avscparser/test

This file was deleted.

47 changes: 12 additions & 35 deletions plugin/src/sbt-test/sbt-avro/basic/build.sbt
Original file line number Diff line number Diff line change
@@ -1,41 +1,18 @@
ThisBuild / scalaVersion := "2.13.11"

lazy val basic: Project = project
.in(file("."))
.settings(
libraryDependencies += "org.apache.avro" % "avro" % avroVersion.value
)

lazy val `avro-11`: Project = project
.in(file(".avro_11"))
.settings(
avroVersion := "1.11.3",
libraryDependencies += "org.apache.avro" % "avro" % avroVersion.value,
sourceDirectory := (basic / sourceDirectory).value
)

lazy val `avro-10`: Project = project
.in(file(".avro_10"))
.settings(
avroVersion := "1.10.2",
libraryDependencies += "org.apache.avro" % "avro" % avroVersion.value,
sourceDirectory := (basic / sourceDirectory).value
lazy val basic = crossProject(
Avro("1.12.0"),
Avro("1.11.3"),
Avro("1.10.0"),
Avro("1.9.2"),
Avro("1.8.2"),
)

lazy val `avro-9`: Project = project
.in(file(".avro_9"))
.settings(
avroVersion := "1.9.2",
libraryDependencies += "org.apache.avro" % "avro" % avroVersion.value,
sourceDirectory := (basic / sourceDirectory).value
.crossType(CrossType.Pure)
.in(file("."))
.avroSettings("1.9.2")(
libraryDependencies += "joda-time" % "joda-time" % "2.12.7"
)

lazy val `avro-8`: Project = project
.in(file(".avro_8"))
.settings(
avroVersion := "1.8.2",
libraryDependencies += "org.apache.avro" % "avro" % avroVersion.value,
sourceDirectory := (basic / sourceDirectory).value
.avroSettings("1.8.2")(
libraryDependencies += "joda-time" % "joda-time" % "2.12.7"
)


Loading

0 comments on commit c497656

Please sign in to comment.