Skip to content

Commit cf29048

Browse files
glbrnttgjcairo
andauthored
Make examples standalone packages (#2067)
Motivation: Standalone packages for examples are an easier on-ramp for newcomers as they only see the details they care about and it can act as a starting point which they can develop from. Modifications: - Turn each example into a standalone package and add a README to each - Update the generate and fetch scripts and re-run them Result: Better examples --------- Co-authored-by: Gus Cairo <[email protected]>
1 parent 9bdb0d1 commit cf29048

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+495
-1372
lines changed

.github/workflows/ci.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,18 @@ jobs:
6565
working-directory: "./IntegrationTests/Benchmarks"
6666
run: swift package benchmark baseline check --no-progress --check-absolute-path Thresholds/${{ matrix.swift-version }}/
6767
timeout-minutes: 20
68+
examples:
69+
strategy:
70+
fail-fast: false
71+
matrix:
72+
include:
73+
- image: swiftlang/swift:nightly-jammy
74+
- image: swift:6.0-jammy
75+
name: Build examples using ${{ matrix.image }}
76+
runs-on: ubuntu-latest
77+
container:
78+
image: ${{ matrix.image }}
79+
steps:
80+
- uses: actions/checkout@v4
81+
- name: Build examples
82+
run: ./dev/build-examples.sh

Examples/echo/.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.DS_Store
2+
/.build
3+
/Packages
4+
xcuserdata/
5+
DerivedData/
6+
.swiftpm/configuration/registries.json
7+
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
8+
.netrc

Examples/echo/Package.swift

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// swift-tools-version:6.0
2+
/*
3+
* Copyright 2024, gRPC Authors All rights reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import PackageDescription
19+
20+
let package = Package(
21+
name: "echo",
22+
platforms: [.macOS("15.0")],
23+
dependencies: [
24+
.package(url: "https://github.com/grpc/grpc-swift-protobuf", branch: "main"),
25+
.package(url: "https://github.com/grpc/grpc-swift-nio-transport", branch: "main"),
26+
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.5.0"),
27+
],
28+
targets: [
29+
.executableTarget(
30+
name: "echo",
31+
dependencies: [
32+
.product(name: "GRPCNIOTransportHTTP2", package: "grpc-swift-nio-transport"),
33+
.product(name: "GRPCProtobuf", package: "grpc-swift-protobuf"),
34+
.product(name: "ArgumentParser", package: "swift-argument-parser"),
35+
]
36+
)
37+
]
38+
)

Examples/echo/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Echo
2+
3+
This example demonstrates all four RPC types using a simple 'echo' service and
4+
client and the Swift NIO based HTTP/2 transport.
5+
6+
## Overview
7+
8+
An "echo" command line tool that uses generated stubs for an 'echo' service
9+
which allows you to start a server and to make requests against it for each of
10+
the four RPC types.
11+
12+
The tool uses the [SwiftNIO](https://github.com/grpc/grpc-swift-nio-transport)
13+
HTTP/2 transport.
14+
15+
## Usage
16+
17+
Build and run the server using the CLI:
18+
19+
```console
20+
$ swift run echo serve
21+
Echo listening on [ipv4]127.0.0.1:1234
22+
```
23+
24+
Use the CLI to make a unary 'Get' request against it:
25+
26+
```console
27+
$ swift run echo get --message "Hello"
28+
get → Hello
29+
get ← Hello
30+
```
31+
32+
Use the CLI to make a bidirectional streaming 'Update' request:
33+
34+
```console
35+
$ swift run echo update --message "Hello World"
36+
update → Hello
37+
update → World
38+
update ← Hello
39+
update ← World
40+
```
41+
42+
Get help with the CLI by running:
43+
44+
```console
45+
$ swift run echo --help
46+
```

Examples/v2/echo/Echo.swift renamed to Examples/echo/Sources/Echo.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import ArgumentParser
1818

1919
@main
20-
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
2120
struct Echo: AsyncParsableCommand {
2221
static let configuration = CommandConfiguration(
2322
commandName: "echo",
File renamed without changes.
File renamed without changes.

Examples/v2/echo/Subcommands/ClientArguments.swift renamed to Examples/echo/Sources/Subcommands/ClientArguments.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
import ArgumentParser
18-
import GRPCHTTP2Core
18+
import GRPCNIOTransportHTTP2
1919

2020
struct ClientArguments: ParsableArguments {
2121
@Option(help: "The server's listening port")

Examples/v2/echo/Subcommands/Collect.swift renamed to Examples/echo/Sources/Subcommands/Collect.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@
1616

1717
import ArgumentParser
1818
import GRPCCore
19-
import GRPCHTTP2Core
20-
import GRPCHTTP2TransportNIOPosix
19+
import GRPCNIOTransportHTTP2
2120

22-
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
2321
struct Collect: AsyncParsableCommand {
2422
static let configuration = CommandConfiguration(
2523
abstract: "Makes a client streaming RPC to the echo server."

Examples/v2/echo/Subcommands/Expand.swift renamed to Examples/echo/Sources/Subcommands/Expand.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@
1616

1717
import ArgumentParser
1818
import GRPCCore
19-
import GRPCHTTP2Core
20-
import GRPCHTTP2TransportNIOPosix
19+
import GRPCNIOTransportHTTP2
2120

22-
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
2321
struct Expand: AsyncParsableCommand {
2422
static let configuration = CommandConfiguration(
2523
abstract: "Makes a server streaming RPC to the echo server."

0 commit comments

Comments
 (0)