Skip to content

Commit be41136

Browse files
authored
Generate code file for protos with no definitions (#27)
This change is broken out of #26 ### Motivation To prepare for use in a SwiftPM build plugin which requires deterministic output files and to match the behavior of `proto-gen-swift` we should generate a source file even if no definitions are found. ### Modifications: * We no longer return early in the case of no definitions being found. * Add empty proto file test * Add a preamble to `foo-messages.proto` * Don't unconditionally add a dependency on `GRPCProtobuf` if there are no services * Add the new test case code generation to `dev/protos/generate.sh` * Depend on `grpc-swift` `main` to pick up empty file generation changes. ### Result: We will generate a Swift source file even if no definitions are found.
1 parent 3ed26da commit be41136

File tree

8 files changed

+874
-817
lines changed

8 files changed

+874
-817
lines changed

Package.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ let products: [Product] = [
3131
let dependencies: [Package.Dependency] = [
3232
.package(
3333
url: "https://github.com/grpc/grpc-swift.git",
34-
exact: "2.0.0-beta.2"
34+
branch: "main"
3535
),
3636
.package(
3737
url: "https://github.com/apple/swift-protobuf.git",

Sources/GRPCProtobufCodeGen/ProtobufCodeGenParser.swift

+4-1
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,13 @@ package struct ProtobufCodeGenParser {
9898

9999
extension ProtobufCodeGenParser {
100100
fileprivate func codeDependencies(file: FileDescriptor) -> [Dependency] {
101+
guard file.services.count > 0 else {
102+
return []
103+
}
104+
101105
var codeDependencies: [Dependency] = [
102106
Dependency(module: "GRPCProtobuf", accessLevel: .internal)
103107
]
104-
105108
// If there's a dependency on a bundled proto then add the SwiftProtobuf import.
106109
//
107110
// Importing SwiftProtobuf unconditionally results in warnings in the generated

Sources/protoc-gen-grpc-swift/GenerateGRPC.swift

-4
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,6 @@ final class GenerateGRPC: CodeGenerator {
6363
)
6464
}
6565

66-
if descriptor.services.isEmpty {
67-
continue
68-
}
69-
7066
try self.generateV2Stubs(descriptor, options: options, outputs: outputs)
7167
}
7268
}
Binary file not shown.
Binary file not shown.

Tests/GRPCProtobufCodeGenTests/ProtobufCodeGeneratorTests.swift

+856-811
Large diffs are not rendered by default.

dev/protos/generate.sh

+12
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,17 @@ function generate_foo_service_descriptor_set {
119119
--include_imports
120120
}
121121

122+
function generate_foo_messages_descriptor_set {
123+
local proto proto_path output
124+
proto="$here/local/foo-messages.proto"
125+
proto_path="$(dirname "$proto")"
126+
output="$root/Tests/GRPCProtobufCodeGenTests/Generated/foo-messages.pb"
127+
128+
invoke_protoc --descriptor_set_out="$output" "$proto" -I "$proto_path" \
129+
--include_source_info \
130+
--include_imports
131+
}
132+
122133
function generate_bar_service_descriptor_set {
123134
local proto proto_path output
124135
proto="$here/local/bar-service.proto"
@@ -152,5 +163,6 @@ generate_error_service
152163
# Descriptor sets for tests
153164
generate_test_service_descriptor_set
154165
generate_foo_service_descriptor_set
166+
generate_foo_messages_descriptor_set
155167
generate_bar_service_descriptor_set
156168
generate_wkt_service_descriptor_set

dev/protos/local/foo-messages.proto

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Leading trivia.
12
syntax = "proto3";
23

34
package foo;

0 commit comments

Comments
 (0)