Skip to content

Commit

Permalink
Merge pull request #142 from daddykotex/dfrancoeur/exclude
Browse files Browse the repository at this point in the history
Add a list of excluded namespaces
  • Loading branch information
Baccata authored Mar 17, 2022
2 parents fac06f5 + 7757381 commit 4053a06
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,31 @@ 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,
openApiOutputOpt,
skipScalaOpt,
skipOpenapiOpt,
allowedNSOpt,
excludedNSOpt,
repositoriesOpt,
dependenciesOpt,
transformersOpt,
specsArgs
)
.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,
Expand All @@ -101,6 +111,7 @@ object CodegenCommand {
skipScala,
skipOpenapi,
allowedNS,
excludedNS,
repositories.getOrElse(List.empty),
dependencies.getOrElse(List.empty),
transformers.getOrElse(List.empty)
Expand Down
Original file line number Diff line number Diff line change
@@ -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")
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace smithy4s.toexclude

structure StructureToExclude {
@required
key: String
}
Original file line number Diff line number Diff line change
@@ -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
24 changes: 21 additions & 3 deletions modules/codegen-plugin/src/smithy4s/codegen/CodegenPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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]](
Expand Down Expand Up @@ -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
Expand All @@ -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 =>
Expand All @@ -136,6 +153,7 @@ object Smithy4sCodegenPlugin extends AutoPlugin {
skipScala = false,
skipOpenapi = false,
allowedNS = allowedNamespaces,
excludedNS = excludedNamespaces,
repositories = res,
dependencies = dependencies,
transforms
Expand Down
11 changes: 8 additions & 3 deletions modules/codegen/src/smithy4s/codegen/Codegen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions modules/codegen/src/smithy4s/codegen/CodegenArgs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 4053a06

Please sign in to comment.