2
0

Generator.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. )
  32. self.printMain()
  33. }
  34. public var code: String {
  35. return self.printer.content
  36. }
  37. internal func println(_ text: String = "", newline: Bool = true) {
  38. self.printer.print(text)
  39. if newline {
  40. self.printer.print("\n")
  41. }
  42. }
  43. internal func indent() {
  44. self.printer.indent()
  45. }
  46. internal func outdent() {
  47. self.printer.outdent()
  48. }
  49. internal func withIndentation(body: () -> Void) {
  50. self.indent()
  51. body()
  52. self.outdent()
  53. }
  54. internal enum Braces {
  55. case none
  56. case curly
  57. case round
  58. var open: String {
  59. switch self {
  60. case .none:
  61. return ""
  62. case .curly:
  63. return "{"
  64. case .round:
  65. return "("
  66. }
  67. }
  68. var close: String {
  69. switch self {
  70. case .none:
  71. return ""
  72. case .curly:
  73. return "}"
  74. case .round:
  75. return ")"
  76. }
  77. }
  78. }
  79. internal func withIndentation(
  80. _ header: String,
  81. braces: Braces,
  82. _ body: () -> Void
  83. ) {
  84. let spaceBeforeOpeningBrace: Bool
  85. switch braces {
  86. case .curly:
  87. spaceBeforeOpeningBrace = true
  88. case .round, .none:
  89. spaceBeforeOpeningBrace = false
  90. }
  91. self.println(header + "\(spaceBeforeOpeningBrace ? " " : "")" + "\(braces.open)")
  92. self.withIndentation(body: body)
  93. self.println(braces.close)
  94. }
  95. private func printMain() {
  96. self.printer.print("""
  97. //
  98. // DO NOT EDIT.
  99. //
  100. // Generated by the protocol buffer compiler.
  101. // Source: \(self.file.name)
  102. //
  103. //
  104. // Copyright 2018, gRPC Authors All rights reserved.
  105. //
  106. // Licensed under the Apache License, Version 2.0 (the "License");
  107. // you may not use this file except in compliance with the License.
  108. // You may obtain a copy of the License at
  109. //
  110. // http://www.apache.org/licenses/LICENSE-2.0
  111. //
  112. // Unless required by applicable law or agreed to in writing, software
  113. // distributed under the License is distributed on an "AS IS" BASIS,
  114. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  115. // See the License for the specific language governing permissions and
  116. // limitations under the License.
  117. //\n
  118. """)
  119. let moduleNames = [
  120. self.options.gRPCModuleName,
  121. "NIO",
  122. self.options.swiftProtobufModuleName,
  123. ]
  124. for moduleName in (moduleNames + self.options.extraModuleImports).sorted() {
  125. self.println("import \(moduleName)")
  126. }
  127. // Add imports for required modules
  128. let moduleMappings = self.options.protoToModuleMappings
  129. for importedProtoModuleName in moduleMappings.neededModules(forFile: self.file) ?? [] {
  130. self.println("import \(importedProtoModuleName)")
  131. }
  132. self.println()
  133. // We defer the check for printing clients to `printClient()` since this could be the 'real'
  134. // client or the test client.
  135. for service in self.file.services {
  136. self.service = service
  137. self.printClient()
  138. }
  139. self.println()
  140. if self.options.generateServer || self.options.generateAsyncServer {
  141. for service in self.file.services {
  142. self.service = service
  143. printServer()
  144. }
  145. }
  146. }
  147. func printAvailabilityForAsyncAwait() {
  148. self.println("@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)")
  149. }
  150. func printIfCompilerGuardForAsyncAwait() {
  151. self.println("#if compiler(>=5.5.2) && canImport(_Concurrency)")
  152. }
  153. func printEndCompilerGuardForAsyncAwait() {
  154. self.println("#endif // compiler(>=5.5.2) && canImport(_Concurrency)")
  155. }
  156. }