-
Notifications
You must be signed in to change notification settings - Fork 434
Update tutorials to use the build plugin #2178
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 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
25 changes: 25 additions & 0 deletions
25
.../Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec01-step09-plugin.swift
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// swift-tools-version: 6.0 | ||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "RouteGuide", | ||
platforms: [.macOS(.v15)], | ||
dependencies: [ | ||
.package(url: "https://github.com/grpc/grpc-swift.git", from: "2.0.0-beta.3"), | ||
.package(url: "https://github.com/grpc/grpc-swift-protobuf.git", from: "1.0.0-beta.3"), | ||
.package(url: "https://github.com/grpc/grpc-swift-nio-transport.git", from: "1.0.0-beta.3"), | ||
], | ||
targets: [ | ||
.executableTarget( | ||
name: "RouteGuide", | ||
dependencies: [ | ||
.product(name: "GRPCCore", package: "grpc-swift"), | ||
.product(name: "GRPCNIOTransportHTTP2", package: "grpc-swift-nio-transport"), | ||
.product(name: "GRPCProtobuf", package: "grpc-swift-protobuf"), | ||
], | ||
plugins: [ | ||
.plugin(name: "GRPCProtobufGenerator", package: "grpc-swift-protobuf") | ||
] | ||
) | ||
] | ||
) |
7 changes: 7 additions & 0 deletions
7
...entation.docc/Tutorials/Route-Guide/Resources/route-guide-sec01-step10-plugin-config.json
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"generate": { | ||
"clients": true, | ||
"servers": true, | ||
"messages": true | ||
} | ||
} |
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
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
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
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 |
---|---|---|
|
@@ -20,6 +20,12 @@ | |
Before we can write any code we need to create a new Swift Package and configure it | ||
to depend on gRPC Swift. | ||
|
||
As a prerequisite you must have the Protocol Buffers compiler (`protoc`) installed. You can | ||
find the instructions for doing this in the [gRPC Swift Protobuf | ||
documentation](https://swiftpackageindex.com/grpc/grpc-swift-protobuf/documentation/grpcprotobuf/installing-protoc). | ||
The remainder of this tutorial assumes you installed `protoc` and it's available in | ||
your `$PATH`. | ||
|
||
@Steps { | ||
@Step { | ||
Create a new directory called for the package called `RouteGuide`. | ||
|
@@ -84,6 +90,24 @@ | |
|
||
@Code(name: "Package.swift", file: "route-guide-sec01-step08-description.swift") | ||
} | ||
|
||
@Step { | ||
We'll also add a build plugin. This allows the build system to generate gRPC code at build | ||
time rather than having to generate it with separate tooling. | ||
|
||
@Code(name: "Package.swift", file: "route-guide-sec01-step09-plugin.swift") | ||
} | ||
|
||
@Step { | ||
A configuration file is required so that the plugin knows what to generate. Create | ||
a JSON file in the `Sources/Protos` directory called `grpc-swift-proto-generator-config.json` | ||
with this content. | ||
|
||
The name of the file (`grpc-swift-proto-generator-config.json`) is important: the plugin | ||
looks for files matching this name in the source directory of your target. | ||
|
||
@Code(name: "Sources/Protos/grpc-swift-proto-generator-config.json", file: "route-guide-sec01-step10-plugin-config.json") | ||
} | ||
} | ||
} | ||
|
||
|
@@ -93,16 +117,26 @@ | |
|
||
@Steps { | ||
@Step { | ||
Create a new empty file in the `Protos` directory called `route_guide.proto`. We'll use | ||
the "proto3" syntax and our service will be part of the "routeguide" package. | ||
Create a new directory in the `Sources/Protos` directory called `routeguide` | ||
using `mkdir Sources/Protos/routeguide`. | ||
} | ||
|
||
@Step { | ||
Create a new empty file in the `Sources/Protos/routeguide` directory | ||
called `route_guide.proto`. We'll use the "proto3" syntax and our service will be part of | ||
the "routeguide" package. | ||
|
||
@Code(name: "Protos/route_guide.proto", file: "route-guide-sec02-step01-import.proto") | ||
It's good practice to organise your `.proto` files according to the package they are | ||
|
||
declared in, that's why we created the `routeguide` directory to match the "routeguide" | ||
package name. | ||
|
||
@Code(name: "Sources/Protos/routeguide/route_guide.proto", file: "route-guide-sec02-step01-import.proto") | ||
} | ||
|
||
@Step { | ||
To define a service we create a named `service` in the `.proto` file. | ||
|
||
@Code(name: "Protos/route_guide.proto", file: "route-guide-sec02-step02-service.proto") | ||
@Code(name: "Sources/Protos/routeguide/route_guide.proto", file: "route-guide-sec02-step02-service.proto") | ||
} | ||
|
||
@Step { | ||
|
@@ -113,7 +147,7 @@ | |
A *unary RPC* where the client sends a request to the server using the stub | ||
and waits for a response to come back, just like a normal function call. | ||
|
||
@Code(name: "Protos/route_guide.proto", file: "route-guide-sec02-step03-unary.proto") | ||
@Code(name: "Sources/Protos/routeguide/route_guide.proto", file: "route-guide-sec02-step03-unary.proto") | ||
} | ||
|
||
@Step { | ||
|
@@ -123,7 +157,7 @@ | |
example, you specify a server-side streaming method by placing the `stream` | ||
keyword before the *response* type. | ||
|
||
@Code(name: "Protos/route_guide.proto", file: "route-guide-sec02-step04-server-streaming.proto") | ||
@Code(name: "Sources/Protos/routeguide/route_guide.proto", file: "route-guide-sec02-step04-server-streaming.proto") | ||
} | ||
|
||
@Step { | ||
|
@@ -133,7 +167,7 @@ | |
and return its response. You specify a client-side streaming method by placing | ||
the `stream` keyword before the *request* type. | ||
|
||
@Code(name: "Protos/route_guide.proto", file: "route-guide-sec02-step05-client-streaming.proto") | ||
@Code(name: "Sources/Protos/routeguide/route_guide.proto", file: "route-guide-sec02-step05-client-streaming.proto") | ||
} | ||
|
||
@Step { | ||
|
@@ -146,53 +180,30 @@ | |
stream is preserved. You specify this type of method by placing the `stream` | ||
keyword before both the request and the response. | ||
|
||
@Code(name: "Protos/route_guide.proto", file: "route-guide-sec02-step06-bidi-streaming.proto") | ||
@Code(name: "Sources/Protos/routeguide/route_guide.proto", file: "route-guide-sec02-step06-bidi-streaming.proto") | ||
} | ||
|
||
@Step { | ||
The `.proto` file also contains the Protocol Buffers message type definitions for all | ||
request and response messages used by the service. | ||
|
||
@Code(name: "Protos/route_guide.proto", file: "route-guide-sec02-step07-messages.proto") | ||
@Code(name: "Sources/Protos/routeguide/route_guide.proto", file: "route-guide-sec02-step07-messages.proto") | ||
} | ||
} | ||
} | ||
|
||
@Section(title: "Generating client and server code") { | ||
Next we need to generate the gRPC client and server interfaces from our `.proto` | ||
service definition. We do this using the Protocol Buffer compiler, `protoc`, with | ||
two plugins: one with support for Swift (via [Swift Protobuf](https://github.com/apple/swift-protobuf)) | ||
and the other for gRPC. This section assumes you already have `protoc` installed. | ||
service definition. As we're using the build plugin we just need to build our project. | ||
|
||
To learn more about generating code check out the <doc:Generating-stubs> article. | ||
|
||
@Steps { | ||
@Step { | ||
First we need to build the two plugins for `protoc`, `protoc-gen-swift` and | ||
`protoc-gen-grpc-swift`. | ||
|
||
@Code(name: "Console", file: "route-guide-sec03-step01-protoc-plugins.txt") | ||
} | ||
|
||
@Step { | ||
We'll generate the code into a separate directory within `Sources` called `Generated` which | ||
we need to create first. | ||
|
||
@Code(name: "Console", file: "route-guide-sec03-step02-mkdir.txt") | ||
} | ||
|
||
@Step { | ||
Now run `protoc` to generate the messages. This will create | ||
`Sources/Generated/route_guide.pb.swift`. | ||
|
||
@Code(name: "Console", file: "route-guide-sec03-step03-gen-messages.txt") | ||
} | ||
|
||
@Step { | ||
Run `protoc` again to generate the service code. This will create | ||
`Sources/Generated/route_guide.grpc.swift`. | ||
Build the project using `PROTOC_PATH=$(which protoc) swift build`. | ||
|
||
@Code(name: "Console", file: "route-guide-sec03-step04-gen-grpc.txt") | ||
If you are using Xcode or another IDE then you'll need to set the environment variable | ||
appropriately. | ||
} | ||
} | ||
} | ||
|
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.
Should we explain why
PROTOC_PATH
is needed and/or link to the plugin docs that explain it in more detail?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.
Yeah that's a great point, I'll do that.