Skip to content

Commit

Permalink
Cross build to sbt v2
Browse files Browse the repository at this point in the history
  • Loading branch information
RustedBones committed Oct 23, 2024
1 parent 19ada60 commit e327015
Show file tree
Hide file tree
Showing 30 changed files with 254 additions and 238 deletions.
14 changes: 12 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
scala: [2.12.20]
scala: [3.3.4, 2.12.20]
java: [temurin@11]
runs-on: ${{ matrix.os }}
steps:
Expand Down Expand Up @@ -68,7 +68,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
scala: [2.12.20]
scala: [3.3.4]
java: [temurin@11]
runs-on: ${{ matrix.os }}
steps:
Expand All @@ -88,6 +88,16 @@ jobs:
- name: Setup sbt
uses: sbt/setup-sbt@v1

- name: Download target directories (3.3.4)
uses: actions/download-artifact@v4
with:
name: target-${{ matrix.os }}-3.3.4-${{ matrix.java }}

- name: Inflate target directories (3.3.4)
run: |
tar xf targets.tar
rm targets.tar
- name: Download target directories (2.12.20)
uses: actions/download-artifact@v4
with:
Expand Down
6 changes: 6 additions & 0 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
version = "3.8.3"
runner.dialect = scala212

fileOverride {
"glob:**/scala-3/**" {
runner.dialect = scala3
}
}

maxColumn = 100
lineEndings=preserve
binPack.literalArgumentLists = true
Expand Down
18 changes: 16 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ ThisBuild / developers := List(
)

// sbt-github-actions
ThisBuild / scalaVersion := "2.12.20"
lazy val scala3 = "3.3.4"
lazy val scala212 = "2.12.20"
ThisBuild / scalaVersion := scala3
ThisBuild / crossScalaVersions := Seq(scala3, scala212)
ThisBuild / githubWorkflowBuild := Seq(
WorkflowStep.Sbt(name = Some("Build project"), commands = List("compile", "test", "scripted"))
)
Expand Down Expand Up @@ -96,9 +99,20 @@ lazy val `sbt-avro`: Project = project
.enablePlugins(BuildInfoPlugin, SbtPlugin)
.settings(
description := "Sbt plugin for compiling Avro sources",
(pluginCrossBuild / sbtVersion) := {
scalaBinaryVersion.value match {
case "2.12" => "1.5.0"
case _ => "2.0.0-M2"
}
},
scriptedSbt := {
scalaBinaryVersion.value match {
case "2.12" => "1.10.3"
case _ => "2.0.0-M2"
}
},
buildInfoKeys := Seq[BuildInfoKey](name, version),
buildInfoPackage := "com.github.sbt.avro",
pluginCrossBuild / sbtVersion := "1.3.0",
Compile / scalacOptions ++= Seq("-deprecation"),
scriptedLaunchOpts ++= Seq(
"-Xmx1024M",
Expand Down
14 changes: 14 additions & 0 deletions plugin/src/main/scala-2.12/com/github/sbt/avro/PluginCompat.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.github.sbt.avro
import sbt.*
import xsbti.FileConverter

import java.nio.file.{Path => NioPath}

private[avro] object PluginCompat {
type FileRef = java.io.File
type Out = java.io.File

def toNioPath(a: Attributed[File])(implicit conv: FileConverter): NioPath =
a.data.toPath()
def toFileRef(f: File): FileRef = f
}
14 changes: 14 additions & 0 deletions plugin/src/main/scala-3/com/github/sbt/avro/PluginCompat.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.github.sbt.avro

import java.nio.file.{Path => NioPath}
import sbt.*
import xsbti.{FileConverter, HashedVirtualFileRef, VirtualFile}

private[avro] object PluginCompat:
type FileRef = HashedVirtualFileRef
type Out = VirtualFile

def toNioPath(a: Attributed[HashedVirtualFileRef])(using conv: FileConverter): NioPath =
conv.toPath(a.data)

def toFileRef(f: File)(using conv: FileConverter): FileRef = conv.toVirtualFile(f.toPath)
36 changes: 23 additions & 13 deletions plugin/src/main/scala/com/github/sbt/avro/SbtAvro.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.github.sbt.avro

import sbt.Keys.*
import sbt.*
import sbt.{*, given}
import Path.relativeTo
import PluginCompat.*
import sbt.librarymanagement.DependencyFilter

import java.io.File
Expand Down Expand Up @@ -45,10 +46,11 @@ object SbtAvro extends AutoPlugin {

val avroGenerate = taskKey[Seq[File]]("Generate Java sources for Avro schemas.")
val avroUnpackDependencies = taskKey[Seq[File]]("Unpack avro dependencies.")
val packageAvro = taskKey[File]("Produces an avro artifact, such as a jar containing avro schemas.")
val packageAvro = taskKey[FileRef]("Produces an avro artifact, such as a jar containing avro schemas.")
// format: on

lazy val avroArtifactTasks: Seq[TaskKey[File]] = Seq(Compile, Test).map(_ / packageAvro)
lazy val avroArtifactTasks: Seq[TaskKey[FileRef]] =
Seq(Compile, Test).map(conf => conf / packageAvro)

lazy val defaultSettings: Seq[Setting[_]] = Seq(
// compiler
Expand Down Expand Up @@ -108,8 +110,11 @@ object SbtAvro extends AutoPlugin {

import autoImport._

def packageAvroMappings: Def.Initialize[Task[Seq[(File, String)]]] = Def.task {
avroUnmanagedSourceDirectories.value.flatMap(src => (src ** AvroFilter).pair(relativeTo(src)))
def packageAvroMappings: Def.Initialize[Task[Seq[(FileRef, String)]]] = Def.task {
implicit val conv: xsbti.FileConverter = fileConverter.value
avroUnmanagedSourceDirectories.value
.flatMap(src => (src ** AvroFilter).pair(relativeTo(src)))
.map { case (p, path) => toFileRef(p) -> path }
}

override def trigger: PluginTrigger = noTrigger
Expand Down Expand Up @@ -158,6 +163,7 @@ object SbtAvro extends AutoPlugin {
private def unpackDependenciesTask(key: TaskKey[Seq[File]]) = Def.task {
val cacheBaseDirectory = Defaults.makeCrossTarget(
streams.value.cacheDirectory,
scalaVersion.value,
scalaBinaryVersion.value,
(pluginCrossBuild / sbtBinaryVersion).value,
sbtPlugin.value,
Expand All @@ -184,6 +190,7 @@ object SbtAvro extends AutoPlugin {
val externalSrcDir = (avroUnpackDependencies / target).value
val srcDirs = avroUnmanagedSourceDirectories.value :+ externalSrcDir
val outDir = (key / target).value
implicit val conv: xsbti.FileConverter = fileConverter.value // used by PluginCompat

val cachedCompile = {
import sbt.util.CacheStoreFactory
Expand Down Expand Up @@ -215,7 +222,10 @@ object SbtAvro extends AutoPlugin {
// TODO Cache class loader
val avroClassLoader = new URLClassLoader(
"AvroClassLoader",
(Avro / dependencyClasspath).value.map(_.data.toURI.toURL).toArray,
(Avro / dependencyClasspath).value
.map(toNioPath)
.map(_.toUri.toURL)
.toArray,
this.getClass.getClassLoader
)

Expand All @@ -234,13 +244,13 @@ object SbtAvro extends AutoPlugin {

try {
val recs = records.map(avroClassLoader.loadClass)
val avdls = srcDirs.flatMap(d => (d ** AvroAvdlFilter).get)
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)
)
(d ** AvroAvscFilter)
.get()
.map(avsc => new AvroFileRef(d, avsc.relativeTo(d).get.toString))
)
val avprs = srcDirs.flatMap(d => (d ** AvroAvrpFilter).get)
val avprs = srcDirs.flatMap(d => (d ** AvroAvrpFilter).get())

out.log.info(
s"Avro compiler ${avroVersion.value} using stringType=${avroStringType.value}"
Expand All @@ -251,7 +261,7 @@ object SbtAvro extends AutoPlugin {
compiler.compileAvscs(avscs.toArray, outDir)
compiler.compileAvprs(avprs.toArray, outDir)

(outDir ** SbtAvro.JavaFileFilter).get.toSet
(outDir ** SbtAvro.JavaFileFilter).get().toSet
} finally {
avroClassLoader.close()
}
Expand All @@ -263,7 +273,7 @@ object SbtAvro extends AutoPlugin {
}
}

cachedCompile((srcDirs ** AvroFilter).get.toSet, avroSpecificRecords.value).toSeq
cachedCompile((srcDirs ** AvroFilter).get().toSet, avroSpecificRecords.value).toSeq
}

}

This file was deleted.

103 changes: 91 additions & 12 deletions plugin/src/sbt-test/sbt-avro/basic/build.sbt
Original file line number Diff line number Diff line change
@@ -1,17 +1,96 @@
ThisBuild / scalaVersion := "2.13.11"

lazy val basic = crossProject(
Avro("1.12.0"),
Avro("1.11.3"),
Avro("1.10.0"),
Avro("1.9.2"),
Avro("1.8.2"),
)
.crossType(CrossType.Pure)
lazy val files = Seq(
"A", "B", "C", "D", "E", "_A", "_B", "_C", "_D", "_E", "LogicalTypesTest"
)

lazy val testFiles = Seq(
"X", "Y", "Z"
)

val checkGenerated = TaskKey[Unit]("checkGenerated")
val checkCompiled = TaskKey[Unit]("checkCompiled")

def exists(f: File): Unit = assert(f.exists(), s"$f does not exist")

val checkSettings = inConfig(Compile)(Def.settings(
checkGenerated := {
files.foreach { f =>
exists(crossTarget.value / "src_managed" / "compiled_avro" / "main" / "com" / "github" / "sbt" / "avro" / "test" / s"$f.java")
}
},
checkCompiled := {
files.foreach { f =>
exists(classDirectory.value / "com" / "github" / "sbt" / "avro" / "test" / s"$f.class")
}
}
)) ++ inConfig(Test)(Def.settings(
checkGenerated := {
testFiles.foreach { f =>
exists(crossTarget.value / "src_managed" / "compiled_avro" / "test" / "com" / "github" / "sbt" / "avro" / "test" / s"$f.java")
}
},
checkCompiled := {
testFiles.foreach { f =>
exists(classDirectory.value / "com" / "github" / "sbt" / "avro" / "test" / s"$f.class")
}
}
))


lazy val `basic` = project
.in(file("."))
.avroSettings("1.9.2")(
libraryDependencies += "joda-time" % "joda-time" % "2.12.7"
.aggregate(
`basic8`,
`basic9`,
`basic10`,
`basic11`,
`basic12`
)

lazy val `basic8` = project
.in(file(".basic_8"))
.enablePlugins(SbtAvro)
.settings(checkSettings)
.settings(
avroVersion := "1.8.2",
libraryDependencies += "joda-time" % "joda-time" % "2.12.7",
sourceDirectory := file(".") / "src"
)

lazy val `basic9` = project
.in(file(".basic_9"))
.enablePlugins(SbtAvro)
.settings(checkSettings)
.settings(
avroVersion := "1.9.2",
libraryDependencies += "joda-time" % "joda-time" % "2.12.7",
sourceDirectory := file(".") / "src"
)

lazy val `basic10` = project
.in(file(".basic_10"))
.enablePlugins(SbtAvro)
.settings(checkSettings)
.settings(
avroVersion := "1.10.0",
sourceDirectory := file(".") / "src"
)

lazy val `basic11` = project
.in(file(".basic_11"))
.enablePlugins(SbtAvro)
.settings(checkSettings)
.settings(
avroVersion := "1.11.3",
sourceDirectory := file(".") / "src"
)
.avroSettings("1.8.2")(
libraryDependencies += "joda-time" % "joda-time" % "2.12.7"

lazy val `basic12` = project
.in(file(".basic_12"))
.enablePlugins(SbtAvro)
.settings(checkSettings)
.settings(
avroVersion := "1.12.0",
sourceDirectory := file(".") / "src"
)
24 changes: 0 additions & 24 deletions plugin/src/sbt-test/sbt-avro/basic/project/Avro.scala

This file was deleted.

25 changes: 0 additions & 25 deletions plugin/src/sbt-test/sbt-avro/basic/project/AvroCrossPlugin.scala

This file was deleted.

This file was deleted.

2 changes: 0 additions & 2 deletions plugin/src/sbt-test/sbt-avro/basic/project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

addSbtPlugin("org.portable-scala" % "sbt-crossproject" % "1.3.2")
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "LogicalTypesTest",
"namespace": "org.apache.parquet.avro",
"namespace": "com.github.sbt.avro.test",
"doc": "Record for testing logical types",
"type": "record",
"fields": [
Expand Down
Loading

0 comments on commit e327015

Please sign in to comment.