From 7757381679db27e4227eefa0d2be64499bf7ffe8 Mon Sep 17 00:00:00 2001 From: David Francoeur Date: Wed, 16 Mar 2022 10:41:21 -0400 Subject: [PATCH] Add a list of excluded namespaces --- .../smithy4s/codegen/cli/CodegenCommand.scala | 13 +++++++- .../codegen-plugin/custom-settings/build.sbt | 31 +++++++++++++++---- .../smithy_input/exclude.smithy | 6 ++++ .../codegen-plugin/custom-settings/test | 11 +++++-- .../src/smithy4s/codegen/CodegenPlugin.scala | 24 ++++++++++++-- .../src/smithy4s/codegen/Codegen.scala | 11 +++++-- .../src/smithy4s/codegen/CodegenArgs.scala | 1 + 7 files changed, 81 insertions(+), 16 deletions(-) create mode 100644 modules/codegen-plugin/src/sbt-test/codegen-plugin/custom-settings/smithy_input/exclude.smithy diff --git a/modules/codegen-cli/src/smithy4s/codegen/cli/CodegenCommand.scala b/modules/codegen-cli/src/smithy4s/codegen/cli/CodegenCommand.scala index 10e03b006..82fe64e38 100644 --- a/modules/codegen-cli/src/smithy4s/codegen/cli/CodegenCommand.scala +++ b/modules/codegen-cli/src/smithy4s/codegen/cli/CodegenCommand.scala @@ -78,6 +78,15 @@ object CodegenCommand { .map(_.split(',').toSet) .orNone + val excludedNSOpt: Opts[Option[Set[String]]] = + Opts + .option[String]( + "excluded-ns", + "Comma-delimited list of namespaces that should not be processed. If unset, all namespaces are processed (except stdlib ones)" + ) + .map(_.split(',').toSet) + .orNone + val options = ( outputOpt, @@ -85,6 +94,7 @@ object CodegenCommand { skipScalaOpt, skipOpenapiOpt, allowedNSOpt, + excludedNSOpt, repositoriesOpt, dependenciesOpt, transformersOpt, @@ -92,7 +102,7 @@ object CodegenCommand { ) .mapN { // format: off - case (output, openApiOutput, skipScala, skipOpenapi, allowedNS, repositories, dependencies, transformers, specsArgs) => + case (output, openApiOutput, skipScala, skipOpenapi, allowedNS, excludedNS, repositories, dependencies, transformers, specsArgs) => // format: on CodegenArgs( specsArgs, @@ -101,6 +111,7 @@ object CodegenCommand { skipScala, skipOpenapi, allowedNS, + excludedNS, repositories.getOrElse(List.empty), dependencies.getOrElse(List.empty), transformers.getOrElse(List.empty) diff --git a/modules/codegen-plugin/src/sbt-test/codegen-plugin/custom-settings/build.sbt b/modules/codegen-plugin/src/sbt-test/codegen-plugin/custom-settings/build.sbt index 696a71ca0..e73fb622b 100644 --- a/modules/codegen-plugin/src/sbt-test/codegen-plugin/custom-settings/build.sbt +++ b/modules/codegen-plugin/src/sbt-test/codegen-plugin/custom-settings/build.sbt @@ -1,13 +1,32 @@ -lazy val root = (project in file(".")) +lazy val commonSettings = Def.settings( + scalaVersion := "2.13.6", + smithy4sInputDir in Compile := (baseDirectory in ThisBuild).value / "smithy_input", + Compile / smithy4sAllowedNamespaces := List( + "aws.iam", + "smithy4s.example" + ), + libraryDependencies += "com.disneystreaming.smithy4s" %% "smithy4s-core" % smithy4sVersion.value, + libraryDependencies += "software.amazon.smithy" % "smithy-aws-iam-traits" % "1.14.1" % Smithy4s +) + +lazy val p1 = project .enablePlugins(Smithy4sCodegenPlugin) + .settings(commonSettings) .settings( - scalaVersion := "2.13.6", - smithy4sInputDir in Compile := (baseDirectory in ThisBuild).value / "smithy_input", - smithy4sOutputDir in Compile := (baseDirectory in ThisBuild).value / "smithy_output", + smithy4sOutputDir in Compile := baseDirectory.value / "smithy_output", + Compile / smithy4sAllowedNamespaces := List( + "aws.iam", + "smithy4s.example" + ) + ) +lazy val p2 = project + .enablePlugins(Smithy4sCodegenPlugin) + .settings(commonSettings) + .settings( + smithy4sOutputDir in Compile := baseDirectory.value / "smithy_output", Compile / smithy4sAllowedNamespaces := List( "aws.iam", "smithy4s.example" ), - libraryDependencies += "com.disneystreaming.smithy4s" %% "smithy4s-core" % smithy4sVersion.value, - libraryDependencies += "software.amazon.smithy" % "smithy-aws-iam-traits" % "1.14.1" % "smithy4s" + Compile / smithy4sExcludedNamespaces := List("smithy4s.toexclude") ) diff --git a/modules/codegen-plugin/src/sbt-test/codegen-plugin/custom-settings/smithy_input/exclude.smithy b/modules/codegen-plugin/src/sbt-test/codegen-plugin/custom-settings/smithy_input/exclude.smithy new file mode 100644 index 000000000..ccb92dd8a --- /dev/null +++ b/modules/codegen-plugin/src/sbt-test/codegen-plugin/custom-settings/smithy_input/exclude.smithy @@ -0,0 +1,6 @@ +namespace smithy4s.toexclude + +structure StructureToExclude { + @required + key: String +} \ No newline at end of file diff --git a/modules/codegen-plugin/src/sbt-test/codegen-plugin/custom-settings/test b/modules/codegen-plugin/src/sbt-test/codegen-plugin/custom-settings/test index 560141a2b..0d173879f 100644 --- a/modules/codegen-plugin/src/sbt-test/codegen-plugin/custom-settings/test +++ b/modules/codegen-plugin/src/sbt-test/codegen-plugin/custom-settings/test @@ -1,4 +1,9 @@ # check if smithy4sCodegen works -> compile -$ exists smithy_output/aws/iam/ActionPermissionDescription.scala -$ exists smithy_output/smithy4s/example/ObjectService.scala +> p1/compile +$ exists p1/smithy_output/aws/iam/ActionPermissionDescription.scala +$ exists p1/smithy_output/smithy4s/example/ObjectService.scala + +> p2/compile +$ exists p2/smithy_output/aws/iam/ActionPermissionDescription.scala +$ exists p2/smithy_output/smithy4s/example/ObjectService.scala +-$ exists p2/smithy_output/smithy4s/toexclude/StructureToExclude.scala diff --git a/modules/codegen-plugin/src/smithy4s/codegen/CodegenPlugin.scala b/modules/codegen-plugin/src/smithy4s/codegen/CodegenPlugin.scala index b96347582..f8a200219 100644 --- a/modules/codegen-plugin/src/smithy4s/codegen/CodegenPlugin.scala +++ b/modules/codegen-plugin/src/smithy4s/codegen/CodegenPlugin.scala @@ -28,32 +28,46 @@ object Smithy4sCodegenPlugin extends AutoPlugin { taskKey[Seq[File]]( "Generate .scala and other files from smithy specs (.smithy or .json files)" ) + val smithy4sVersion = settingKey[String]("Smithy4sVersion") + val smithy4sInputDir = settingKey[File]( "Input directory for smithy specs (.smithy or .json files)" ) + val smithy4sOutputDir = settingKey[File]( "Output directory for .scala files generated by smithy4s" ) + val smithy4sOpenapiDir = settingKey[File]( "Output directory for openapi .json files generated by smithy4s" ) + val smithy4sAllowedNamespaces = settingKey[List[String]]( "Allow-list of namespaces that should be processed by the generator. If unset, considers all namespaces but stdlib ones" ) - @deprecated("2022-03-01", """use `libraryDependencies += "org.acme" % "artifact" % "version" % Smithy4s`""") + val smithy4sExcludedNamespaces = + settingKey[List[String]]( + "Disallow-list of namespaces that should not be processed by the generator. When set, namespaces are evicted as the last filtering step" + ) + + @deprecated( + "2022-03-01", + """use `libraryDependencies += "org.acme" % "artifact" % "version" % Smithy4s`""" + ) val smithy4sCodegenDependencies = settingKey[List[String]]( "List of dependencies containing smithy files to include in codegen task" ) - val Smithy4s = config("smithy4s").describedAs("Dependencies for Smithy code.") + val Smithy4s = + config("smithy4s").describedAs("Dependencies for Smithy code.") val smithy4sModelTransformers = settingKey[List[String]]( @@ -98,7 +112,8 @@ object Smithy4sCodegenPlugin extends AutoPlugin { deps .filter { _.configurations.contains(Smithy4s.name) } .map { m => - if (CrossVersion.disabled == m.crossVersion) s"${m.organization}:${m.name}:${m.revision}" + if (CrossVersion.disabled == m.crossVersion) + s"${m.organization}:${m.name}:${m.revision}" else s"${m.organization}::${m.name}:${m.revision}" } .toList @@ -110,6 +125,8 @@ object Smithy4sCodegenPlugin extends AutoPlugin { val openApiOutputPath = (conf / smithy4sOpenapiDir).value.getAbsolutePath() val allowedNamespaces = (conf / smithy4sAllowedNamespaces).?.value.map(_.toSet) + val excludedNamespaces = + (conf / smithy4sExcludedNamespaces).?.value.map(_.toSet) val dependencies = prepareSmithy4sDeps(libraryDependencies.value) val res = (conf / resolvers).value.toList.collect { case m: MavenRepository => @@ -136,6 +153,7 @@ object Smithy4sCodegenPlugin extends AutoPlugin { skipScala = false, skipOpenapi = false, allowedNS = allowedNamespaces, + excludedNS = excludedNamespaces, repositories = res, dependencies = dependencies, transforms diff --git a/modules/codegen/src/smithy4s/codegen/Codegen.scala b/modules/codegen/src/smithy4s/codegen/Codegen.scala index 3ccdd319b..89488d2f4 100644 --- a/modules/codegen/src/smithy4s/codegen/Codegen.scala +++ b/modules/codegen/src/smithy4s/codegen/Codegen.scala @@ -35,7 +35,7 @@ object Codegen { self => ) val scalaFiles = if (!args.skipScala) { - Codegen.generate(model, args.allowedNS).map { + Codegen.generate(model, args.allowedNS, args.excludedNS).map { case (relPath, name, outputString) => val fileName = name + ".scala" val scalaFile = (args.output / relPath / fileName) @@ -59,7 +59,8 @@ object Codegen { self => private def generate( model: Model, - allowedNS: Option[Set[String]] + allowedNS: Option[Set[String]], + excludedNS: Option[Set[String]] ): List[(os.RelPath, String, String)] = { val namespaces = model .shapes() @@ -72,15 +73,19 @@ object Codegen { self => Set( "smithy4s.api" ) + val excluded = excludedNS.getOrElse(Set.empty) val filteredNamespaces = allowedNS match { case Some(allowedNamespaces) => - namespaces.filter(allowedNamespaces) + namespaces + .filter(allowedNamespaces) + .filterNot(excluded) case None => namespaces .filterNot(_.startsWith("aws.")) .filterNot(_.startsWith("smithy.")) .filterNot(reserved) + .filterNot(excluded) } filteredNamespaces.toList diff --git a/modules/codegen/src/smithy4s/codegen/CodegenArgs.scala b/modules/codegen/src/smithy4s/codegen/CodegenArgs.scala index 1ed792814..7cb2a2089 100644 --- a/modules/codegen/src/smithy4s/codegen/CodegenArgs.scala +++ b/modules/codegen/src/smithy4s/codegen/CodegenArgs.scala @@ -23,6 +23,7 @@ case class CodegenArgs( skipScala: Boolean, skipOpenapi: Boolean, allowedNS: Option[Set[String]], + excludedNS: Option[Set[String]], repositories: List[String], dependencies: List[String], transformers: List[String]