Skip to content

Commit 8b5d7d9

Browse files
authored
Allow the gRPC module name to be customised when generating code (grpc#945)
Motivation: Some Cocoapods may find themselves in a situation where their dependency graph contains both gRPC Swift and the gRPC core library. This is problematic since both have the same module name ("GRPC"). Changing our own module name is a very large breaking change and one we'd like to avoid. As an escape valve, users who take a direct dependency on us may rename our module via their Podfile; doing so would require their generated code to depend on that module, rather than the currently hardcoded 'GRPC' module. Modifications: - Add an option to the codegen to specify the gRPC module name - Remove a couple of imports which were previously used but are now no longer require - Regenerate Result: It's possible for Cocoapods users to take a direct dependency on gRPC Swift and libgrpc.
1 parent a59c5e5 commit 8b5d7d9

File tree

16 files changed

+23
-40
lines changed

16 files changed

+23
-40
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ the following line to your `Podfile`:
102102
Then, run `pod install` from command line and use your project's generated
103103
`.xcworkspace` file. You might also need to add `use_frameworks!` to your `Podfile`.
104104

105+
*⚠️ If you have conficting modules as a result of having a transitive
106+
dependency on '[gRPC-Core][grpc-core-pod]' see [grpc/grpc-swift#945][grpc-swift-945].*
107+
105108
### Getting the `protoc` Plugins
106109

107110
Binary releases of `protoc`, the Protocol Buffer Compiler, are available on
@@ -171,6 +174,8 @@ Please get involved! See our [guidelines for contributing](CONTRIBUTING.md).
171174
[docs-keepalive]: ./docs/keepalive.md
172175
[docs-tutorial]: ./docs/basic-tutorial.md
173176
[grpc]: https://github.com/grpc/grpc
177+
[grpc-core-pod]: https://cocoapods.org/pods/gRPC-Core
178+
[grpc-swift-945]: https://github.com/grpc/grpc-swift/pull/945
174179
[protobuf-releases]: https://github.com/protocolbuffers/protobuf/releases
175180
[swift-nio-platforms]: https://github.com/apple/swift-nio#supported-platforms
176181
[swift-nio]: https://github.com/apple/swift-nio

Sources/Examples/Echo/Model/echo.grpc.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,8 @@
2020
// See the License for the specific language governing permissions and
2121
// limitations under the License.
2222
//
23-
import Foundation
2423
import GRPC
2524
import NIO
26-
import NIOHTTP1
27-
import SwiftProtobuf
2825

2926

3027
/// Usage: instantiate Echo_EchoClient, then call methods of this protocol to make API calls.

Sources/Examples/HelloWorld/Model/helloworld.grpc.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,8 @@
2020
// See the License for the specific language governing permissions and
2121
// limitations under the License.
2222
//
23-
import Foundation
2423
import GRPC
2524
import NIO
26-
import NIOHTTP1
27-
import SwiftProtobuf
2825

2926

3027
/// Usage: instantiate Helloworld_GreeterClient, then call methods of this protocol to make API calls.

Sources/Examples/RouteGuide/Model/route_guide.grpc.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,8 @@
2020
// See the License for the specific language governing permissions and
2121
// limitations under the License.
2222
//
23-
import Foundation
2423
import GRPC
2524
import NIO
26-
import NIOHTTP1
27-
import SwiftProtobuf
2825

2926

3027
/// Usage: instantiate Routeguide_RouteGuideClient, then call methods of this protocol to make API calls.

Sources/GRPCInteroperabilityTestModels/Generated/test.grpc.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,8 @@
2020
// See the License for the specific language governing permissions and
2121
// limitations under the License.
2222
//
23-
import Foundation
2423
import GRPC
2524
import NIO
26-
import NIOHTTP1
27-
import SwiftProtobuf
2825

2926

3027
/// Usage: instantiate Grpc_Testing_TestServiceClient, then call methods of this protocol to make API calls.

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,8 @@ class Generator {
8888
""")
8989

9090
let moduleNames = [
91-
"Foundation",
91+
self.options.gRPCModuleName,
9292
"NIO",
93-
"NIOHTTP1",
94-
"GRPC",
95-
"SwiftProtobuf",
9693
]
9794

9895
for moduleName in (moduleNames + self.options.extraModuleImports).sorted() {

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ final class GeneratorOptions {
5858
private(set) var protoToModuleMappings = ProtoFileToModuleMappings()
5959
private(set) var fileNaming = FileNaming.FullPath
6060
private(set) var extraModuleImports: [String] = []
61+
private(set) var gRPCModuleName = "GRPC"
6162

6263
init(parameter: String?) throws {
6364
for pair in GeneratorOptions.parseParameter(string: parameter) {
@@ -116,6 +117,13 @@ final class GeneratorOptions {
116117
throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
117118
}
118119

120+
case "GRPCModuleName":
121+
if !pair.value.isEmpty {
122+
self.gRPCModuleName = pair.value
123+
} else {
124+
throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
125+
}
126+
119127
default:
120128
throw GenerationError.unknownParameter(name: pair.key)
121129
}

dev/codegen-tests/01-echo/golden/echo.grpc.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,8 @@
2020
// See the License for the specific language governing permissions and
2121
// limitations under the License.
2222
//
23-
import Foundation
2423
import GRPC
2524
import NIO
26-
import NIOHTTP1
27-
import SwiftProtobuf
2825

2926

3027
/// Usage: instantiate Echo_EchoClient, then call methods of this protocol to make API calls.

dev/codegen-tests/02-multifile/golden/a.grpc.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,8 @@
2020
// See the License for the specific language governing permissions and
2121
// limitations under the License.
2222
//
23-
import Foundation
2423
import GRPC
2524
import NIO
26-
import NIOHTTP1
27-
import SwiftProtobuf
2825

2926

3027
/// Usage: instantiate A_ServiceAClient, then call methods of this protocol to make API calls.

dev/codegen-tests/02-multifile/golden/b.grpc.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,8 @@
2020
// See the License for the specific language governing permissions and
2121
// limitations under the License.
2222
//
23-
import Foundation
2423
import GRPC
2524
import NIO
26-
import NIOHTTP1
27-
import SwiftProtobuf
2825

2926

3027
/// Usage: instantiate B_ServiceBClient, then call methods of this protocol to make API calls.

0 commit comments

Comments
 (0)