Pārlūkot izejas kodu

Generate code file for protos with no definitions (#27)

This change is broken out of
https://github.com/grpc/grpc-swift-protobuf/pull/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.
Rick Newton-Rogers 11 mēneši atpakaļ
vecāks
revīzija
be41136e21

+ 1 - 1
Package.swift

@@ -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",

+ 4 - 1
Sources/GRPCProtobufCodeGen/ProtobufCodeGenParser.swift

@@ -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

+ 0 - 4
Sources/protoc-gen-grpc-swift/GenerateGRPC.swift

@@ -63,10 +63,6 @@ final class GenerateGRPC: CodeGenerator {
         )
       }
 
-      if descriptor.services.isEmpty {
-        continue
-      }
-
       try self.generateV2Stubs(descriptor, options: options, outputs: outputs)
     }
   }

BIN
Tests/GRPCProtobufCodeGenTests/Generated/foo-messages.pb


BIN
Tests/GRPCProtobufCodeGenTests/Generated/foo-service.pb


Failā izmaiņas netiks attēlotas, jo tās ir par lielu
+ 724 - 473
Tests/GRPCProtobufCodeGenTests/ProtobufCodeGeneratorTests.swift


+ 12 - 0
dev/protos/generate.sh

@@ -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"
@@ -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 - 0
dev/protos/local/foo-messages.proto

@@ -1,3 +1,4 @@
+// Leading trivia.
 syntax = "proto3";
 
 package foo;

Daži faili netika attēloti, jo izmaiņu fails ir pārāk liels