From c497656ab447dd378facb9120e2e09ef9cd58f27 Mon Sep 17 00:00:00 2001 From: Michel Davit Date: Thu, 12 Sep 2024 14:16:00 +0200 Subject: [PATCH] Fix test and solidify implementation --- .github/workflows/ci.yml | 2 +- .../sbt/avro/test/TestSpecificRecord.java | 0 .../avro/test/TestSpecificRecordParent.java | 0 .../src/test/resources/avro/_a.avsc | 0 .../src/test/resources/avro/_b.avsc | 0 .../src/test/resources/avro/_c.avsc | 0 .../src/test/resources/avro/_d.avsc | 0 .../src/test/resources/avro/_e.avsc | 0 .../src/test/resources/avro/a.avsc | 0 .../src/test/resources/avro/b.avsc | 0 .../src/test/resources/avro/c.avsc | 0 .../src/test/resources/avro/d.avsc | 0 .../src/test/resources/avro/e.avsc | 0 .../src/test/resources/avro/test_records.avsc | 0 .../sbt/avro/AvroCompilerBridgeSpec.scala | 13 +---- build.sbt | 34 +++++++---- .../scala/com/github/sbt/avro/SbtAvro.scala | 32 +++++----- .../sbt-test/sbt-avro/avscparser/build.sbt | 14 ----- .../AnnotateWithArtifactSchemaParser.scala | 34 ----------- .../avscparser/project/build.properties | 1 - .../sbt-avro/avscparser/project/plugins.sbt | 7 --- .../sbt-avro/avscparser/src/main/avro/a.avsc | 11 ---- .../github/sbt/avro/test/AvscParserSpec.scala | 17 ------ plugin/src/sbt-test/sbt-avro/avscparser/test | 10 ---- plugin/src/sbt-test/sbt-avro/basic/build.sbt | 47 ++++----------- .../sbt-avro/basic/project/Avro.scala | 27 +++++++++ .../basic/project/AvroCrossPlugin.scala | 25 ++++++++ .../sbt-avro/basic/project/plugins.sbt | 2 + plugin/src/sbt-test/sbt-avro/basic/test | 58 ++++++++++--------- .../sbt-test/sbt-avro/publishing/build.sbt | 8 +-- .../sbt-avro/publishing/project/plugins.sbt | 2 - .../src/sbt-test/sbt-avro/recompile/build.sbt | 7 ++- .../sbt-avro/recompile/project/plugins.sbt | 7 --- .../src/sbt-test/sbt-avro/sbt_1.3/build.sbt | 2 + .../sbt-avro/sbt_1.3/project/plugins.sbt | 2 - plugin/src/sbt-test/sbt-avro/sbt_1.3/src | 2 +- plugin/src/sbt-test/sbt-avro/sbt_1.3/test | 37 +++++++++++- .../src/sbt-test/sbt-avro/settings/build.sbt | 7 ++- .../sbt-avro/settings/project/plugins.sbt | 7 --- project/Dependencies.scala | 2 - project/plugins.sbt | 1 + 41 files changed, 195 insertions(+), 223 deletions(-) rename {plugin => bridge}/src/test/java/com/github/sbt/avro/test/TestSpecificRecord.java (100%) rename {plugin => bridge}/src/test/java/com/github/sbt/avro/test/TestSpecificRecordParent.java (100%) rename {plugin => bridge}/src/test/resources/avro/_a.avsc (100%) rename {plugin => bridge}/src/test/resources/avro/_b.avsc (100%) rename {plugin => bridge}/src/test/resources/avro/_c.avsc (100%) rename {plugin => bridge}/src/test/resources/avro/_d.avsc (100%) rename {plugin => bridge}/src/test/resources/avro/_e.avsc (100%) rename {plugin => bridge}/src/test/resources/avro/a.avsc (100%) rename {plugin => bridge}/src/test/resources/avro/b.avsc (100%) rename {plugin => bridge}/src/test/resources/avro/c.avsc (100%) rename {plugin => bridge}/src/test/resources/avro/d.avsc (100%) rename {plugin => bridge}/src/test/resources/avro/e.avsc (100%) rename {plugin => bridge}/src/test/resources/avro/test_records.avsc (100%) rename plugin/src/test/scala/com/github/sbt/avro/SbtAvroSpec.scala => bridge/src/test/scala/com/github/sbt/avro/AvroCompilerBridgeSpec.scala (90%) delete mode 100644 plugin/src/sbt-test/sbt-avro/avscparser/build.sbt delete mode 100644 plugin/src/sbt-test/sbt-avro/avscparser/project/AnnotateWithArtifactSchemaParser.scala delete mode 100644 plugin/src/sbt-test/sbt-avro/avscparser/project/build.properties delete mode 100644 plugin/src/sbt-test/sbt-avro/avscparser/project/plugins.sbt delete mode 100644 plugin/src/sbt-test/sbt-avro/avscparser/src/main/avro/a.avsc delete mode 100644 plugin/src/sbt-test/sbt-avro/avscparser/src/test/scala/com/github/sbt/avro/test/AvscParserSpec.scala delete mode 100644 plugin/src/sbt-test/sbt-avro/avscparser/test create mode 100644 plugin/src/sbt-test/sbt-avro/basic/project/Avro.scala create mode 100644 plugin/src/sbt-test/sbt-avro/basic/project/AvroCrossPlugin.scala mode change 120000 => 100644 plugin/src/sbt-test/sbt-avro/sbt_1.3/test diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3f9cff1..8137c90 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 diff --git a/plugin/src/test/java/com/github/sbt/avro/test/TestSpecificRecord.java b/bridge/src/test/java/com/github/sbt/avro/test/TestSpecificRecord.java similarity index 100% rename from plugin/src/test/java/com/github/sbt/avro/test/TestSpecificRecord.java rename to bridge/src/test/java/com/github/sbt/avro/test/TestSpecificRecord.java diff --git a/plugin/src/test/java/com/github/sbt/avro/test/TestSpecificRecordParent.java b/bridge/src/test/java/com/github/sbt/avro/test/TestSpecificRecordParent.java similarity index 100% rename from plugin/src/test/java/com/github/sbt/avro/test/TestSpecificRecordParent.java rename to bridge/src/test/java/com/github/sbt/avro/test/TestSpecificRecordParent.java diff --git a/plugin/src/test/resources/avro/_a.avsc b/bridge/src/test/resources/avro/_a.avsc similarity index 100% rename from plugin/src/test/resources/avro/_a.avsc rename to bridge/src/test/resources/avro/_a.avsc diff --git a/plugin/src/test/resources/avro/_b.avsc b/bridge/src/test/resources/avro/_b.avsc similarity index 100% rename from plugin/src/test/resources/avro/_b.avsc rename to bridge/src/test/resources/avro/_b.avsc diff --git a/plugin/src/test/resources/avro/_c.avsc b/bridge/src/test/resources/avro/_c.avsc similarity index 100% rename from plugin/src/test/resources/avro/_c.avsc rename to bridge/src/test/resources/avro/_c.avsc diff --git a/plugin/src/test/resources/avro/_d.avsc b/bridge/src/test/resources/avro/_d.avsc similarity index 100% rename from plugin/src/test/resources/avro/_d.avsc rename to bridge/src/test/resources/avro/_d.avsc diff --git a/plugin/src/test/resources/avro/_e.avsc b/bridge/src/test/resources/avro/_e.avsc similarity index 100% rename from plugin/src/test/resources/avro/_e.avsc rename to bridge/src/test/resources/avro/_e.avsc diff --git a/plugin/src/test/resources/avro/a.avsc b/bridge/src/test/resources/avro/a.avsc similarity index 100% rename from plugin/src/test/resources/avro/a.avsc rename to bridge/src/test/resources/avro/a.avsc diff --git a/plugin/src/test/resources/avro/b.avsc b/bridge/src/test/resources/avro/b.avsc similarity index 100% rename from plugin/src/test/resources/avro/b.avsc rename to bridge/src/test/resources/avro/b.avsc diff --git a/plugin/src/test/resources/avro/c.avsc b/bridge/src/test/resources/avro/c.avsc similarity index 100% rename from plugin/src/test/resources/avro/c.avsc rename to bridge/src/test/resources/avro/c.avsc diff --git a/plugin/src/test/resources/avro/d.avsc b/bridge/src/test/resources/avro/d.avsc similarity index 100% rename from plugin/src/test/resources/avro/d.avsc rename to bridge/src/test/resources/avro/d.avsc diff --git a/plugin/src/test/resources/avro/e.avsc b/bridge/src/test/resources/avro/e.avsc similarity index 100% rename from plugin/src/test/resources/avro/e.avsc rename to bridge/src/test/resources/avro/e.avsc diff --git a/plugin/src/test/resources/avro/test_records.avsc b/bridge/src/test/resources/avro/test_records.avsc similarity index 100% rename from plugin/src/test/resources/avro/test_records.avsc rename to bridge/src/test/resources/avro/test_records.avsc diff --git a/plugin/src/test/scala/com/github/sbt/avro/SbtAvroSpec.scala b/bridge/src/test/scala/com/github/sbt/avro/AvroCompilerBridgeSpec.scala similarity index 90% rename from plugin/src/test/scala/com/github/sbt/avro/SbtAvroSpec.scala rename to bridge/src/test/scala/com/github/sbt/avro/AvroCompilerBridgeSpec.scala index 28b474b..22fd712 100644 --- a/plugin/src/test/scala/com/github/sbt/avro/SbtAvroSpec.scala +++ b/bridge/src/test/scala/com/github/sbt/avro/AvroCompilerBridgeSpec.scala @@ -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( @@ -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 @@ -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") diff --git a/build.sbt b/build.sbt index 09ebb6b..bcaa47c 100644 --- a/build.sbt +++ b/build.sbt @@ -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 @@ -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, @@ -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, diff --git a/plugin/src/main/scala/com/github/sbt/avro/SbtAvro.scala b/plugin/src/main/scala/com/github/sbt/avro/SbtAvro.scala index 873da2f..7920fe7 100644 --- a/plugin/src/main/scala/com/github/sbt/avro/SbtAvro.scala +++ b/plugin/src/main/scala/com/github/sbt/avro/SbtAvro.scala @@ -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. @@ -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.") @@ -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, ) ) @@ -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 @@ -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 @@ -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 => @@ -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, @@ -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, diff --git a/plugin/src/sbt-test/sbt-avro/avscparser/build.sbt b/plugin/src/sbt-test/sbt-avro/avscparser/build.sbt deleted file mode 100644 index 6505537..0000000 --- a/plugin/src/sbt-test/sbt-avro/avscparser/build.sbt +++ /dev/null @@ -1,14 +0,0 @@ -import java.util.Collections.{singletonMap, singletonList} -import org.apache.avro.Schema - -name := "avscparser-test" -scalaVersion := "2.13.11" -libraryDependencies ++= Seq( - "org.apache.avro" % "avro" % avroVersion.value, - "org.specs2" %% "specs2-core" % "4.20.8" % Test -) -//avroSchemaParserBuilder := AnnotateWithArtifactSchemaParser -// .newBuilder(projectID.value) -// .copy( -// types = singletonMap("B", Schema.createEnum("B", null, "com.github.sbt.avro.test", singletonList("B1")) -// )) diff --git a/plugin/src/sbt-test/sbt-avro/avscparser/project/AnnotateWithArtifactSchemaParser.scala b/plugin/src/sbt-test/sbt-avro/avscparser/project/AnnotateWithArtifactSchemaParser.scala deleted file mode 100644 index 2e86f87..0000000 --- a/plugin/src/sbt-test/sbt-avro/avscparser/project/AnnotateWithArtifactSchemaParser.scala +++ /dev/null @@ -1,34 +0,0 @@ -import com.github.sbt.avro.mojo.SchemaParserBuilder -import org.apache.avro.Schema -import sbt.ModuleID - -class AnnotateWithArtifactSchemaParser( - moduleID: ModuleID, - types: java.util.Map[String, Schema] -) extends org.apache.avro.Schema.Parser { - - addTypes(types) - - override def parse(file: java.io.File): org.apache.avro.Schema = { - val schema = super.parse(file) - if (schema.getType == org.apache.avro.Schema.Type.RECORD) { - schema.addProp("com.github.sbt.sbt-avro.artifact", moduleID.toString()) - } - schema - } - -} - -object AnnotateWithArtifactSchemaParser { - - case class Builder(moduleID: ModuleID, types: java.util.Map[String, Schema]) - extends SchemaParserBuilder { - - override def build(): Schema.Parser = - new AnnotateWithArtifactSchemaParser(moduleID, types) - } - - def newBuilder(moduleID: ModuleID): AnnotateWithArtifactSchemaParser.Builder = - new Builder(moduleID, java.util.Collections.emptyMap()) - -} diff --git a/plugin/src/sbt-test/sbt-avro/avscparser/project/build.properties b/plugin/src/sbt-test/sbt-avro/avscparser/project/build.properties deleted file mode 100644 index ee4c672..0000000 --- a/plugin/src/sbt-test/sbt-avro/avscparser/project/build.properties +++ /dev/null @@ -1 +0,0 @@ -sbt.version=1.10.1 diff --git a/plugin/src/sbt-test/sbt-avro/avscparser/project/plugins.sbt b/plugin/src/sbt-test/sbt-avro/avscparser/project/plugins.sbt deleted file mode 100644 index 851cc08..0000000 --- a/plugin/src/sbt-test/sbt-avro/avscparser/project/plugins.sbt +++ /dev/null @@ -1,7 +0,0 @@ -sys.props.get("plugin.version") match { - case Some(x) => addSbtPlugin("com.github.sbt" % "sbt-avro" % x) - case _ => sys.error("""|The system property 'plugin.version' is not defined. - |Specify this property using the scriptedLaunchOpts -D.""".stripMargin) -} - -libraryDependencies += "org.apache.avro" % "avro-compiler" % "1.12.0" diff --git a/plugin/src/sbt-test/sbt-avro/avscparser/src/main/avro/a.avsc b/plugin/src/sbt-test/sbt-avro/avscparser/src/main/avro/a.avsc deleted file mode 100644 index 186ad92..0000000 --- a/plugin/src/sbt-test/sbt-avro/avscparser/src/main/avro/a.avsc +++ /dev/null @@ -1,11 +0,0 @@ -{ - "name": "A", - "namespace": "com.github.sbt.avro.test", - "type": "record", - "fields": [ - { - "name": "supportsCustomRegisteredType", - "type": "B" - } - ] -} diff --git a/plugin/src/sbt-test/sbt-avro/avscparser/src/test/scala/com/github/sbt/avro/test/AvscParserSpec.scala b/plugin/src/sbt-test/sbt-avro/avscparser/src/test/scala/com/github/sbt/avro/test/AvscParserSpec.scala deleted file mode 100644 index a82a5e1..0000000 --- a/plugin/src/sbt-test/sbt-avro/avscparser/src/test/scala/com/github/sbt/avro/test/AvscParserSpec.scala +++ /dev/null @@ -1,17 +0,0 @@ -package com.github.sbt.avro.test - -import java.io.File - -import org.apache.avro.Schema -import org.apache.avro.generic.GenericData.StringType -import org.specs2.mutable.Specification - -import com.github.sbt.avro.test.A - -class AvscParserSpec extends Specification { - - "A should have artifact property" >> { - A.getClassSchema().getProp("com.github.sbt.sbt-avro.artifact") == "avscparser-test:avscparser-test:0.1.0-SNAPSHOT" - } - -} diff --git a/plugin/src/sbt-test/sbt-avro/avscparser/test b/plugin/src/sbt-test/sbt-avro/avscparser/test deleted file mode 100644 index e622cb2..0000000 --- a/plugin/src/sbt-test/sbt-avro/avscparser/test +++ /dev/null @@ -1,10 +0,0 @@ -> compile - -$ exists target/scala-2.13/src_managed/compiled_avro/main/com/github/sbt/avro/test/A.java -$ exists target/scala-2.13/src_managed/compiled_avro/main/com/github/sbt/avro/test/B.java - --> test - -> clean - -> compile diff --git a/plugin/src/sbt-test/sbt-avro/basic/build.sbt b/plugin/src/sbt-test/sbt-avro/basic/build.sbt index fb1ad41..1050595 100644 --- a/plugin/src/sbt-test/sbt-avro/basic/build.sbt +++ b/plugin/src/sbt-test/sbt-avro/basic/build.sbt @@ -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" ) - diff --git a/plugin/src/sbt-test/sbt-avro/basic/project/Avro.scala b/plugin/src/sbt-test/sbt-avro/basic/project/Avro.scala new file mode 100644 index 0000000..9a8c266 --- /dev/null +++ b/plugin/src/sbt-test/sbt-avro/basic/project/Avro.scala @@ -0,0 +1,27 @@ +import sbt._ +import Keys._ +import sbtcrossproject._ +import com.github.sbt.avro.SbtAvro + +case class Avro(version: String) extends Platform { + + import CrossPlugin.autoImport._ + import SbtAvro.autoImport._ + + def identifier: String = "avro-" + version.split('.')(1) + def sbtSuffix: String = "Avro_" + version.split('.')(1) + def enable(project: Project): Project = project + .enablePlugins(SbtAvro) + .settings( + avroVersion := version, + // favor shared folder if any + Compile / avroSource := (crossProjectCrossType.value.sharedSrcDir(baseDirectory.value, "main") match { + case Some(src) => src.getParentFile / "avro" + case None => (Compile / avroSource).value + }), + Test / avroSource := (crossProjectCrossType.value.sharedSrcDir(baseDirectory.value, "test") match { + case Some(src) => src.getParentFile / "avro" + case None => (Compile / avroSource).value + }) + ) +} \ No newline at end of file diff --git a/plugin/src/sbt-test/sbt-avro/basic/project/AvroCrossPlugin.scala b/plugin/src/sbt-test/sbt-avro/basic/project/AvroCrossPlugin.scala new file mode 100644 index 0000000..14d0536 --- /dev/null +++ b/plugin/src/sbt-test/sbt-avro/basic/project/AvroCrossPlugin.scala @@ -0,0 +1,25 @@ +import scala.language.implicitConversions + +import sbt._ +import sbtcrossproject._ + +object AvroCrossPlugin extends sbt.AutoPlugin { + object autoImport { + + implicit def AvroCrossProjectBuilderOps(builder: CrossProject.Builder): AvroCrossProjectOps = + new AvroCrossProjectOps(builder.crossType(CrossType.Full)) + + implicit class AvroCrossProjectOps(project: CrossProject) { + def avro(version: String): Project = project.projects(Avro(version)) + + def avroSettings(version: String)(ss: Def.SettingsDefinition*): CrossProject = + avroConfigure(version)(_.settings(ss: _*)) + + def avroEnablePlugins(version: String)(plugins: Plugins*): CrossProject = + avroConfigure(version)(_.enablePlugins(plugins: _*)) + + def avroConfigure(version: String)(transformer: Project => Project): CrossProject = + project.configurePlatform(Avro(version))(transformer) + } + } +} \ No newline at end of file diff --git a/plugin/src/sbt-test/sbt-avro/basic/project/plugins.sbt b/plugin/src/sbt-test/sbt-avro/basic/project/plugins.sbt index c52f4d5..a97f6e3 100644 --- a/plugin/src/sbt-test/sbt-avro/basic/project/plugins.sbt +++ b/plugin/src/sbt-test/sbt-avro/basic/project/plugins.sbt @@ -3,3 +3,5 @@ sys.props.get("plugin.version") match { case _ => sys.error("""|The system property 'plugin.version' is not defined. |Specify this property using the scriptedLaunchOpts -D.""".stripMargin) } + +addSbtPlugin("org.portable-scala" % "sbt-crossproject" % "1.3.2") diff --git a/plugin/src/sbt-test/sbt-avro/basic/test b/plugin/src/sbt-test/sbt-avro/basic/test index 2e9fddb..85c19dd 100644 --- a/plugin/src/sbt-test/sbt-avro/basic/test +++ b/plugin/src/sbt-test/sbt-avro/basic/test @@ -1,38 +1,44 @@ #> set avroSchemaParserBuilder := com.github.sbt.avro.NameValidatorSchemaParserBuilder(validateDefaults = false) + > avroGenerate -$ exists target/scala-2.13/src_managed/compiled_avro/main/com/github/sbt/avro/test/A.java -$ exists target/scala-2.13/src_managed/compiled_avro/main/com/github/sbt/avro/test/B.java -$ exists target/scala-2.13/src_managed/compiled_avro/main/com/github/sbt/avro/test/C.java -$ exists target/scala-2.13/src_managed/compiled_avro/main/com/github/sbt/avro/test/D.java -$ exists target/scala-2.13/src_managed/compiled_avro/main/com/github/sbt/avro/test/E.java -$ exists target/scala-2.13/src_managed/compiled_avro/main/com/github/sbt/avro/test/_A.java -$ exists target/scala-2.13/src_managed/compiled_avro/main/com/github/sbt/avro/test/_B.java -$ exists target/scala-2.13/src_managed/compiled_avro/main/com/github/sbt/avro/test/_C.java -$ exists target/scala-2.13/src_managed/compiled_avro/main/com/github/sbt/avro/test/_D.java -$ exists target/scala-2.13/src_managed/compiled_avro/main/com/github/sbt/avro/test/_E.java +$ exists .avro-12/target/scala-2.13/src_managed/compiled_avro/main/com/github/sbt/avro/test/A.java +$ exists .avro-12/target/scala-2.13/src_managed/compiled_avro/main/com/github/sbt/avro/test/B.java +$ exists .avro-12/target/scala-2.13/src_managed/compiled_avro/main/com/github/sbt/avro/test/C.java +$ exists .avro-12/target/scala-2.13/src_managed/compiled_avro/main/com/github/sbt/avro/test/D.java +$ exists .avro-12/target/scala-2.13/src_managed/compiled_avro/main/com/github/sbt/avro/test/E.java +$ exists .avro-12/target/scala-2.13/src_managed/compiled_avro/main/com/github/sbt/avro/test/_A.java +$ exists .avro-12/target/scala-2.13/src_managed/compiled_avro/main/com/github/sbt/avro/test/_B.java +$ exists .avro-12/target/scala-2.13/src_managed/compiled_avro/main/com/github/sbt/avro/test/_C.java +$ exists .avro-12/target/scala-2.13/src_managed/compiled_avro/main/com/github/sbt/avro/test/_D.java +$ exists .avro-12/target/scala-2.13/src_managed/compiled_avro/main/com/github/sbt/avro/test/_E.java + +$ exists .avro-11/target/scala-2.13/src_managed/compiled_avro/main/com/github/sbt/avro/test/A.java +$ exists .avro-10/target/scala-2.13/src_managed/compiled_avro/main/com/github/sbt/avro/test/A.java +$ exists .avro-9/target/scala-2.13/src_managed/compiled_avro/main/com/github/sbt/avro/test/A.java +$ exists .avro-8/target/scala-2.13/src_managed/compiled_avro/main/com/github/sbt/avro/test/A.java > compile -$ exists target/scala-2.13/classes/com/github/sbt/avro/test/A.class -$ exists target/scala-2.13/classes/com/github/sbt/avro/test/B.class -$ exists target/scala-2.13/classes/com/github/sbt/avro/test/C.class -$ exists target/scala-2.13/classes/com/github/sbt/avro/test/D.class -$ exists target/scala-2.13/classes/com/github/sbt/avro/test/E.class -$ exists target/scala-2.13/classes/com/github/sbt/avro/test/_A.class -$ exists target/scala-2.13/classes/com/github/sbt/avro/test/_B.class -$ exists target/scala-2.13/classes/com/github/sbt/avro/test/_C.class -$ exists target/scala-2.13/classes/com/github/sbt/avro/test/_D.class -$ exists target/scala-2.13/classes/com/github/sbt/avro/test/_E.class +$ exists .avro-12/target/scala-2.13/classes/com/github/sbt/avro/test/A.class +$ exists .avro-12/target/scala-2.13/classes/com/github/sbt/avro/test/B.class +$ exists .avro-12/target/scala-2.13/classes/com/github/sbt/avro/test/C.class +$ exists .avro-12/target/scala-2.13/classes/com/github/sbt/avro/test/D.class +$ exists .avro-12/target/scala-2.13/classes/com/github/sbt/avro/test/E.class +$ exists .avro-12/target/scala-2.13/classes/com/github/sbt/avro/test/_A.class +$ exists .avro-12/target/scala-2.13/classes/com/github/sbt/avro/test/_B.class +$ exists .avro-12/target/scala-2.13/classes/com/github/sbt/avro/test/_C.class +$ exists .avro-12/target/scala-2.13/classes/com/github/sbt/avro/test/_D.class +$ exists .avro-12/target/scala-2.13/classes/com/github/sbt/avro/test/_E.class > Test/compile -$ exists target/scala-2.13/src_managed/compiled_avro/test/com/github/sbt/avro/test/X.java -$ exists target/scala-2.13/src_managed/compiled_avro/test/com/github/sbt/avro/test/Y.java -$ exists target/scala-2.13/src_managed/compiled_avro/test/com/github/sbt/avro/test/Z.java -$ exists target/scala-2.13/test-classes/com/github/sbt/avro/test/X.class -$ exists target/scala-2.13/test-classes/com/github/sbt/avro/test/Y.class -$ exists target/scala-2.13/test-classes/com/github/sbt/avro/test/Z.class +$ exists .avro-12/target/scala-2.13/src_managed/compiled_avro/test/com/github/sbt/avro/test/X.java +$ exists .avro-12/target/scala-2.13/src_managed/compiled_avro/test/com/github/sbt/avro/test/Y.java +$ exists .avro-12/target/scala-2.13/src_managed/compiled_avro/test/com/github/sbt/avro/test/Z.java +$ exists .avro-12/target/scala-2.13/test-classes/com/github/sbt/avro/test/X.class +$ exists .avro-12/target/scala-2.13/test-classes/com/github/sbt/avro/test/Y.class +$ exists .avro-12/target/scala-2.13/test-classes/com/github/sbt/avro/test/Z.class > clean diff --git a/plugin/src/sbt-test/sbt-avro/publishing/build.sbt b/plugin/src/sbt-test/sbt-avro/publishing/build.sbt index f72cb18..ca6bb33 100644 --- a/plugin/src/sbt-test/sbt-avro/publishing/build.sbt +++ b/plugin/src/sbt-test/sbt-avro/publishing/build.sbt @@ -1,14 +1,12 @@ lazy val commonSettings = Seq( organization := "com.github.sbt", publishTo := Some(Opts.resolver.sonatypeReleases), - scalaVersion := "2.13.11", - libraryDependencies ++= Seq( - "org.apache.avro" % "avro" % avroVersion.value - ) + scalaVersion := "2.13.11" ) lazy val `external`: Project = project .in(file("external")) + .enablePlugins(SbtAvro) .settings(commonSettings) .settings( name := "external", @@ -20,6 +18,7 @@ lazy val `external`: Project = project lazy val `transitive`: Project = project .in(file("transitive")) + .enablePlugins(SbtAvro) .settings(commonSettings) .settings( name := "transitive", @@ -33,6 +32,7 @@ lazy val `transitive`: Project = project lazy val root: Project = project .in(file(".")) + .enablePlugins(SbtAvro) .settings(commonSettings) .settings( name := "publishing-test", diff --git a/plugin/src/sbt-test/sbt-avro/publishing/project/plugins.sbt b/plugin/src/sbt-test/sbt-avro/publishing/project/plugins.sbt index 851cc08..c52f4d5 100644 --- a/plugin/src/sbt-test/sbt-avro/publishing/project/plugins.sbt +++ b/plugin/src/sbt-test/sbt-avro/publishing/project/plugins.sbt @@ -3,5 +3,3 @@ sys.props.get("plugin.version") match { case _ => sys.error("""|The system property 'plugin.version' is not defined. |Specify this property using the scriptedLaunchOpts -D.""".stripMargin) } - -libraryDependencies += "org.apache.avro" % "avro-compiler" % "1.12.0" diff --git a/plugin/src/sbt-test/sbt-avro/recompile/build.sbt b/plugin/src/sbt-test/sbt-avro/recompile/build.sbt index 9631aa3..65bcf5f 100644 --- a/plugin/src/sbt-test/sbt-avro/recompile/build.sbt +++ b/plugin/src/sbt-test/sbt-avro/recompile/build.sbt @@ -1,7 +1,10 @@ +enablePlugins(SbtAvro) + name := "recompile-test" scalaVersion := "2.13.11" libraryDependencies ++= Seq( - "org.apache.avro" % "avro" % avroVersion.value, + // depend on test jar to get some generated records in the build + "org.apache.avro" % "avro" % avroVersion.value % "avro" classifier "tests", "org.specs2" %% "specs2-core" % "4.20.8" % Test ) @@ -9,4 +12,4 @@ avroStringType := "String" avroFieldVisibility := "public" avroOptionalGetters := true avroEnableDecimalLogicalType := false -Compile / avroSpecificRecords += classOf[org.apache.avro.specific.TestRecordWithLogicalTypes] +Compile / avroSpecificRecords += "org.apache.avro.specific.TestRecordWithLogicalTypes" diff --git a/plugin/src/sbt-test/sbt-avro/recompile/project/plugins.sbt b/plugin/src/sbt-test/sbt-avro/recompile/project/plugins.sbt index 9fbf75b..82d3c49 100644 --- a/plugin/src/sbt-test/sbt-avro/recompile/project/plugins.sbt +++ b/plugin/src/sbt-test/sbt-avro/recompile/project/plugins.sbt @@ -4,10 +4,3 @@ sys.props.get("plugin.version") match { case _ => sys.error("""|The system property 'plugin.version' is not defined. |Specify this property using the scriptedLaunchOpts -D.""".stripMargin) } - -val avroVersion = "1.12.0" -libraryDependencies ++= Seq( - "org.apache.avro" % "avro-compiler" % avroVersion, - // depend on test jar to get some generated records in the build - "org.apache.avro" % "avro" % avroVersion classifier "tests" -) diff --git a/plugin/src/sbt-test/sbt-avro/sbt_1.3/build.sbt b/plugin/src/sbt-test/sbt-avro/sbt_1.3/build.sbt index eae4c72..391e135 100644 --- a/plugin/src/sbt-test/sbt-avro/sbt_1.3/build.sbt +++ b/plugin/src/sbt-test/sbt-avro/sbt_1.3/build.sbt @@ -1,3 +1,5 @@ +enablePlugins(SbtAvro) + name := "basic-test" scalaVersion := "2.13.11" libraryDependencies += "org.apache.avro" % "avro" % avroVersion.value diff --git a/plugin/src/sbt-test/sbt-avro/sbt_1.3/project/plugins.sbt b/plugin/src/sbt-test/sbt-avro/sbt_1.3/project/plugins.sbt index 851cc08..c52f4d5 100644 --- a/plugin/src/sbt-test/sbt-avro/sbt_1.3/project/plugins.sbt +++ b/plugin/src/sbt-test/sbt-avro/sbt_1.3/project/plugins.sbt @@ -3,5 +3,3 @@ sys.props.get("plugin.version") match { case _ => sys.error("""|The system property 'plugin.version' is not defined. |Specify this property using the scriptedLaunchOpts -D.""".stripMargin) } - -libraryDependencies += "org.apache.avro" % "avro-compiler" % "1.12.0" diff --git a/plugin/src/sbt-test/sbt-avro/sbt_1.3/src b/plugin/src/sbt-test/sbt-avro/sbt_1.3/src index 2c57068..12c7604 120000 --- a/plugin/src/sbt-test/sbt-avro/sbt_1.3/src +++ b/plugin/src/sbt-test/sbt-avro/sbt_1.3/src @@ -1 +1 @@ -../basic_current/src \ No newline at end of file +../basic/src \ No newline at end of file diff --git a/plugin/src/sbt-test/sbt-avro/sbt_1.3/test b/plugin/src/sbt-test/sbt-avro/sbt_1.3/test deleted file mode 120000 index 088b752..0000000 --- a/plugin/src/sbt-test/sbt-avro/sbt_1.3/test +++ /dev/null @@ -1 +0,0 @@ -../basic_current/test \ No newline at end of file diff --git a/plugin/src/sbt-test/sbt-avro/sbt_1.3/test b/plugin/src/sbt-test/sbt-avro/sbt_1.3/test new file mode 100644 index 0000000..f7e3997 --- /dev/null +++ b/plugin/src/sbt-test/sbt-avro/sbt_1.3/test @@ -0,0 +1,36 @@ +> avroGenerate + +$ exists target/scala-2.13/src_managed/compiled_avro/main/com/github/sbt/avro/test/A.java +$ exists target/scala-2.13/src_managed/compiled_avro/main/com/github/sbt/avro/test/B.java +$ exists target/scala-2.13/src_managed/compiled_avro/main/com/github/sbt/avro/test/C.java +$ exists target/scala-2.13/src_managed/compiled_avro/main/com/github/sbt/avro/test/D.java +$ exists target/scala-2.13/src_managed/compiled_avro/main/com/github/sbt/avro/test/E.java +$ exists target/scala-2.13/src_managed/compiled_avro/main/com/github/sbt/avro/test/_A.java +$ exists target/scala-2.13/src_managed/compiled_avro/main/com/github/sbt/avro/test/_B.java +$ exists target/scala-2.13/src_managed/compiled_avro/main/com/github/sbt/avro/test/_C.java +$ exists target/scala-2.13/src_managed/compiled_avro/main/com/github/sbt/avro/test/_D.java +$ exists target/scala-2.13/src_managed/compiled_avro/main/com/github/sbt/avro/test/_E.java + +> compile + +$ exists target/scala-2.13/classes/com/github/sbt/avro/test/A.class +$ exists target/scala-2.13/classes/com/github/sbt/avro/test/B.class +$ exists target/scala-2.13/classes/com/github/sbt/avro/test/C.class +$ exists target/scala-2.13/classes/com/github/sbt/avro/test/D.class +$ exists target/scala-2.13/classes/com/github/sbt/avro/test/E.class +$ exists target/scala-2.13/classes/com/github/sbt/avro/test/_A.class +$ exists target/scala-2.13/classes/com/github/sbt/avro/test/_B.class +$ exists target/scala-2.13/classes/com/github/sbt/avro/test/_C.class +$ exists target/scala-2.13/classes/com/github/sbt/avro/test/_D.class +$ exists target/scala-2.13/classes/com/github/sbt/avro/test/_E.class + +> Test/compile + +$ exists target/scala-2.13/src_managed/compiled_avro/test/com/github/sbt/avro/test/X.java +$ exists target/scala-2.13/src_managed/compiled_avro/test/com/github/sbt/avro/test/Y.java +$ exists target/scala-2.13/src_managed/compiled_avro/test/com/github/sbt/avro/test/Z.java +$ exists target/scala-2.13/test-classes/com/github/sbt/avro/test/X.class +$ exists target/scala-2.13/test-classes/com/github/sbt/avro/test/Y.class +$ exists target/scala-2.13/test-classes/com/github/sbt/avro/test/Z.class + +> clean diff --git a/plugin/src/sbt-test/sbt-avro/settings/build.sbt b/plugin/src/sbt-test/sbt-avro/settings/build.sbt index 1e74f0f..242a8f8 100644 --- a/plugin/src/sbt-test/sbt-avro/settings/build.sbt +++ b/plugin/src/sbt-test/sbt-avro/settings/build.sbt @@ -1,7 +1,10 @@ +addSbtPlugins(SbtAvro) + name := "settings-test" scalaVersion := "2.13.11" libraryDependencies ++= Seq( - "org.apache.avro" % "avro" % avroVersion.value, + // depend on test jar to get some generated records in the build + "org.apache.avro" % "avro" % avroVersion.value % Avro classifier "tests", "org.specs2" %% "specs2-core" % "4.20.8" % Test ) @@ -9,6 +12,6 @@ avroStringType := "String" avroFieldVisibility := "public" avroOptionalGetters := true avroEnableDecimalLogicalType := false -Compile / avroSpecificRecords += classOf[org.apache.avro.specific.TestRecordWithLogicalTypes] +Compile / avroSpecificRecords += "org.apache.avro.specific.TestRecordWithLogicalTypes" Compile / avroSource := (Compile / sourceDirectory).value / "avro_source" Compile / avroGenerate / target := (Compile / sourceManaged).value diff --git a/plugin/src/sbt-test/sbt-avro/settings/project/plugins.sbt b/plugin/src/sbt-test/sbt-avro/settings/project/plugins.sbt index 7f899a1..c52f4d5 100644 --- a/plugin/src/sbt-test/sbt-avro/settings/project/plugins.sbt +++ b/plugin/src/sbt-test/sbt-avro/settings/project/plugins.sbt @@ -3,10 +3,3 @@ sys.props.get("plugin.version") match { case _ => sys.error("""|The system property 'plugin.version' is not defined. |Specify this property using the scriptedLaunchOpts -D.""".stripMargin) } - -val avroVersion = "1.12.0" -libraryDependencies ++= Seq( - "org.apache.avro" % "avro-compiler" % avroVersion, - // depend on test jar to get some generated records in the build - "org.apache.avro" % "avro" % avroVersion classifier "tests" -) diff --git a/project/Dependencies.scala b/project/Dependencies.scala index f3dd176..1606a55 100644 --- a/project/Dependencies.scala +++ b/project/Dependencies.scala @@ -7,8 +7,6 @@ object Dependencies { val Specs2 = "4.20.8" } - val - object Provided { val AvroCompiler = "org.apache.avro" % "avro-compiler" % Versions.Avro % "provided" } diff --git a/project/plugins.sbt b/project/plugins.sbt index 8691f70..9891926 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -1,2 +1,3 @@ +addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.12.0") addSbtPlugin("com.github.sbt" % "sbt-github-actions" % "0.24.0") addSbtPlugin("com.github.sbt" % "sbt-ci-release" % "1.6.0")