Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ lazy val sbtPlugin = project
commonSettings,
scalaVersion := "2.12.20",
libraryDependencies ++= Seq(
"software.amazon.smithy" % "smithy-trait-codegen" % "1.60.2",
"software.amazon.smithy" % "smithy-model" % "1.60.2",
"software.amazon.smithy" % "smithy-trait-codegen" % "1.61.0",
"software.amazon.smithy" % "smithy-model" % "1.61.0",
) ++ Seq(
"com.lihaoyi" %% "os-lib" % "0.10.7"
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,23 +88,6 @@ object SmithyTraitCodegen {

}

// Hack / workaround for https://github.com/smithy-lang/smithy/pull/2671
private def namespaceHackRequired(ns: String) =
ns.startsWith("smithy") && ns != "smithy" && !ns.startsWith("smithy.")

private def renameNamespaceForHack(ns: String) =
if (namespaceHackRequired(ns))
"hack" + ns
else
ns

private def replaceNamespaceRefsInFile(fileText: String, ns: String) =
if (namespaceHackRequired(ns)) {
fileText.replaceAll(s"hack$ns", ns)
} else {
fileText
}

def generate(args: Args): Output = {
val outputDir = args.targetDir / "smithy-trait-generator-output"
val genDir = outputDir / "java"
Expand All @@ -114,37 +97,13 @@ object SmithyTraitCodegen {

val manifest = FileManifest.create(genDir.toNIO)

val model =
args
.dependencies
.foldLeft(Model.assembler().addImport(args.smithySourcesDir.path.toNIO)) {
case (acc, dep) => acc.addImport(dep.path.toNIO)
}
.assemble()
.unwrap() match {
case model =>
if (namespaceHackRequired(args.smithyNamespace)) {
println("Applying namespace workaround - `hack` prefix will be used")

val renames =
model
.shapes()
.collect(Collectors.toList())
.asScala
.filter(_.getId().getNamespace() == args.smithyNamespace)
.map { shp =>
shp.getId() ->
shp.getId().withNamespace(renameNamespaceForHack(shp.getId().getNamespace()))
}
.toMap
.asJava

ModelTransformer
.create()
.renameShapes(model, renames)
} else
model
val model = args
.dependencies
.foldLeft(Model.assembler().addImport(args.smithySourcesDir.path.toNIO)) { case (acc, dep) =>
acc.addImport(dep.path.toNIO)
}
.assemble()
.unwrap()

val context = PluginContext
.builder()
Expand All @@ -155,7 +114,7 @@ object SmithyTraitCodegen {
ObjectNode
.builder()
.withMember("package", args.javaPackage)
.withMember("namespace", renameNamespaceForHack(args.smithyNamespace))
.withMember("namespace", args.smithyNamespace)
.withMember("header", ArrayNode.builder.build())
.withMember("excludeTags", ArrayNode.builder.withValue("nocodegen").build())
.build()
Expand All @@ -168,13 +127,6 @@ object SmithyTraitCodegen {
if (os.exists(genDir / "META-INF"))
os.move(genDir / "META-INF", metaDir / "META-INF")

os.walk(genDir)
.filter(os.isFile)
.filter(_.ext == "java")
.foreach { f =>
os.write.over(f, replaceNamespaceRefsInFile(os.read(f), args.smithyNamespace))
}

os
.walk(metaDir, includeTarget = true)
.filter(os.isFile)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ val traits = project
smithyTraitCodegenDependencies := List(
"io.github.disneystreaming.alloy" % "alloy-core" % "0.3.23"
),
// Prior to smithy 1.61.0, namespaces starting with "smithy" were forbidden and required a workaround
smithyTraitCodegenJavaPackage := "smithy4bazinga",
smithyTraitCodegenNamespace := "smithy4bazinga",
)
Expand Down