options.swift 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Copyright 2017, 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 Foundation
  17. import SwiftProtobufPluginLibrary
  18. enum GenerationError: Error {
  19. /// Raised when parsing the parameter string and found an unknown key
  20. case unknownParameter(name: String)
  21. /// Raised when a parameter was giving an invalid value
  22. case invalidParameterValue(name: String, value: String)
  23. /// Raised to wrap another error but provide a context message.
  24. case wrappedError(message: String, error: Error)
  25. var localizedDescription: String {
  26. switch self {
  27. case let .unknownParameter(name):
  28. return "Unknown generation parameter '\(name)'"
  29. case let .invalidParameterValue(name, value):
  30. return "Unknown value for generation parameter '\(name)': '\(value)'"
  31. case let .wrappedError(message, error):
  32. return "\(message): \(error.localizedDescription)"
  33. }
  34. }
  35. }
  36. final class GeneratorOptions {
  37. enum Visibility: String {
  38. case `internal` = "Internal"
  39. case `public` = "Public"
  40. case `package` = "Package"
  41. var sourceSnippet: String {
  42. switch self {
  43. case .internal:
  44. return "internal"
  45. case .public:
  46. return "public"
  47. case .package:
  48. return "package"
  49. }
  50. }
  51. }
  52. private(set) var visibility = Visibility.internal
  53. private(set) var generateServer = true
  54. private(set) var generateClient = true
  55. private(set) var generateTestClient = false
  56. private(set) var keepMethodCasing = false
  57. private(set) var protoToModuleMappings = ProtoFileToModuleMappings()
  58. private(set) var fileNaming = FileNaming.FullPath
  59. private(set) var extraModuleImports: [String] = []
  60. private(set) var gRPCModuleName = "GRPC"
  61. private(set) var swiftProtobufModuleName = "SwiftProtobuf"
  62. private(set) var generateReflectionData = false
  63. #if compiler(>=6.0)
  64. private(set) var v2 = false
  65. #endif
  66. private(set) var useAccessLevelOnImports = true
  67. init(parameter: String?) throws {
  68. for pair in GeneratorOptions.parseParameter(string: parameter) {
  69. switch pair.key {
  70. case "Visibility":
  71. if let value = Visibility(rawValue: pair.value) {
  72. self.visibility = value
  73. } else {
  74. throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
  75. }
  76. case "Server":
  77. if let value = Bool(pair.value) {
  78. self.generateServer = value
  79. } else {
  80. throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
  81. }
  82. case "Client":
  83. if let value = Bool(pair.value) {
  84. self.generateClient = value
  85. } else {
  86. throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
  87. }
  88. case "TestClient":
  89. if let value = Bool(pair.value) {
  90. self.generateTestClient = value
  91. } else {
  92. throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
  93. }
  94. case "KeepMethodCasing":
  95. if let value = Bool(pair.value) {
  96. self.keepMethodCasing = value
  97. } else {
  98. throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
  99. }
  100. case "ProtoPathModuleMappings":
  101. if !pair.value.isEmpty {
  102. do {
  103. self.protoToModuleMappings = try ProtoFileToModuleMappings(path: pair.value)
  104. } catch let e {
  105. throw GenerationError.wrappedError(
  106. message: "Parameter 'ProtoPathModuleMappings=\(pair.value)'",
  107. error: e
  108. )
  109. }
  110. }
  111. case "FileNaming":
  112. if let value = FileNaming(rawValue: pair.value) {
  113. self.fileNaming = value
  114. } else {
  115. throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
  116. }
  117. case "ExtraModuleImports":
  118. if !pair.value.isEmpty {
  119. self.extraModuleImports.append(pair.value)
  120. } else {
  121. throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
  122. }
  123. case "GRPCModuleName":
  124. if !pair.value.isEmpty {
  125. self.gRPCModuleName = pair.value
  126. } else {
  127. throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
  128. }
  129. case "SwiftProtobufModuleName":
  130. if !pair.value.isEmpty {
  131. self.swiftProtobufModuleName = pair.value
  132. } else {
  133. throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
  134. }
  135. case "ReflectionData":
  136. if let value = Bool(pair.value) {
  137. self.generateReflectionData = value
  138. } else {
  139. throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
  140. }
  141. #if compiler(>=6.0)
  142. case "_V2":
  143. if let value = Bool(pair.value) {
  144. self.v2 = value
  145. } else {
  146. throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
  147. }
  148. #endif
  149. case "UseAccessLevelOnImports":
  150. if let value = Bool(pair.value) {
  151. self.useAccessLevelOnImports = value
  152. } else {
  153. throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
  154. }
  155. default:
  156. throw GenerationError.unknownParameter(name: pair.key)
  157. }
  158. }
  159. }
  160. static func parseParameter(string: String?) -> [(key: String, value: String)] {
  161. guard let string = string, !string.isEmpty else {
  162. return []
  163. }
  164. let parts = string.components(separatedBy: ",")
  165. // Partitions the string into the section before the = and after the =
  166. let result = parts.map { string -> (key: String, value: String) in
  167. // Finds the equal sign and exits early if none
  168. guard let index = string.range(of: "=")?.lowerBound else {
  169. return (string, "")
  170. }
  171. // Creates key/value pair and trims whitespace
  172. let key = string[..<index]
  173. .trimmingCharacters(in: .whitespacesAndNewlines)
  174. let value = string[string.index(after: index)...]
  175. .trimmingCharacters(in: .whitespacesAndNewlines)
  176. return (key: key, value: value)
  177. }
  178. return result
  179. }
  180. }