Skip to content

Commit

Permalink
add support for transforms in codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
lewisjkl authored and Baccata committed Jan 14, 2022
1 parent 1fbbe67 commit 05ab381
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 17 deletions.
2 changes: 2 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ lazy val codegen = projectMatrix
libraryDependencies ++= Seq(
Dependencies.Cats.core.value,
Dependencies.Smithy.model,
Dependencies.Smithy.build,
Dependencies.Smithy.awsTraits,
Dependencies.Smithy.waiters,
"com.lihaoyi" %% "os-lib" % "0.8.0",
Expand Down Expand Up @@ -591,6 +592,7 @@ lazy val Dependencies = new {
val Smithy = new {
val smithyVersion = "1.16.2"
val model = "software.amazon.smithy" % "smithy-model" % smithyVersion
val build = "software.amazon.smithy" % "smithy-build" % smithyVersion
val awsTraits =
"software.amazon.smithy" % "smithy-aws-traits" % smithyVersion
val openapi = "software.amazon.smithy" % "smithy-openapi" % smithyVersion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,12 @@ object CodegenCommand {
allowedNSOpt,
repositoriesOpt,
dependenciesOpt,
transformersOpt,
specsArgs
)
.mapN {
// format: off
case (output, openApiOutput, skipScala, skipOpenapi, allowedNS, repositories, dependencies, specsArgs) =>
case (output, openApiOutput, skipScala, skipOpenapi, allowedNS, repositories, dependencies, transformers, specsArgs) =>
// format: on
CodegenArgs(
specsArgs,
Expand All @@ -101,7 +102,8 @@ object CodegenCommand {
skipOpenapi,
allowedNS,
repositories.getOrElse(List.empty),
dependencies.getOrElse(List.empty)
dependencies.getOrElse(List.empty),
transformers.getOrElse(List.empty)
)
}

Expand Down
3 changes: 2 additions & 1 deletion modules/codegen-cli/src/smithy4s/codegen/cli/DumpModel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ object DumpModel {
val (_, model) = ModelLoader.load(
args.specs.map(_.toIO).toSet,
args.dependencies,
args.repositories
args.repositories,
args.transformers
)

Node.prettyPrintJson(ModelSerializer.builder().build.serialize(model))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ object DumpModelCommand {
val options = (
specsArgs,
repositoriesOpt.map(_.getOrElse(Nil)),
dependenciesOpt.map(_.getOrElse(Nil))
dependenciesOpt.map(_.getOrElse(Nil)),
transformersOpt.map(_.getOrElse(Nil))
).mapN(DumpModelArgs.apply)

val command: Command[DumpModel] =
Expand Down
9 changes: 9 additions & 0 deletions modules/codegen-cli/src/smithy4s/codegen/cli/Options.scala
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,13 @@ object Options {
.map(_.split(',').toList)
.orNone

val transformersOpt: Opts[Option[List[String]]] =
Opts
.option[String](
"transformers",
"Comma-delimited list of transformer names to apply to smithy files"
)
.map(_.split(',').toList)
.orNone

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ object Smithy4sCommand {
final case class DumpModelArgs(
specs: List[os.Path],
repositories: List[String],
dependencies: List[String]
dependencies: List[String],
transformers: List[String]
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ object CommandParsingSpec extends FunSuite {
skipOpenapi = false,
allowedNS = None,
repositories = Nil,
dependencies = Nil
dependencies = Nil,
transformers = Nil
)
)
)
Expand All @@ -56,7 +57,9 @@ object CommandParsingSpec extends FunSuite {
"--repositories",
"repo1,repo2",
"--dependencies",
"dep1,dep2"
"dep1,dep2",
"--transformers",
"t1,t2"
)
)

Expand All @@ -75,7 +78,8 @@ object CommandParsingSpec extends FunSuite {
skipOpenapi = true,
allowedNS = Some(Set("name1", "name2")),
repositories = List("repo1", "repo2"),
dependencies = List("dep1", "dep2")
dependencies = List("dep1", "dep2"),
transformers = List("t1", "t2")
)
)
)
Expand All @@ -90,7 +94,8 @@ object CommandParsingSpec extends FunSuite {
Smithy4sCommand.DumpModelArgs(
specs = Nil,
repositories = Nil,
dependencies = Nil
dependencies = Nil,
transformers = Nil
)
)
)
Expand All @@ -105,7 +110,9 @@ object CommandParsingSpec extends FunSuite {
"--repositories",
"repo1,repo2",
"--dependencies",
"dep1,dep2"
"dep1,dep2",
"--transformers",
"t1,t2"
)
)
assert(
Expand All @@ -118,7 +125,8 @@ object CommandParsingSpec extends FunSuite {
os.pwd / "sampleSpecs" / "example.smithy"
),
repositories = List("repo1", "repo2"),
dependencies = List("dep1", "dep2")
dependencies = List("dep1", "dep2"),
transformers = List("t1", "t2")
)
)
)
Expand Down
12 changes: 10 additions & 2 deletions modules/codegen-plugin/src/smithy4s/codegen/CodegenPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ object Smithy4sCodegenPlugin extends AutoPlugin {
settingKey[List[String]](
"List of dependencies containing smithy files to include in codegen task"
)

val smithy4sModelTransformers =
settingKey[List[String]](
"List of transformer names that should be applied to the model prior to codegen"
)
}

import autoImport._
Expand All @@ -72,7 +77,8 @@ object Smithy4sCodegenPlugin extends AutoPlugin {
Compile / resourceGenerators += (Compile / smithy4sCodegen).map(
_.filter(_.ext != "scala")
),
cleanFiles += (Compile / smithy4sOutputDir).value
cleanFiles += (Compile / smithy4sOutputDir).value,
Compile / smithy4sModelTransformers := List.empty
)

private def untupled[A, B, C](f: ((A, B)) => C): (A, B) => C = (a, b) =>
Expand All @@ -96,6 +102,7 @@ object Smithy4sCodegenPlugin extends AutoPlugin {
(conf / resolvers).value.toList.collect { case m: MavenRepository =>
m.root
}
val transforms = (conf / smithy4sModelTransformers).value
val s = streams.value

val cached =
Expand All @@ -117,7 +124,8 @@ object Smithy4sCodegenPlugin extends AutoPlugin {
skipOpenapi = false,
allowedNS = allowedNamespaces,
repositories = res,
dependencies = dependencies
dependencies = dependencies,
transforms
)
val resPaths = smithy4s.codegen.Codegen
.processSpecs(codegenArgs)
Expand Down
3 changes: 2 additions & 1 deletion modules/codegen/src/smithy4s/codegen/Codegen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ object Codegen { self =>
val (classloader, model) = ModelLoader.load(
args.specs.map(_.toIO).toSet,
args.dependencies,
args.repositories
args.repositories,
args.transformers
)

val scalaFiles = if (!args.skipScala) {
Expand Down
3 changes: 2 additions & 1 deletion modules/codegen/src/smithy4s/codegen/CodegenArgs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ case class CodegenArgs(
skipOpenapi: Boolean,
allowedNS: Option[Set[String]],
repositories: List[String],
dependencies: List[String]
dependencies: List[String],
transformers: List[String]
)
19 changes: 17 additions & 2 deletions modules/codegen/src/smithy4s/codegen/ModelLoader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@ import software.amazon.smithy.model.Model
import java.io.File
import java.net.URL
import java.net.URLClassLoader
import software.amazon.smithy.build.ProjectionTransformer
import software.amazon.smithy.build.TransformContext

object ModelLoader {
def load(
specs: Set[File],
dependencies: List[String],
repositories: List[String]
repositories: List[String],
transformers: List[String]
): (ClassLoader, Model) = {
val maybeDeps = resolveDependencies(dependencies, repositories)
val currentClassLoader = this.getClass().getClassLoader()
Expand Down Expand Up @@ -76,7 +79,19 @@ object ModelLoader {
.assemble()
.unwrap()

(validatorClassLoader, model)
val serviceFactory =
ProjectionTransformer.createServiceFactory(validatorClassLoader)

val trans = transformers.flatMap { t =>
val result = serviceFactory(t)
if (result.isPresent()) Some(result.get) else None
}

val transformedModel = trans.foldLeft(model)((m, t) =>
t.transform(TransformContext.builder().model(m).build())
)

(validatorClassLoader, transformedModel)
}

private def resolveDependencies(
Expand Down

0 comments on commit 05ab381

Please sign in to comment.