Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow dependency on project with avro scope #220

Merged
merged 1 commit into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions plugin/src/main/scala/com/github/sbt/avro/SbtAvro.scala
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ object SbtAvro extends AutoPlugin {
// source generation
avroGenerate / target := configSrcSub(avroGenerate / target).value,
managedSourceDirectories += (avroGenerate / target).value,
avroGenerate := sourceGeneratorTask(avroGenerate).dependsOn(avroUnpackDependencies).value,
avroGenerate := sourceGeneratorTask(avroGenerate)
.dependsOn(avroUnpackDependencies)
.dependsOn(avroUnpackDependencies.?.all(filterDependsOn))
.value,
sourceGenerators += avroGenerate.taskValue,
compile := compile.dependsOn(avroGenerate).value,
// packaging
Expand Down Expand Up @@ -126,6 +129,13 @@ object SbtAvro extends AutoPlugin {
inConfig(Avro)(Defaults.configSettings) ++
Seq(Compile, Test).flatMap(c => inConfig(c)(configScopedSettings))

// This filter is meant evaluate for all dependant submodules
// eg. source files / unpack dependencies
private val filterDependsOn = ScopeFilter(
inDependencies(ThisProject, transitive = false),
inConfigurations(Compile)
)

private def unpack(
cacheBaseDirectory: File,
deps: Seq[File],
Expand Down Expand Up @@ -196,7 +206,11 @@ object SbtAvro extends AutoPlugin {
private def sourceGeneratorTask(key: TaskKey[Seq[File]]) = Def.task {
val out = (key / streams).value
val externalSrcDir = (avroUnpackDependencies / target).value
val srcDirs = avroUnmanagedSourceDirectories.value :+ externalSrcDir
val unmanagedSrcDirs = avroUnmanagedSourceDirectories.value
val dependsOnDirs = (avroUnpackDependencies / target).?.all(filterDependsOn).value.flatten ++
avroUnmanagedSourceDirectories.?.all(filterDependsOn).value.flatten.flatten
val srcDirs = Seq(externalSrcDir) ++ unmanagedSrcDirs ++ dependsOnDirs

val outDir = (key / target).value
implicit val conv: xsbti.FileConverter = fileConverter.value // used by PluginCompat

Expand Down
76 changes: 76 additions & 0 deletions plugin/src/sbt-test/sbt-avro/local-dependency/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
val checkUnpacked = TaskKey[Unit]("checkUnpacked")
val checkGenerated = TaskKey[Unit]("checkGenerated")

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

lazy val commonSettings = Seq(
organization := "com.github.sbt",
scalaVersion := "2.13.15"
)

lazy val avroOnlySettings = Seq(
crossScalaVersions := Seq.empty,
crossPaths := false,
autoScalaLibrary := false,
// only create avro jar
Compile / packageAvro / publishArtifact := true,
Compile / packageBin / publishArtifact := false,
Compile / packageSrc / publishArtifact := false,
Compile / packageDoc / publishArtifact := false,
)

lazy val `external`: Project = project
.in(file("external"))
.enablePlugins(SbtAvro)
.settings(commonSettings)
.settings(avroOnlySettings)
.settings(
name := "external",
version := "0.0.1-SNAPSHOT",
)

lazy val `transitive`: Project = project
.in(file("transitive"))
.enablePlugins(SbtAvro)
.settings(commonSettings)
.settings(avroOnlySettings)
.settings(
name := "transitive",
version := "0.0.1-SNAPSHOT",
libraryDependencies ++= Seq(
// when using avro scope, it won't be part of the pom dependencies -> intransitive
// to declare transitive dependency use the compile scope
"com.github.sbt" % "external" % "0.0.1-SNAPSHOT" classifier "avro"
),
Compile / avroDependencyIncludeFilter := artifactFilter(classifier = "avro"),
// create a test jar with a schema as resource
Test / packageBin / publishArtifact := true,
)

lazy val root: Project = project
.in(file("."))
.enablePlugins(SbtAvro)
.dependsOn(`transitive` % "avro->avro")
.settings(commonSettings)
.settings(
name := "local-dependency",
crossScalaVersions := Seq("2.13.15", "2.12.20"),
libraryDependencies ++= Seq(
"org.specs2" %% "specs2-core" % "4.20.9" % Test
),
// add additional avro source test jar
Test / avroDependencyIncludeFilter := artifactFilter(name = "transitive", classifier = "tests"),

Compile / checkUnpacked := {
exists((`transitive` / crossTarget).value / "src_managed" / "avro" / "main" / "external-avro" / "avdl.avdl")
exists((`transitive` / crossTarget).value / "src_managed" / "avro" / "main" / "external-avro" / "avpr.avpr")
exists((`transitive` / crossTarget).value / "src_managed" / "avro" / "main" / "external-avro" / "avsc.avsc")
},
Compile / checkGenerated := {
exists(crossTarget.value / "src_managed" / "compiled_avro" / "main" / "com" / "github" / "sbt" / "avro" / "test" / "external" / "Avdl.java")
exists(crossTarget.value / "src_managed" / "compiled_avro" / "main" / "com" / "github" / "sbt" / "avro" / "test" / "external" / "Avpr.java")
exists(crossTarget.value / "src_managed" / "compiled_avro" / "main" / "com" / "github" / "sbt" / "avro" / "test" / "external" / "Avsc.java")
exists(crossTarget.value / "src_managed" / "compiled_avro" / "main" / "com" / "github" / "sbt" / "avro" / "test" / "transitive" / "Avsc.java")
}
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@namespace("com.github.sbt.avro.test.external")
protocol ProtocolAvdl {
record Avdl {
string stringField;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"namespace": "com.github.sbt.avro.test.external",
"protocol": "ProtocolAvpr",
"types": [
{
"name": "Avpr",
"type": "record",
"fields": [
{
"name": "stringField",
"type": "string"
}
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "Avsc",
"namespace": "com.github.sbt.avro.test.external",
"type": "record",
"fields": [
{
"name": "stringField",
"type": "string"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
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)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.github.sbt.avro.test

object Main extends App {

external.Avsc.newBuilder().setStringField("external").build()
external.Avpr.newBuilder().setStringField("external").build()
external.Avdl.newBuilder().setStringField("external").build()
transitive.Avsc.newBuilder().setStringField("transitive").build()

println("success")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.github.sbt.avro.test

import com.github.sbt.avro.test.transitive.Test

object AvroTest extends App {

Test.newBuilder().setStringField("external").build()

println("success")
}
7 changes: 7 additions & 0 deletions plugin/src/sbt-test/sbt-avro/local-dependency/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
> external/publishLocal

> avroGenerate
> checkUnpacked
> checkGenerated

> +compile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "Avsc",
"namespace": "com.github.sbt.avro.test.transitive",
"type": "record",
"fields": [
{
"name": "stringField",
"type": "string"
},
{
"name": "referencedTypeField",
"type": "com.github.sbt.avro.test.external.Avsc"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "Test",
"namespace": "com.github.sbt.avro.test.transitive",
"type": "record",
"fields": [
{
"name": "stringField",
"type": "string"
},
{
"name": "referencedTypeField",
"type": "com.github.sbt.avro.test.external.Avsc"
}
]
}
5 changes: 1 addition & 4 deletions plugin/src/sbt-test/sbt-avro/publishing/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ lazy val `transitive`: Project = project
// to declare transitive dependency use the compile scope
"com.github.sbt" % "external" % "0.0.1-SNAPSHOT" classifier "avro"
),
transitiveClassifiers += "avro",
Compile / avroDependencyIncludeFilter := artifactFilter(classifier = "avro"),
// create a test jar with a schema as resource
Test / packageBin / publishArtifact := true,
Expand All @@ -62,9 +61,7 @@ lazy val root: Project = project
"org.specs2" %% "specs2-core" % "4.20.9" % Test
),
// add additional avro source test jar
// we unfortunatelly must recompile schemas from compile scope when test schema depends on it.
Test / avroDependencyIncludeFilter := (Compile / avroDependencyIncludeFilter).value ||
artifactFilter(name = "transitive", classifier = "tests"),
Test / avroDependencyIncludeFilter := artifactFilter(name = "transitive", classifier = "tests"),
// exclude specific avsc file
Compile / avroUnpackDependencies / excludeFilter := (Compile / avroUnpackDependencies / excludeFilter).value || "exclude.avsc",

Expand Down
Loading