plugin.swift 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright 2022, 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 PackagePlugin
  18. @main
  19. struct GRPCSwiftPlugin: BuildToolPlugin {
  20. /// Errors thrown by the `GRPCSwiftPlugin`
  21. enum PluginError: Error {
  22. /// Indicates that the target where the plugin was applied to was not `SourceModuleTarget`.
  23. case invalidTarget
  24. /// Indicates that the file extension of an input file was not `.proto`.
  25. case invalidInputFileExtension
  26. }
  27. /// The configuration of the plugin.
  28. struct Configuration: Codable {
  29. /// Encapsulates a single invocation of protoc.
  30. struct Invocation: Codable {
  31. /// The visibility of the generated files.
  32. enum Visibility: String, Codable {
  33. /// The generated files should have `internal` access level.
  34. case `internal`
  35. /// The generated files should have `public` access level.
  36. case `public`
  37. }
  38. /// An array of paths to `.proto` files for this invocation.
  39. var protoFiles: [String]
  40. /// The visibility of the generated files.
  41. var visibility: Visibility?
  42. /// Whether server code is generated.
  43. var server: Bool?
  44. /// Whether client code is generated.
  45. var client: Bool?
  46. /// Determines whether the casing of generated function names is kept.
  47. var keepMethodCasing: Bool?
  48. }
  49. /// Specify the directory in which to search for
  50. /// imports. May be specified multiple times;
  51. /// directories will be searched in order.
  52. /// The target source directory is always appended
  53. /// to the import paths.
  54. var importPaths: [String]?
  55. /// The path to the `protoc` binary.
  56. ///
  57. /// If this is not set, SPM will try to find the tool itself.
  58. var protocPath: String?
  59. /// A list of invocations of `protoc` with the `GRPCSwiftPlugin`.
  60. var invocations: [Invocation]
  61. }
  62. static let configurationFileName = "grpc-swift-config.json"
  63. func createBuildCommands(context: PluginContext, target: Target) throws -> [Command] {
  64. // Let's check that this is a source target
  65. guard let target = target as? SourceModuleTarget else {
  66. throw PluginError.invalidTarget
  67. }
  68. // We need to find the configuration file at the root of the target
  69. let configurationFilePath = target.directory.appending(subpath: Self.configurationFileName)
  70. let data = try Data(contentsOf: URL(fileURLWithPath: "\(configurationFilePath)"))
  71. let configuration = try JSONDecoder().decode(Configuration.self, from: data)
  72. try self.validateConfiguration(configuration)
  73. var importPaths: [Path] = [target.directory]
  74. if let configuredImportPaths = configuration.importPaths {
  75. importPaths.append(contentsOf: configuredImportPaths.map { Path($0) })
  76. }
  77. // We need to find the path of protoc and protoc-gen-grpc-swift
  78. let protocPath: Path
  79. if let configuredProtocPath = configuration.protocPath {
  80. protocPath = Path(configuredProtocPath)
  81. } else if let environmentPath = ProcessInfo.processInfo.environment["PROTOC_PATH"] {
  82. // The user set the env variable, so let's take that
  83. protocPath = Path(environmentPath)
  84. } else {
  85. // The user didn't set anything so let's try see if SPM can find a binary for us
  86. protocPath = try context.tool(named: "protoc").path
  87. }
  88. let protocGenGRPCSwiftPath = try context.tool(named: "protoc-gen-grpc-swift").path
  89. // This plugin generates its output into GeneratedSources
  90. let outputDirectory = context.pluginWorkDirectory
  91. return configuration.invocations.map { invocation in
  92. self.invokeProtoc(
  93. target: target,
  94. invocation: invocation,
  95. protocPath: protocPath,
  96. protocGenGRPCSwiftPath: protocGenGRPCSwiftPath,
  97. outputDirectory: outputDirectory,
  98. importPaths: importPaths
  99. )
  100. }
  101. }
  102. /// Invokes `protoc` with the given inputs
  103. ///
  104. /// - Parameters:
  105. /// - target: The plugin's target.
  106. /// - invocation: The `protoc` invocation.
  107. /// - protocPath: The path to the `protoc` binary.
  108. /// - protocGenSwiftPath: The path to the `protoc-gen-swift` binary.
  109. /// - outputDirectory: The output directory for the generated files.
  110. /// - importPaths: List of paths to pass with "-I <path>" to `protoc`
  111. /// - Returns: The build command.
  112. private func invokeProtoc(
  113. target: Target,
  114. invocation: Configuration.Invocation,
  115. protocPath: Path,
  116. protocGenGRPCSwiftPath: Path,
  117. outputDirectory: Path,
  118. importPaths: [Path]
  119. ) -> Command {
  120. // Construct the `protoc` arguments.
  121. var protocArgs = [
  122. "--plugin=protoc-gen-grpc-swift=\(protocGenGRPCSwiftPath)",
  123. "--grpc-swift_out=\(outputDirectory)",
  124. ]
  125. importPaths.forEach { path in
  126. protocArgs.append("-I")
  127. protocArgs.append("\(path)")
  128. }
  129. if let visibility = invocation.visibility {
  130. protocArgs.append("--grpc-swift_opt=Visibility=\(visibility.rawValue.capitalized)")
  131. }
  132. if let generateServerCode = invocation.server {
  133. protocArgs.append("--grpc-swift_opt=Server=\(generateServerCode)")
  134. }
  135. if let generateClientCode = invocation.client {
  136. protocArgs.append("--grpc-swift_opt=Client=\(generateClientCode)")
  137. }
  138. if let keepMethodCasingOption = invocation.keepMethodCasing {
  139. protocArgs.append("--grpc-swift_opt=KeepMethodCasing=\(keepMethodCasingOption)")
  140. }
  141. var inputFiles = [Path]()
  142. var outputFiles = [Path]()
  143. for var file in invocation.protoFiles {
  144. // Append the file to the protoc args so that it is used for generating
  145. protocArgs.append("\(file)")
  146. inputFiles.append(target.directory.appending(file))
  147. // The name of the output file is based on the name of the input file.
  148. // We validated in the beginning that every file has the suffix of .proto
  149. // This means we can just drop the last 5 elements and append the new suffix
  150. file.removeLast(5)
  151. file.append("grpc.swift")
  152. let protobufOutputPath = outputDirectory.appending(file)
  153. // Add the outputPath as an output file
  154. outputFiles.append(protobufOutputPath)
  155. }
  156. // Construct the command. Specifying the input and output paths lets the build
  157. // system know when to invoke the command. The output paths are passed on to
  158. // the rule engine in the build system.
  159. return Command.buildCommand(
  160. displayName: "Generating gRPC Swift files from proto files",
  161. executable: protocPath,
  162. arguments: protocArgs,
  163. inputFiles: inputFiles + [protocGenGRPCSwiftPath],
  164. outputFiles: outputFiles
  165. )
  166. }
  167. /// Validates the configuration file for various user errors.
  168. private func validateConfiguration(_ configuration: Configuration) throws {
  169. for invocation in configuration.invocations {
  170. for protoFile in invocation.protoFiles {
  171. if !protoFile.hasSuffix(".proto") {
  172. throw PluginError.invalidInputFileExtension
  173. }
  174. }
  175. }
  176. }
  177. }