Generator.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. // swift-format-ignore-file
  100. //
  101. // Generated by the protocol buffer compiler.
  102. // Source: \(self.file.name)
  103. //
  104. //
  105. // Copyright 2018, gRPC Authors All rights reserved.
  106. //
  107. // Licensed under the Apache License, Version 2.0 (the "License");
  108. // you may not use this file except in compliance with the License.
  109. // You may obtain a copy of the License at
  110. //
  111. // http://www.apache.org/licenses/LICENSE-2.0
  112. //
  113. // Unless required by applicable law or agreed to in writing, software
  114. // distributed under the License is distributed on an "AS IS" BASIS,
  115. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  116. // See the License for the specific language governing permissions and
  117. // limitations under the License.
  118. //\n
  119. """)
  120. let moduleNames = [
  121. self.options.gRPCModuleName,
  122. "NIO",
  123. "NIOConcurrencyHelpers",
  124. self.options.swiftProtobufModuleName,
  125. ]
  126. for moduleName in (moduleNames + self.options.extraModuleImports).sorted() {
  127. self.println("import \(moduleName)")
  128. }
  129. // Add imports for required modules
  130. let moduleMappings = self.options.protoToModuleMappings
  131. for importedProtoModuleName in moduleMappings.neededModules(forFile: self.file) ?? [] {
  132. self.println("import \(importedProtoModuleName)")
  133. }
  134. self.println()
  135. // We defer the check for printing clients to `printClient()` since this could be the 'real'
  136. // client or the test client.
  137. for service in self.file.services {
  138. self.service = service
  139. self.printClient()
  140. }
  141. self.println()
  142. if self.options.generateServer {
  143. for service in self.file.services {
  144. self.service = service
  145. printServer()
  146. }
  147. }
  148. }
  149. func printAvailabilityForAsyncAwait() {
  150. self.println("@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)")
  151. }
  152. }