CodeGenerator.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright 2024, 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. @available(*, deprecated, renamed: "CodeGenerator")
  17. public typealias SourceGenerator = CodeGenerator
  18. /// Generates ``SourceFile`` objects containing generated code for the RPCs represented
  19. /// in a ``CodeGenerationRequest`` object.
  20. public struct CodeGenerator: Sendable {
  21. /// The options regarding the access level, indentation for the generated code
  22. /// and whether to generate server and client code.
  23. public var config: Config
  24. public init(config: Config) {
  25. self.config = config
  26. }
  27. /// User options for the CodeGeneration.
  28. public struct Config: Sendable {
  29. /// The access level the generated code will have.
  30. public var accessLevel: AccessLevel
  31. /// Whether imports have explicit access levels.
  32. public var accessLevelOnImports: Bool
  33. /// The indentation of the generated code as the number of spaces.
  34. public var indentation: Int
  35. /// Whether or not client code should be generated.
  36. public var client: Bool
  37. /// Whether or not server code should be generated.
  38. public var server: Bool
  39. /// The name of the core gRPC module.
  40. public var grpcCoreModuleName: String
  41. /// The availability annotations to use on the generated code.
  42. public var availability: AvailabilityAnnotations = .default
  43. /// Creates a new configuration.
  44. ///
  45. /// - Parameters:
  46. /// - accessLevel: The access level the generated code will have.
  47. /// - accessLevelOnImports: Whether imports have explicit access levels.
  48. /// - client: Whether or not client code should be generated.
  49. /// - server: Whether or not server code should be generated.
  50. /// - indentation: The indentation of the generated code as the number of spaces.
  51. public init(
  52. accessLevel: AccessLevel,
  53. accessLevelOnImports: Bool,
  54. client: Bool,
  55. server: Bool,
  56. indentation: Int = 4
  57. ) {
  58. self.accessLevel = accessLevel
  59. self.accessLevelOnImports = accessLevelOnImports
  60. self.indentation = indentation
  61. self.client = client
  62. self.server = server
  63. self.grpcCoreModuleName = "GRPCCore"
  64. }
  65. /// The possible access levels for the generated code.
  66. public struct AccessLevel: Sendable, Hashable {
  67. package var level: Level
  68. package enum Level {
  69. case `internal`
  70. case `public`
  71. case `package`
  72. }
  73. /// The generated code will have `internal` access level.
  74. public static var `internal`: Self { Self(level: .`internal`) }
  75. /// The generated code will have `public` access level.
  76. public static var `public`: Self { Self(level: .`public`) }
  77. /// The generated code will have `package` access level.
  78. public static var `package`: Self { Self(level: .`package`) }
  79. }
  80. // The availability that generated code is annotated with.
  81. public struct AvailabilityAnnotations: Sendable, Hashable {
  82. public struct Platform: Sendable, Hashable {
  83. /// The name of the OS, e.g. 'macOS'.
  84. public var os: String
  85. /// The version of the OS, e.g. '15.0'.
  86. public var version: String
  87. public init(os: String, version: String) {
  88. self.os = os
  89. self.version = version
  90. }
  91. }
  92. fileprivate enum Wrapped: Sendable, Hashable {
  93. case macOS15Aligned
  94. case custom([Platform])
  95. }
  96. fileprivate var wrapped: Wrapped
  97. private init(_ wrapped: Wrapped) {
  98. self.wrapped = wrapped
  99. }
  100. /// Use the default availability.
  101. ///
  102. /// The default platform availability is:
  103. /// - macOS 15.0
  104. /// - iOS 18.0
  105. /// - tvOS 18.0
  106. /// - watchOS 11.0
  107. /// - visionOS 2.0
  108. public static var `default`: Self {
  109. Self(.macOS15Aligned)
  110. }
  111. /// Use a custom set of availability attributes.
  112. /// - Parameter platforms: Availability requirements.
  113. public static func custom(_ platforms: [Platform]) -> Self {
  114. Self(.custom(platforms))
  115. }
  116. }
  117. }
  118. /// Transforms a ``CodeGenerationRequest`` object into a ``SourceFile`` object containing
  119. /// the generated code.
  120. public func generate(
  121. _ request: CodeGenerationRequest
  122. ) throws -> SourceFile {
  123. let translator = IDLToStructuredSwiftTranslator()
  124. let textRenderer = TextBasedRenderer(indentation: self.config.indentation)
  125. let structuredSwiftRepresentation = try translator.translate(
  126. codeGenerationRequest: request,
  127. accessLevel: self.config.accessLevel,
  128. accessLevelOnImports: self.config.accessLevelOnImports,
  129. client: self.config.client,
  130. server: self.config.server,
  131. grpcCoreModuleName: self.config.grpcCoreModuleName,
  132. availability: AvailabilityDescription(self.config.availability)
  133. )
  134. let sourceFile = try textRenderer.render(structured: structuredSwiftRepresentation)
  135. return sourceFile
  136. }
  137. }
  138. extension AvailabilityDescription {
  139. init(_ availability: CodeGenerator.Config.AvailabilityAnnotations) throws {
  140. switch availability.wrapped {
  141. case .macOS15Aligned:
  142. self = .macOS15Aligned
  143. case .custom(let platforms):
  144. self.osVersions = platforms.map {
  145. .init(os: .init(name: $0.os), version: $0.version)
  146. }
  147. }
  148. }
  149. }