Skip to content

Commit

Permalink
Generate code file for protos with no definitions (#27)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rnro authored Jan 7, 2025
1 parent 3ed26da commit be41136
Show file tree
Hide file tree
Showing 8 changed files with 874 additions and 817 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ let products: [Product] = [
let dependencies: [Package.Dependency] = [
.package(
url: "https://github.com/grpc/grpc-swift.git",
exact: "2.0.0-beta.2"
branch: "main"
),
.package(
url: "https://github.com/apple/swift-protobuf.git",
Expand Down
5 changes: 4 additions & 1 deletion Sources/GRPCProtobufCodeGen/ProtobufCodeGenParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,13 @@ package struct ProtobufCodeGenParser {

extension ProtobufCodeGenParser {
fileprivate func codeDependencies(file: FileDescriptor) -> [Dependency] {
guard file.services.count > 0 else {
return []
}

var codeDependencies: [Dependency] = [
Dependency(module: "GRPCProtobuf", accessLevel: .internal)
]

// If there's a dependency on a bundled proto then add the SwiftProtobuf import.
//
// Importing SwiftProtobuf unconditionally results in warnings in the generated
Expand Down
4 changes: 0 additions & 4 deletions Sources/protoc-gen-grpc-swift/GenerateGRPC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ final class GenerateGRPC: CodeGenerator {
)
}

if descriptor.services.isEmpty {
continue
}

try self.generateV2Stubs(descriptor, options: options, outputs: outputs)
}
}
Expand Down
Binary file not shown.
Binary file modified Tests/GRPCProtobufCodeGenTests/Generated/foo-service.pb
Binary file not shown.
1,667 changes: 856 additions & 811 deletions Tests/GRPCProtobufCodeGenTests/ProtobufCodeGeneratorTests.swift

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions dev/protos/generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,17 @@ function generate_foo_service_descriptor_set {
--include_imports
}

function generate_foo_messages_descriptor_set {
local proto proto_path output
proto="$here/local/foo-messages.proto"
proto_path="$(dirname "$proto")"
output="$root/Tests/GRPCProtobufCodeGenTests/Generated/foo-messages.pb"

invoke_protoc --descriptor_set_out="$output" "$proto" -I "$proto_path" \
--include_source_info \
--include_imports
}

function generate_bar_service_descriptor_set {
local proto proto_path output
proto="$here/local/bar-service.proto"
Expand Down Expand Up @@ -152,5 +163,6 @@ generate_error_service
# Descriptor sets for tests
generate_test_service_descriptor_set
generate_foo_service_descriptor_set
generate_foo_messages_descriptor_set
generate_bar_service_descriptor_set
generate_wkt_service_descriptor_set
1 change: 1 addition & 0 deletions dev/protos/local/foo-messages.proto
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Leading trivia.
syntax = "proto3";

package foo;
Expand Down

0 comments on commit be41136

Please sign in to comment.