|
| 1 | +# smithy-trait-codegen-scala |
| 2 | + |
| 3 | +Generate Java classes (with builders, serialization to/from `Node`, trait providers) for Smithy traits, for usage in Scala projects. |
| 4 | + |
| 5 | +## Usage |
| 6 | + |
| 7 | +### sbt |
| 8 | + |
| 9 | +In `project/plugins.sbt`: |
| 10 | + |
| 11 | +```scala |
| 12 | +addSbtPlugin("org.polyvariant" % "smithy-trait-codegen-sbt" % version) |
| 13 | +``` |
| 14 | + |
| 15 | +In `build.sbt`: |
| 16 | + |
| 17 | +```scala |
| 18 | +// on a Java-only module, e.g. autoScalaLibrary := false, crossPaths := false |
| 19 | +.enablePlugins(SmithyTraitCodegenPlugin) |
| 20 | +.settings( |
| 21 | + smithyTraitCodegenDependencies := Nil, |
| 22 | + smithyTraitCodegenNamespace := "my.ns", |
| 23 | + smithyTraitCodegenJavaPackage := "my.pkg", |
| 24 | +) |
| 25 | +``` |
| 26 | + |
| 27 | +### mill |
| 28 | + |
| 29 | +Currently not supported in this plugin. See https://github.com/simple-scala-tooling/smithy-trait-codegen-mill/ for an alternative. |
| 30 | + |
| 31 | +## Useful patterns |
| 32 | + |
| 33 | +### Mixing generated and handwritten providers |
| 34 | + |
| 35 | +In case you want to keep some handwritten trait providers, e.g. if you have union traits (which are currently not supported in smithy-trait-codegen), or want to keep binary compatibility on existing traits. |
| 36 | + |
| 37 | +Instead of `META-INF/services/software.amazon.smithy.model.traits.TraitService`, add the providers to |
| 38 | + |
| 39 | +`META-INF/services-input/software.amazon.smithy.model.traits.TraitService` |
| 40 | + |
| 41 | +and add this snippet to include those in the actual, generated file under `META-INF/services`: |
| 42 | + |
| 43 | +```scala |
| 44 | +smithyTraitCodegenExternalProviders ++= |
| 45 | + IO.readLines( |
| 46 | + (ThisBuild / baseDirectory).value / "modules" / "protocol" / "resources" / "META-INF" / "services-input" / classOf[TraitService].getName() |
| 47 | + ).filterNot(_.trim.startsWith("#")) |
| 48 | + .filterNot(_.trim.isEmpty), |
| 49 | +Compile / packageBin / mappings ~= { |
| 50 | + _.filterNot { case (_, path) => path.contains("services-input") } |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +You can also hardcode `smithyTraitCodegenExternalProviders` right there: |
| 55 | + |
| 56 | +```scala |
| 57 | +smithyTraitCodegenExternalProviders ++= List("my.pkg.Trait1$Provider", "my.pkg.Trait2$Provider") |
| 58 | +``` |
0 commit comments