-
Notifications
You must be signed in to change notification settings - Fork 434
Document use of the build plugin #2176
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6cb362c
Document use of the build plugin
rnro 9e70d5c
review comments
rnro 5df133f
Merge branch 'main' into grpc-swift-protobuf_plugin_docs
rnro 299f6b8
Update Sources/GRPCCore/Documentation.docc/Articles/Generating-stubs.md
rnro 9b9c1ad
Update Sources/GRPCCore/Documentation.docc/Articles/Generating-stubs.md
rnro 3d2e229
Update Sources/GRPCCore/Documentation.docc/Articles/Generating-stubs.md
rnro 5a33c78
more review comments
rnro 7c0d31d
formatting
rnro 03aafc5
Merge branch 'main' into grpc-swift-protobuf_plugin_docs
rnro 31dfa48
Apply suggestions from code review
glbrntt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -4,19 +4,107 @@ Learn how to generate stubs for gRPC Swift from a service defined using the Prot | |||||
|
||||||
## Overview | ||||||
|
||||||
### Using protoc | ||||||
|
||||||
If you've used Protocol Buffers before then generating gRPC Swift stubs should be simple. If you're | ||||||
unfamiliar with Protocol Buffers then you should get comfortable with the concepts before | ||||||
continuing; the [Protocol Buffers website](https://protobuf.dev/) is a great place to start. | ||||||
|
||||||
The [`grpc-swift-protobuf`](https://github.com/grpc/grpc-swift-protobuf) package provides | ||||||
`protoc-gen-grpc-swift`, a program which is a plugin for the Protocol Buffers compiler, `protoc`. | ||||||
|
||||||
You can use the protoc plugin from the command line directly, or you can make use of a | ||||||
[Swift Package Manager build plugin](https://github.com/swiftlang/swift-package-manager/blob/main/Documentation/Plugins.md) | ||||||
convenience which adds the stub generation to the Swift build graph. | ||||||
You may use the build plugin either from the command line or from Xcode. | ||||||
|
||||||
|
||||||
> `protoc-gen-grpc-swift` only generates gRPC stubs, it doesn't generate messages. You must use | ||||||
> `protoc-gen-swift` to generate messages in addition to gRPC Stubs. | ||||||
|
||||||
|
||||||
To generate gRPC stubs for your `.proto` files you must run the `protoc` command with | ||||||
|
||||||
|
||||||
## Using the build plugin | ||||||
|
||||||
The build plugin (`GRPCProtobufGenerator`) is a great choice for convenient dynamic code generation, however it does come with some limitations. | ||||||
Because it generates the gRPC Swift stubs as part of the build it has the requirement that `protoc` must be available | ||||||
at compile time. This requirements means it is not a good fit for library authors who do not have | ||||||
glbrntt marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
direct control over this. | ||||||
|
||||||
The build plugin detects `.proto` files in the source tree and invokes `protoc` once for each file | ||||||
(caching results and performing the generation as necessary). | ||||||
|
||||||
### Adoption | ||||||
You must adopt Swift Package Manager build plugins on a per-target basis by modifying your package manifest | ||||||
(`Package.swift` file). To do this, declare the grpc-swift-protobuf package as a dependency and add the plugin | ||||||
to your desired targets. | ||||||
|
||||||
For example, to make use of the plugin for generating gRPC Swift stubs as part of the | ||||||
`echo-server` target: | ||||||
```swift | ||||||
targets: [ | ||||||
.executableTarget( | ||||||
name: "echo-server", | ||||||
dependencies: [ | ||||||
// ... | ||||||
], | ||||||
plugins: [ | ||||||
.plugin(name: "GRPCProtobufGenerator", package: "grpc-swift-protobuf") | ||||||
] | ||||||
) | ||||||
] | ||||||
``` | ||||||
Once this is done you need to ensure that that the `.proto` files to be used for generation | ||||||
glbrntt marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
are included in the target's source directory and that you have defined at least one configuration file. | ||||||
|
||||||
### Configuration | ||||||
|
||||||
You must provide a configuration file in the directory which encloses all .proto files (in the same directory or a parent). | ||||||
rnro marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
Configuration files, written in JSON, tell the build plugin about the options used for protoc invocations. | ||||||
rnro marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
You must name the file grpc-swift-proto-generator-config.json and structure it in the following format: | ||||||
|
You must name the file grpc-swift-proto-generator-config.json and structure it in the following format: | |
You must name the file `grpc-swift-proto-generator-config.json` and structure it in the following format: |
glbrntt marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
glbrntt marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
rnro marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
rnro marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I think this should be in the first paragraph in
### Using protoc
section