-
Notifications
You must be signed in to change notification settings - Fork 35
feat: Create code generator build tool plugin #999
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
| dependencies: [ | ||
| .product(name: "ArgumentParser", package: "swift-argument-parser"), | ||
| ] | ||
| ), |
There was a problem hiding this comment.
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"]), |
There was a problem hiding this comment.
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 | ||
|
|
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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.jsonfile 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
SmithyCodegenCLItool 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 { |
There was a problem hiding this comment.
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.
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:
smithy-model-info.jsonfile to the project which is an input to the code generator.SmithyCodeGenerator) that will insert code generation into the target's Swift build process.SmithyCodegenCLI) which will be invoked to perform code generation. (Actual codegen is unimplemented, this PR just establishes the structure.)Scope
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.