Generator.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. //\n
  104. """)
  105. let moduleNames = [
  106. self.options.gRPCModuleName,
  107. "NIO",
  108. "NIOConcurrencyHelpers",
  109. self.options.swiftProtobufModuleName,
  110. ]
  111. for moduleName in (moduleNames + self.options.extraModuleImports).sorted() {
  112. self.println("import \(moduleName)")
  113. }
  114. // Add imports for required modules
  115. let moduleMappings = self.options.protoToModuleMappings
  116. for importedProtoModuleName in moduleMappings.neededModules(forFile: self.file) ?? [] {
  117. self.println("import \(importedProtoModuleName)")
  118. }
  119. self.println()
  120. // We defer the check for printing clients to `printClient()` since this could be the 'real'
  121. // client or the test client.
  122. for service in self.file.services {
  123. self.service = service
  124. self.printClient()
  125. }
  126. self.println()
  127. if self.options.generateServer {
  128. for service in self.file.services {
  129. self.service = service
  130. printServer()
  131. }
  132. }
  133. }
  134. func printAvailabilityForAsyncAwait() {
  135. self.println("@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)")
  136. }
  137. }