Generator.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright 2018, 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. import SwiftProtobufPluginLibrary
  17. class Generator {
  18. internal var options: GeneratorOptions
  19. private var printer: CodePrinter
  20. internal var file: FileDescriptor
  21. internal var service: ServiceDescriptor! // context during generation
  22. internal var method: MethodDescriptor! // context during generation
  23. internal let protobufNamer: SwiftProtobufNamer
  24. init(_ file: FileDescriptor, options: GeneratorOptions) {
  25. self.file = file
  26. self.options = options
  27. self.printer = CodePrinter()
  28. self.protobufNamer = SwiftProtobufNamer(
  29. currentFile: file,
  30. protoFileToModuleMappings: options.protoToModuleMappings)
  31. printMain()
  32. }
  33. public var code: String {
  34. return printer.content
  35. }
  36. internal func println(_ text: String = "") {
  37. printer.print(text)
  38. printer.print("\n")
  39. }
  40. internal func indent() {
  41. printer.indent()
  42. }
  43. internal func outdent() {
  44. printer.outdent()
  45. }
  46. private func printMain() {
  47. printer.print("""
  48. //
  49. // DO NOT EDIT.
  50. //
  51. // Generated by the protocol buffer compiler.
  52. // Source: \(file.name)
  53. //
  54. //
  55. // Copyright 2018, gRPC Authors All rights reserved.
  56. //
  57. // Licensed under the Apache License, Version 2.0 (the "License");
  58. // you may not use this file except in compliance with the License.
  59. // You may obtain a copy of the License at
  60. //
  61. // http://www.apache.org/licenses/LICENSE-2.0
  62. //
  63. // Unless required by applicable law or agreed to in writing, software
  64. // distributed under the License is distributed on an "AS IS" BASIS,
  65. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  66. // See the License for the specific language governing permissions and
  67. // limitations under the License.
  68. //\n
  69. """)
  70. let moduleNames = [
  71. "Foundation",
  72. "NIO",
  73. "NIOHTTP1",
  74. "GRPC",
  75. "SwiftProtobuf"
  76. ]
  77. for moduleName in (moduleNames + options.extraModuleImports).sorted() {
  78. println("import \(moduleName)")
  79. }
  80. // Build systems like Bazel will generate the Swift service definitions in a different module
  81. // than the rest of the protos defined in the same file (because they are generated by separate
  82. // build rules that invoke the separate generator plugins). So, we need to specify module
  83. // mappings to import the service protos as well as and any other proto modules that the file
  84. // imports.
  85. let moduleMappings = options.protoToModuleMappings
  86. if let serviceProtoModuleName = moduleMappings.moduleName(forFile: file) {
  87. println("import \(serviceProtoModuleName)")
  88. }
  89. for importedProtoModuleName in moduleMappings.neededModules(forFile: file) ?? [] {
  90. println("import \(importedProtoModuleName)")
  91. }
  92. println()
  93. if options.generateClient {
  94. for service in file.services {
  95. self.service = service
  96. printClient()
  97. }
  98. }
  99. println()
  100. if options.generateServer {
  101. for service in file.services {
  102. self.service = service
  103. printServer()
  104. }
  105. }
  106. println()
  107. printProtoBufExtensions()
  108. }
  109. internal func printProtoBufExtensions() {
  110. var writtenValues = Set<String>()
  111. println("/// Provides conformance to `GRPCPayload` for the request and response messages")
  112. for service in file.services {
  113. self.service = service
  114. for method in service.methods {
  115. self.method = method
  116. printExtension(for: methodInputName, typesSeen: &writtenValues)
  117. printExtension(for: methodOutputName, typesSeen: &writtenValues)
  118. }
  119. println()
  120. }
  121. }
  122. private func printExtension(for messageType: String, typesSeen: inout Set<String>) {
  123. guard !typesSeen.contains(messageType) else { return }
  124. println("extension \(messageType): GRPCProtobufPayload {}")
  125. typesSeen.insert(messageType)
  126. }
  127. }