Skip to content

Commit

Permalink
Consistent settings
Browse files Browse the repository at this point in the history
  • Loading branch information
RustedBones committed Oct 15, 2024
1 parent ba3dc45 commit be074f4
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 39 deletions.
55 changes: 33 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ For instance, add the following lines to `project/plugins.sbt`:
addSbtPlugin("com.github.sbt" % "sbt-avro" % "3.5.0")
```

Enable the plugin in your `build.sbt` and select the avro version to use:
Enable the plugin in your `build.sbt` and select the desired avro version to use:

```
enablePlugins(SbtAvro)
Expand All @@ -34,31 +34,42 @@ to generate the avro classes.

## Settings

| Name | Default | Description |
|:-------------------------------------------|:----------------------------------------------|:----------------------------------------------------------------------------------------|
| `avroSources` | `sourceDirectory` / `avro` | Source directories with `*.avsc`, `*.avdl` and `*.avpr` files. |
| `avroAdditionalDependencies` | `avro-compiler` % `avro`, `avro` % `compile` | Additional dependencies to be added to library dependencies. |
| `avroSpecificRecords` | `Seq.empty` | List of avro generated classes to recompile with current avro version and settings. |
| `avroUnpackDependencies` / `includeFilter` | All avro specifications | Avro specification files from dependencies to unpack |
| `avroUnpackDependencies` / `excludeFilter` | Hidden files | Avro specification files from dependencies to exclude from unpacking |
| `avroUnpackDependencies` / `target` | `sourceManaged` / `avro` / `$config` | Target directory for schemas packaged in the dependencies |
| `avroGenerate` / `target` | `sourceManaged` / `compiled_avro` / `$config` | Source directory for generated `.java` files. |
| `avroDependencyIncludeFilter` | `source` typed `avro` classifier artifacts | Dependencies containing avro schema to be unpacked for generation |
| `packageAvro` / `artifactClassifier` | `Some("avro")` | Classifier for avro artifact |
| `packageAvro` / `publishArtifact` | `false` | Enable / Disable avro artifact publishing |
| `avroCompiler` | `com.github.sbt.avro.AvroCompilerBridge` | Sbt avro compiler class. |
| `avroStringType` | `CharSequence` | Type for representing strings. Possible values: `CharSequence`, `String`, `Utf8`. |
| `avroUseNamespace` | `false` | Validate that directory layout reflects namespaces, i.e. `com/myorg/MyRecord.avsc`. |
| `avroFieldVisibility` | `public` | Field Visibility for the properties. Possible values: `private`, `public`. |
| `avroEnableDecimalLogicalType` | `true` | Use `java.math.BigDecimal` instead of `java.nio.ByteBuffer` for logical type `decimal`. |
| `avroOptionalGetters` | `false` (requires avro `1.10+`) | Generate getters that return `Optional` for nullable fields. |

## Tasks
### Project settings

| Name | Default | Description |
|:-------------------------------|:-------------------------------------------------------------------------|:----------------------------------------------------------------------------------------|
| `avroAdditionalDependencies` | `avro-compiler % avroVersion % "avro"`, `avro % avroVersion % "compile"` | Additional dependencies to be added to library dependencies. |
| `avroCompiler` | `com.github.sbt.avro.AvroCompilerBridge` | Sbt avro compiler class. |
| `avroCreateSetters` | `true` | Generate setters. |
| `avroDependencyIncludeFilter` | `source` typed `avro` classifier artifacts | Filter for including modules containing avro dependencies. |
| `avroEnableDecimalLogicalType` | `true` | Use `java.math.BigDecimal` instead of `java.nio.ByteBuffer` for logical type `decimal`. |
| `avroFieldVisibility` | `public` | Field visibility for the properties. Possible values: `private`, `public`. |
| `avroOptionalGetters` | `false` (requires avro `1.10+`) | Generate getters that return `Optional` for nullable fields. |
| `avroStringType` | `CharSequence` | Type for representing strings. Possible values: `CharSequence`, `String`, `Utf8`. |
| `avroUseNamespace` | `false` | Validate that directory layout reflects namespaces, i.e. `com/myorg/MyRecord.avsc`. |
| `avroVersion` | `1.12.0` | Avro version to use in the project. |

### Scoped settings (Compile/Test)

| Name | Default | Description |
|:-------------------------------------------|:----------------------------------------------|:-----------------------------------------------------------------------------------------------------|
| `avroGenerate` / `target` | `sourceManaged` / `compiled_avro` / `$config` | Source directory for generated `.java` files. |
| `avroSource` | `sourceDirectory` / `$config` / `avro` | Default Avro source directory for `*.avsc`, `*.avdl` and `*.avpr` files. |
| `avroSpecificRecords` | `Seq.empty` | List of fully qualified Avro record class names to recompile with current avro version and settings. |
| `avroUmanagedSourceDirectories` | `Seq(avroSource)` | Unmanaged Avro source directories, which contain manually created sources. |
| `avroUnpackDependencies` / `excludeFilter` | `HiddenFileFilter` | Filter for excluding avro specification files from unpacking. |
| `avroUnpackDependencies` / `includeFilter` | `AllPassFilter` | Filter for including avro specification files to unpack. |
| `avroUnpackDependencies` / `target` | `sourceManaged` / `avro` / `$config` | Target directory for schemas packaged in the dependencies |
| `packageAvro` / `artifactClassifier` | `Some("avro")` | Classifier for avro artifact |
| `packageAvro` / `publishArtifact` | `false` | Enable / Disable avro artifact publishing |


## Scoped Tasks (Compile/Test)

| Name | Description |
|:-------------------------|:--------------------------------------------------------------------------------------------------|
| `avroUnpackDependencies` | Unpack avro schemas from dependencies. This task is automatically executed before `avroGenerate`. |
| `avroGenerate` | Generate Java sources for Avro schemas. This task is automatically executed before `compile`. |
| `avroUnpackDependencies` | Unpack avro schemas from dependencies. This task is automatically executed before `avroGenerate`. |
| `packageAvro` | Produces an avro artifact, such as a jar containing avro schemas. |

## Examples
Expand Down
30 changes: 16 additions & 14 deletions plugin/src/main/scala/com/github/sbt/avro/SbtAvro.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,21 @@ object SbtAvro extends AutoPlugin {

// format: off
val avroAdditionalDependencies = settingKey[Seq[ModuleID]]("Additional dependencies to be added to library dependencies.")
val avroCompiler = settingKey[String]("Sbt avro compiler class. Default: com.github.sbt.avro.AvroCompilerBridge")
val avroCreateSetters = settingKey[Boolean]("Generate setters. Default: true")
val avroCompiler = settingKey[String]("Sbt avro compiler class.")
val avroCreateSetters = settingKey[Boolean]("Generate setters.")
val avroDependencyIncludeFilter = settingKey[DependencyFilter]("Filter for including modules containing avro dependencies.")
val avroEnableDecimalLogicalType = settingKey[Boolean]("Use java.math.BigDecimal instead of java.nio.ByteBuffer for logical type \"decimal\". Default: true.")
val avroFieldVisibility = settingKey[String]("Field visibility for the properties. Possible values: private, public. Default: public.")
val avroOptionalGetters = settingKey[Boolean]("Generate getters that return Optional for nullable fields. Default: false.")
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 avroSources = settingKey[Seq[File]]("Avro source directories.")
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]("Avro version to use in the project. default: 1.12.0")
val avroEnableDecimalLogicalType = settingKey[Boolean]("Use java.math.BigDecimal instead of java.nio.ByteBuffer for logical type decimal.")
val avroFieldVisibility = settingKey[String]("Field visibility for the properties. Possible values: private, public.")
val avroOptionalGetters = settingKey[Boolean]("Generate getters that return Optional for nullable fields.")
val avroSpecificRecords = settingKey[Seq[String]]("List of fully qualified Avro record class names to recompile with current avro version and settings. Classes must be part of the Avro library dependencies scope.")
val avroSource = settingKey[File]("Default Avro source directory for *.avsc, *.avdl and *.avpr files.")
val avroStringType = settingKey[String]("Type for representing strings. Possible values: CharSequence, String, Utf8.")
val avroUnmanagedSourceDirectories = settingKey[Seq[File]]("Unmanaged Avro source directories, which contain manually created sources.")
val avroUseNamespace = settingKey[Boolean]("Validate that directory layout reflects namespaces, i.e. com/myorg/MyRecord.avsc.")
val avroVersion = settingKey[String]("Avro version to use in the project.")

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.")
// format: on

Expand Down Expand Up @@ -79,7 +80,8 @@ object SbtAvro extends AutoPlugin {

// settings to be applied for both Compile and Test
lazy val configScopedSettings: Seq[Setting[_]] = Seq(
avroSources := Seq(sourceDirectory.value / "avro"),
avroSource := sourceDirectory.value / "avro",
avroUnmanagedSourceDirectories := Seq(avroSource.value),
avroSpecificRecords := Seq.empty,
// dependencies
avroUnpackDependencies / includeFilter := AllPassFilter,
Expand All @@ -103,7 +105,7 @@ object SbtAvro extends AutoPlugin {
import autoImport._

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

override def trigger: PluginTrigger = noTrigger
Expand Down Expand Up @@ -168,7 +170,7 @@ 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 = Seq(externalSrcDir) ++ avroSources.value
val srcDirs = avroUnmanagedSourceDirectories.value :+ externalSrcDir
val outDir = (key / target).value

val cachedCompile = {
Expand Down
4 changes: 2 additions & 2 deletions plugin/src/sbt-test/sbt-avro/basic/project/Avro.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ case class Avro(version: String) extends Platform {
.enablePlugins(SbtAvro)
.settings(
avroVersion := version,
Compile / avroSources ++= crossProjectCrossType.value
Compile / avroUnmanagedSourceDirectories ++= crossProjectCrossType.value
.sharedSrcDir(baseDirectory.value, "main")
.map(_.getParentFile / "avro"),
Test / avroSources ++= crossProjectCrossType.value
Test / avroUnmanagedSourceDirectories ++= crossProjectCrossType.value
.sharedSrcDir(baseDirectory.value, "test")
.map(_.getParentFile / "avro"),
)
Expand Down
2 changes: 1 addition & 1 deletion plugin/src/sbt-test/sbt-avro/settings/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ avroFieldVisibility := "public"
avroOptionalGetters := true
avroEnableDecimalLogicalType := false
Compile / avroSpecificRecords += "org.apache.avro.specific.TestRecordWithLogicalTypes"
Compile / avroSources += (Compile / sourceDirectory).value / "avro_source"
Compile / avroSource := (Compile / sourceDirectory).value / "avro_source"
Compile / avroGenerate / target := (Compile / sourceManaged).value

0 comments on commit be074f4

Please sign in to comment.