Skip to content

Conversation

@jbelkins
Copy link
Contributor

@jbelkins jbelkins commented Nov 15, 2025

Description of changes

This PR provides a build plugin that will be used to code generate using a Swift-native code generator.
(An empty Swift file is produced as the code generator output. Actually generating code will be a follow-on project.)

Changes:

  • Write a smithy-model-info.json file to the project which is an input to the code generator.
  • Define a build plugin (SmithyCodeGenerator) that will insert code generation into the target's Swift build process.
  • Define a CLI tool (SmithyCodegenCLI) which will be invoked to perform code generation. (Actual codegen is unimplemented, this PR just establishes the structure.)

Scope

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

dependencies: [
.product(name: "ArgumentParser", package: "swift-argument-parser"),
]
),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SmithyCodeGenerator is the Swift build plugin that we add to service clients to perform their codegen.

SmithyCodegenCLI is a command-line tool that the build plugin invokes to actually generate the code. (Eventually this code generation tool will be usable from the CLI but for now, it's solely for the use of the build plugin.)

.library(name: "SmithyCBOR", targets: ["SmithyCBOR"]),
.library(name: "SmithyWaitersAPI", targets: ["SmithyWaitersAPI"]),
.library(name: "SmithyTestUtil", targets: ["SmithyTestUtil"]),
.plugin(name: "SmithyCodeGenerator", targets: ["SmithyCodeGenerator"]),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The plugin is "published" from smithy-swift so that it can be installed into service clients that smithy-swift creates.

See awslabs/aws-sdk-swift#2058 to see how a Smithy-based SDK installs the plugin into a service client target.

import software.amazon.smithy.aws.traits.ServiceTrait
import software.amazon.smithy.swift.codegen.integration.ProtocolGenerator
import software.amazon.smithy.swift.codegen.model.getTrait

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file generates a file named smithy-model-info.json into a service client target.

Its contents are

{"path":"relative/path/to/model.json"}

where relative/path/to/model.json is the path to the JSON AST file for this service, relative to project root.

The Swift code generator will use this file to locate the model at build time.

}

LOGGER.info("Generating Smithy model file info")
SmithyModelFileInfoGenerator(ctx).writeSmithyModelFileInfo()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Invokes the model file info generator (see generator implementation immediately below.)

import PackagePlugin

@main
struct SmithyCodeGeneratorPlugin: BuildToolPlugin {
Copy link
Contributor Author

@jbelkins jbelkins Nov 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the build tool plugin implementation.

  • Reads the smithy-model-info.json file to get the model location in the form of a file URL. (All other source files in the target are ignored.)
  • Constructs an output file URL for code-generated schemas. This file will be written to the plugin's "working directory" which is provided by the Swift build system.
  • Constructs a build command that uses the SmithyCodegenCLI tool to read the model & generate a schemas file from it.

Note that the plugin doesn't actually invoke the code generation tool. Rather, the Swift build system will insert the invocation into the build process and will perform it at the appropriate time.

import SmithyCodegenCore

@main
struct SmithyCodegenCLI: AsyncParsableCommand {
Copy link
Contributor Author

@jbelkins jbelkins Nov 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the CLI tool that will be invoked to perform code generation.

This CLI tool is currently just a shell - it resolves file URLs for its parameters, then just creates an empty schemas Swift file as its output (the file must exist, even if empty, for the build to succeed, since the build plugin declares a schemas Swift file as its output). Actually implementing code generation will be a follow-on task.

@aws-sdk-swift-automation aws-sdk-swift-automation merged commit a64b53b into epic/sbs Nov 19, 2025
33 checks passed
@aws-sdk-swift-automation aws-sdk-swift-automation deleted the jbe/swift_codegen_plugin branch November 19, 2025 19:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants