Skip to content

Commit 0494be6

Browse files
committed
Code generation package plugins
Motivation: To make code generation more convenient for adopters. Modifications: * New build plugin to generate gRPC services and protobuf messages * New command plugin e.g. `swift package generate-grpc-code-from-protos Sources/Protos/HelloWorld.proto` * Integration tests (incomplete) This PR also includes a change to `protoc-gen-grpc-swift` itself: * Generate an empty file if no gRPC services are found in the protobuf definition file. This is required by the build plugin which needs deterministic outputs and matches the behavior of `protoc-gen-swift`. Result: * Users will be able to make use of the build and command plugins. * `protoc-gen-grpc-swift` will Generate an empty file if no gRPC services are found in the protobuf definition file.
1 parent 3ed26da commit 0494be6

File tree

43 files changed

+1887
-4
lines changed

Some content is hidden

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

43 files changed

+1887
-4
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"config":[{"name":"Plugin tests (6.0)","swift_version":"6.0","runner":"ubuntu-latest","image":"swift:6.0-jammy","platform":"Linux","setup_command":"apt-get update -y -q && apt-get install -y -q curl protobuf-compiler","command":"curl -s https://raw.githubusercontent.com/grpc/grpc-swift-protobuf/package_plugins/dev/plugin-tests.sh | bash","command_arguments":""},{"name":"Plugin tests (nightly-6.0)","swift_version":"nightly-6.0","runner":"ubuntu-latest","image":"swiftlang/swift:nightly-6.0-jammy","platform":"Linux","setup_command":"apt-get update -y -q && apt-get install -y -q curl protobuf-compiler","command":"curl -s https://raw.githubusercontent.com/grpc/grpc-swift-protobuf/package_plugins/dev/plugin-tests.sh | bash","command_arguments":""},{"name":"Plugin tests (nightly-main)","swift_version":"nightly-main","runner":"ubuntu-latest","image":"swiftlang/swift:nightly-main-jammy","platform":"Linux","setup_command":"apt-get update -y -q && apt-get install -y -q curl protobuf-compiler","command":"curl -s https://raw.githubusercontent.com/grpc/grpc-swift-protobuf/package_plugins/dev/plugin-tests.sh | bash","command_arguments":""}]}

.github/workflows/pull_request.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ jobs:
2626
linux_nightly_6_0_arguments_override: "--explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable"
2727
linux_nightly_main_arguments_override: "--explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable"
2828

29+
plugin-tests:
30+
name: Plugin tests
31+
uses: apple/swift-nio/.github/workflows/swift_load_test_matrix.yml@main
32+
with:
33+
name: "Plugin tests"
34+
matrix_path: ".github/workflows/matrices/plugin-tests.json"
35+
2936
cxx-interop:
3037
name: Cxx interop
3138
uses: apple/swift-nio/.github/workflows/cxx_interop.yml@main
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
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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: "grpc-adopter",
22+
platforms: [
23+
.macOS(.v15),
24+
.iOS(.v18),
25+
.tvOS(.v18),
26+
.watchOS(.v11),
27+
.visionOS(.v2),
28+
],
29+
dependencies: [
30+
.package(
31+
path: "../../../../grpc-swift-protobuf"
32+
),
33+
.package(
34+
url: "https://github.com/grpc/grpc-swift.git",
35+
exact: "2.0.0-beta.2"
36+
),
37+
],
38+
targets: [
39+
.executableTarget(
40+
name: "grpc-adopter",
41+
dependencies: [
42+
.product(name: "GRPCCore", package: "grpc-swift"),
43+
.product(name: "GRPCInProcessTransport", package: "grpc-swift"),
44+
.product(name: "GRPCProtobuf", package: "grpc-swift-protobuf"),
45+
],
46+
plugins: [
47+
.plugin(name: "GRPCGeneratorPlugin", package: "grpc-swift-protobuf")
48+
]
49+
)
50+
]
51+
)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2015, gRPC Authors All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
syntax = "proto3";
15+
16+
option java_multiple_files = true;
17+
option java_package = "io.grpc.examples.helloworld";
18+
option java_outer_classname = "HelloWorldProto";
19+
option objc_class_prefix = "HLW";
20+
21+
package helloworld;
22+
23+
// The greeting service definition.
24+
service Greeter {
25+
// Sends a greeting
26+
rpc SayHello (HelloRequest) returns (HelloReply) {}
27+
}
28+
29+
// The request message containing the user's name.
30+
message HelloRequest {
31+
string name = 1;
32+
}
33+
34+
// The response message containing the greetings
35+
message HelloReply {
36+
string message = 1;
37+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2024, gRPC Authors All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import GRPCCore
18+
import GRPCInProcessTransport
19+
import GRPCProtobuf
20+
21+
@main
22+
struct PluginAdopter {
23+
static func main() async throws {
24+
let inProcess = InProcessTransport()
25+
try await withGRPCServer(transport: inProcess.server, services: [Greeter()]) { server in
26+
try await withGRPCClient(transport: inProcess.client) { client in
27+
try await Self.doRPC(Helloworld_Greeter.Client(wrapping: client))
28+
}
29+
}
30+
}
31+
32+
static func doRPC(_ greeter: Helloworld_Greeter.Client) async throws {
33+
do {
34+
let reply = try await greeter.sayHello(.with { $0.name = "(ignored)" })
35+
print("Reply: \(reply.message)")
36+
} catch {
37+
print("Error: \(error)")
38+
}
39+
}
40+
}
41+
42+
struct Greeter: Helloworld_Greeter.SimpleServiceProtocol {
43+
func sayHello(
44+
request: Helloworld_HelloRequest,
45+
context: ServerContext
46+
) async throws -> Helloworld_HelloReply {
47+
return .with { reply in
48+
reply.message = "Hello, world!"
49+
}
50+
}
51+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"visibility": "internal"
3+
}
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
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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: "grpc-adopter",
22+
platforms: [
23+
.macOS(.v15),
24+
.iOS(.v18),
25+
.tvOS(.v18),
26+
.watchOS(.v11),
27+
.visionOS(.v2),
28+
],
29+
dependencies: [
30+
.package(
31+
path: "../../../../grpc-swift-protobuf"
32+
),
33+
.package(
34+
url: "https://github.com/grpc/grpc-swift.git",
35+
exact: "2.0.0-beta.2"
36+
),
37+
],
38+
targets: [
39+
.executableTarget(
40+
name: "grpc-adopter",
41+
dependencies: [
42+
.product(name: "GRPCCore", package: "grpc-swift"),
43+
.product(name: "GRPCInProcessTransport", package: "grpc-swift"),
44+
.product(name: "GRPCProtobuf", package: "grpc-swift-protobuf"),
45+
],
46+
plugins: [
47+
.plugin(name: "GRPCGeneratorPlugin", package: "grpc-swift-protobuf")
48+
]
49+
)
50+
]
51+
)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2015, gRPC Authors All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
syntax = "proto3";
15+
16+
option java_multiple_files = true;
17+
option java_package = "io.grpc.examples.helloworld";
18+
option java_outer_classname = "HelloWorldProto";
19+
option objc_class_prefix = "HLW";
20+
21+
package helloworld;
22+
23+
// The greeting service definition.
24+
service Greeter {
25+
// Sends a greeting
26+
rpc SayHello (HelloRequest) returns (HelloReply) {}
27+
}
28+
29+
// The request message containing the user's name.
30+
message HelloRequest {
31+
string name = 1;
32+
}
33+
34+
// The response message containing the greetings
35+
message HelloReply {
36+
string message = 1;
37+
}

0 commit comments

Comments
 (0)