Skip to content

Commit

Permalink
Support sbt 2
Browse files Browse the repository at this point in the history
Add initial support for sbt 2.0.0-M2
  • Loading branch information
julianpeeters committed Oct 15, 2024
1 parent 27095d3 commit efde2a5
Show file tree
Hide file tree
Showing 24 changed files with 71 additions and 48 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
sbt plugin for generating Scala case classes and ADTs from Apache Avro schemas, datafiles, and protocols.


Install the plugin (compatible with sbt 1.3+)
Install the plugin (compatible with sbt 1.3+ and sbt 2.0+)
---------------------------------------

Add the following lines to the file ``myproject/project/plugins.sbt`` in your
project directory:

addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.8.4")
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.9.0-M2")



Expand Down
12 changes: 10 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
ThisBuild / organization := "com.julianpeeters"
ThisBuild / description := "Sbt plugin for compiling Avro to Scala"
ThisBuild / version := "2.8.4"
ThisBuild / version := "2.9.0-M2"
ThisBuild / versionScheme := Some("semver-spec")

enablePlugins(SbtPlugin)
Expand All @@ -9,7 +9,15 @@ enablePlugins(SbtPlugin)
(Global / run / connectInput) := true
(Global / run / outputStrategy) := Some(StdoutOutput)

ThisBuild / scalaVersion := "2.12.20"
ThisBuild / crossScalaVersions := Seq("2.12.20", "3.3.4")
ThisBuild / pluginCrossBuild / sbtVersion := {
scalaBinaryVersion.value match {
case "2.12" =>
(pluginCrossBuild / sbtVersion).value
case _ =>
"2.0.0-M2"
}
}
ThisBuild / crossSbtVersions := Seq(sbtVersion.value)
ThisBuild / scalacOptions ++= Seq("-unchecked", "-deprecation", "-feature", "-Ywarn-value-discard")

Expand Down
6 changes: 3 additions & 3 deletions src/main/scala/sbtavrohugger/FileWriter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ object FileWriter {
log.info("Considering source directories %s".format(srcDirs.mkString(",")))
def getSrcFiles(dirs: Seq[File], fileExtension: String) = for {
srcDir <- dirs
srcFile <- (srcDir ** s"*.$fileExtension").get
srcFile <- (srcDir ** s"*.$fileExtension").get()
} yield srcFile

for (inFile <- AvscFileSorter.sortSchemaFiles(getSrcFiles(srcDirs, "avsc"))) {
log.info("Compiling AVSC %s to %s".format(inFile, target.getPath))
generator.fileToFile(inFile, target.getPath)
}

for (idlFile <- AvdlFileSorter.sortSchemaFiles(getSrcFiles(srcDirs, "avdl"))) {
for (idlFile <- AvdlFileSorter.sortSchemaFiles(getSrcFiles(srcDirs, "avdl"))) {
log.info("Compiling Avro IDL %s".format(idlFile))
generator.fileToFile(idlFile, target.getPath)
}
Expand All @@ -41,7 +41,7 @@ object FileWriter {
generator.fileToFile(protocol, target.getPath)
}

(target ** ("*.java"|"*.scala")).get.toSet
(target ** ("*.java"|"*.scala")).get().toSet
}

}
16 changes: 8 additions & 8 deletions src/main/scala/sbtavrohugger/SbtAvrohugger.scala
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ object SbtAvrohugger extends AutoPlugin {
val srcDirs = avroSourceDirectories.value
val targetDir = avroScalaSource.value
val out = streams.value
val majMinV(scalaV) = scalaVersion.value
val majMinV(scalaV) = scalaVersion.value: @unchecked
val customTypes = avroScalaCustomTypes.value
val customNamespace = avroScalaCustomNamespace.value
val res = (Compile / resourceDirectory).value
val old = (Compile/ scalaInstance).value
val res = resourceDirectory.value
val old = scalaInstance.value
val classLoader = new java.net.URLClassLoader(Array(res.toURI().toURL()), old.loader)
val isNumberOfFieldsRestricted = scalaV == "2.10"
val gen = new Generator(
Expand All @@ -81,7 +81,7 @@ object SbtAvrohugger extends AutoPlugin {
outStyle = FilesInfo.exists
) { (in: Set[File]) => FileWriter.generateCaseClasses(gen, srcDirs, targetDir, out.log) }

cachedCompile((srcDirs ** "*.av*").get.toSet).toSeq
cachedCompile((srcDirs ** "*.av*").get().toSet).toSeq
}
)

Expand All @@ -97,11 +97,11 @@ object SbtAvrohugger extends AutoPlugin {
val srcDirs = avroSpecificSourceDirectories.value
val targetDir = avroSpecificScalaSource.value
val out = streams.value
val majMinV(scalaV) = scalaVersion.value
val majMinV(scalaV) = scalaVersion.value: @unchecked
val specificCustomTypes = avroScalaSpecificCustomTypes.value
val specificCustomNamespace = avroScalaSpecificCustomNamespace.value
val res = (Compile / resourceDirectory).value
val old = (Compile / scalaInstance).value
val res = resourceDirectory.value
val old = scalaInstance.value
val classLoader = new java.net.URLClassLoader(Array(res.toURI().toURL()), old.loader)
val isNumberOfFieldsRestricted = scalaV == "2.10"
val gen = new Generator(
Expand All @@ -116,7 +116,7 @@ object SbtAvrohugger extends AutoPlugin {
inStyle = FilesInfo.lastModified,
outStyle = FilesInfo.exists
) { (in: Set[File]) => FileWriter.generateCaseClasses(gen, srcDirs, targetDir, out.log) }
cachedCompile((srcDirs ** "*.av*").get.toSet).toSeq
cachedCompile((srcDirs ** "*.av*").get().toSet).toSeq
}
)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.8.4")
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.9.0-M2")

resolvers += Resolver.file("Local Ivy Repository", file(Path.userHome.absolutePath + "/.ivy2/local/"))(Resolver.ivyStylePatterns)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.8.4")
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.9.0-M2")

resolvers += Resolver.file("Local Ivy Repository", file(Path.userHome.absolutePath + "/.ivy2/local/"))(Resolver.ivyStylePatterns)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.8.4")
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.9.0-M2")

resolvers += Resolver.file("Local Ivy Repository", file(Path.userHome.absolutePath + "/.ivy2/local/"))(Resolver.ivyStylePatterns)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.8.4")
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.9.0-M2")

resolvers += Resolver.file("Local Ivy Repository", file(Path.userHome.absolutePath + "/.ivy2/local/"))(Resolver.ivyStylePatterns)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.8.4")
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.9.0-M2")

resolvers += Resolver.file("Local Ivy Repository", file(Path.userHome.absolutePath + "/.ivy2/local/"))(Resolver.ivyStylePatterns)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.8.4")
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.9.0-M2")

resolvers += Resolver.file("Local Ivy Repository", file(Path.userHome.absolutePath + "/.ivy2/local/"))(Resolver.ivyStylePatterns)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.8.4")
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.9.0-M2")

resolvers += Resolver.file("Local Ivy Repository", file(Path.userHome.absolutePath + "/.ivy2/local/"))(Resolver.ivyStylePatterns)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.8.4")
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.9.0-M2")

resolvers += Resolver.file("Local Ivy Repository", file(Path.userHome.absolutePath + "/.ivy2/local/"))(Resolver.ivyStylePatterns)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.8.4")
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.9.0-M2")

resolvers += Resolver.file("Local Ivy Repository", file("~/.ivy2/local/"))(Resolver.ivyStylePatterns)
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ name := "datatype-specific-serializaton-tests"

version := "0.4-SNAPSHOT"

crossScalaVersions := Seq("2.12.20", "2.13.15")
crossScalaVersions := Seq("2.12.20", "3.3.4")

scalacOptions ++= Seq("-unchecked", "-deprecation", "-feature", "-Ywarn-value-discard")

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.10.2
sbt.version=2.0.0-M2
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.8.4")
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.9.0-M2")

resolvers += Resolver.file("Local Ivy Repository", file(Path.userHome.absolutePath + "/.ivy2/local/"))(Resolver.ivyStylePatterns)

Expand Down
2 changes: 1 addition & 1 deletion src/sbt-test/avrohugger/SpecificADTSerializationTests/test
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

> test

> ++2.13.15
> ++3.3.4

> update

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.8.4")
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.9.0-M2")

resolvers += Resolver.file("Local Ivy Repository", file(Path.userHome.absolutePath + "/.ivy2/local/"))(Resolver.ivyStylePatterns)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.8.4")
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.9.0-M2")

resolvers += Resolver.file("Local Ivy Repository", file(Path.userHome.absolutePath + "/.ivy2/local/"))(Resolver.ivyStylePatterns)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.8.4")
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.9.0-M2")

resolvers += Resolver.file("Local Ivy Repository", file(Path.userHome.absolutePath + "/.ivy2/local/"))(Resolver.ivyStylePatterns)

Expand Down
2 changes: 1 addition & 1 deletion src/sbt-test/avrohugger/filesorter/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.8.4")
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.9.0-M2")
Original file line number Diff line number Diff line change
@@ -1 +1 @@
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.8.4")
addSbtPlugin("com.julianpeeters" % "sbt-avrohugger" % "2.9.0-M2")
29 changes: 20 additions & 9 deletions src/test/scala/sbtavrohugger/SbtAvroDependenciesSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,31 @@ package sbtavrohugger

import avrohugger.filesorter.AvscFileSorter
import java.io.File

import java.nio.file.Files
import org.specs2.mutable.Specification

class SbtAvroDependenciesSpec extends Specification {
val sourceDir = new File(getClass.getClassLoader.getResource("dependencies").toURI)
val classLoader = getClass.getClassLoader

val tmp = Files.createTempDirectory("dependencies").toFile
val a = Files.copy(classLoader.getResourceAsStream("dependencies/ArrayRef.avsc"), java.nio.file.Path.of(tmp.toString(), "ArrayRef.avsc"))
val b = Files.copy(classLoader.getResourceAsStream("dependencies/SimpleArrayRef.avsc"), java.nio.file.Path.of(tmp.toString(), "SimpleArrayRef.avsc"))
val c = Files.copy(classLoader.getResourceAsStream("dependencies/EnumRef.avsc"), java.nio.file.Path.of(tmp.toString(), "EnumRef.avsc"))
val d = Files.copy(classLoader.getResourceAsStream("dependencies/Z.avsc"), java.nio.file.Path.of(tmp.toString(), "Z.avsc"))
val e = Files.copy(classLoader.getResourceAsStream("dependencies/A.avsc"), java.nio.file.Path.of(tmp.toString(), "A.avsc"))
val f = Files.copy(classLoader.getResourceAsStream("dependencies/MapRef.avsc"), java.nio.file.Path.of(tmp.toString(), "MapRef.avsc"))
val g = Files.copy(classLoader.getResourceAsStream("dependencies/UnionRef.avsc"), java.nio.file.Path.of(tmp.toString(), "UnionRef.avsc"))
val sourceDir = java.nio.file.Path.of("dependencies").toFile()

val targetDir = new File(sourceDir.getParentFile, "generated")

val arrayRef = new File(sourceDir, "ArrayRef.avsc")
val simpleArrayRef = new File(sourceDir, "SimpleArrayRef.avsc")
val enumRef = new File(sourceDir, "EnumRef.avsc")
val zFile = new File(sourceDir, "Z.avsc")
val aFile = new File(sourceDir, "A.avsc")
val mapRef = new File(sourceDir, "MapRef.avsc")
val unionRef = new File(sourceDir, "UnionRef.avsc")
val arrayRef = new File(tmp, "ArrayRef.avsc")
val simpleArrayRef = new File(tmp, "SimpleArrayRef.avsc")
val enumRef = new File(tmp, "EnumRef.avsc")
val zFile = new File(tmp, "Z.avsc")
val aFile = new File(tmp, "A.avsc")
val mapRef = new File(tmp, "MapRef.avsc")
val unionRef = new File(tmp, "UnionRef.avsc")
val sourceFiles = Seq(arrayRef, zFile, unionRef)
val sourceFiles2 = Seq(simpleArrayRef, zFile, unionRef)
val sourceFiles3 = Seq(mapRef, aFile)
Expand Down
16 changes: 10 additions & 6 deletions src/test/scala/sbtavrohugger/SbtAvroSpec.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package sbtavrohugger

import java.io.File

import java.nio.file.Files
import org.specs2.mutable.Specification
import avrohugger.filesorter.{AvdlFileSorter, AvscFileSorter}

Expand All @@ -13,14 +13,18 @@ import scala.collection.mutable.ArrayBuffer
class SbtAvroSpec extends Specification {

val classLoader = getClass.getClassLoader
val sourceDir = new File(classLoader.getResource("avro").toURI)

val tmp = Files.createTempDirectory("avro").toFile
val a = Files.copy(classLoader.getResourceAsStream("avro/a.avsc"), java.nio.file.Path.of(tmp.toString(), "a.avsc"))
val b = Files.copy(classLoader.getResourceAsStream("avro/b.avsc"), java.nio.file.Path.of(tmp.toString(), "b.avsc"))
val c = Files.copy(classLoader.getResourceAsStream("avro/c.avsc"), java.nio.file.Path.of(tmp.toString(), "c.avsc"))
val sourceDir = java.nio.file.Path.of("avro").toFile()
val targetDir = new File(sourceDir.getParentFile, "generated")
val sourceFiles = Seq(new File(sourceDir, "a.avsc"), new File(sourceDir, "b.avsc"), new File(sourceDir, "c.avsc"))
val avdlSourceFiles = ArrayBuffer(new File(sourceDir, "a.avdl"), new File(sourceDir, "foo/a.avdl"), new File(sourceDir, "c.avdl"))
val sourceFiles = Seq(new File(tmp, "a.avsc"), new File(tmp, "b.avsc"), new File(tmp, "c.avsc"))

"Schema files should be sorted with re-used types schemas first, whatever input order" >> {
AvscFileSorter.sortSchemaFiles(sourceFiles) must beEqualTo(Seq(new File(sourceDir, "c.avsc"), new File(sourceDir, "b.avsc"), new File(sourceDir, "a.avsc")))
AvscFileSorter.sortSchemaFiles(sourceFiles.reverse) must beEqualTo(Seq(new File(sourceDir, "c.avsc"), new File(sourceDir, "b.avsc"), new File(sourceDir, "a.avsc")))
AvscFileSorter.sortSchemaFiles(sourceFiles) must beEqualTo(Seq(new File(tmp, "c.avsc"), new File(tmp, "b.avsc"), new File(tmp, "a.avsc")))
AvscFileSorter.sortSchemaFiles(sourceFiles.reverse) must beEqualTo(Seq(new File(tmp, "c.avsc"), new File(tmp, "b.avsc"), new File(tmp, "a.avsc")))
}
//
// "AVDL files should be sorted correctly for imports" >> {
Expand Down

0 comments on commit efde2a5

Please sign in to comment.