plugin.swift 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 {
  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. /// Indicates that there was no configuration file at the required location.
  27. case noConfigFound
  28. }
  29. /// The configuration of the plugin.
  30. struct Configuration: Codable {
  31. /// Encapsulates a single invocation of protoc.
  32. struct Invocation: Codable {
  33. /// The visibility of the generated files.
  34. enum Visibility: String, Codable {
  35. /// The generated files should have `internal` access level.
  36. case `internal`
  37. /// The generated files should have `public` access level.
  38. case `public`
  39. }
  40. /// An array of paths to `.proto` files for this invocation.
  41. var protoFiles: [String]
  42. /// The visibility of the generated files.
  43. var visibility: Visibility?
  44. /// Whether server code is generated.
  45. var server: Bool?
  46. /// Whether client code is generated.
  47. var client: Bool?
  48. /// Determines whether the casing of generated function names is kept.
  49. var keepMethodCasing: Bool?
  50. }
  51. /// Specify the directory in which to search for
  52. /// imports. May be specified multiple times;
  53. /// directories will be searched in order.
  54. /// The target source directory is always appended
  55. /// to the import paths.
  56. var importPaths: [String]?
  57. /// The path to the `protoc` binary.
  58. ///
  59. /// If this is not set, SPM will try to find the tool itself.
  60. var protocPath: String?
  61. /// A list of invocations of `protoc` with the `GRPCSwiftPlugin`.
  62. var invocations: [Invocation]
  63. }
  64. static let configurationFileName = "grpc-swift-config.json"
  65. /// Create build commands for the given arguments
  66. /// - Parameters:
  67. /// - pluginWorkDirectory: The path of a writable directory into which the plugin or the build
  68. /// commands it constructs can write anything it wants.
  69. /// - sourceFiles: The input files that are associated with the target.
  70. /// - tool: The tool method from the context.
  71. /// - Returns: The build commands configured based on the arguments.
  72. func createBuildCommands(
  73. pluginWorkDirectory: PackagePlugin.Path,
  74. sourceFiles: FileList,
  75. tool: (String) throws -> PackagePlugin.PluginContext.Tool
  76. ) throws -> [Command] {
  77. guard let configurationFilePath = sourceFiles.first(
  78. where: {
  79. $0.path.lastComponent == Self.configurationFileName
  80. }
  81. )?.path else {
  82. throw PluginError.noConfigFound
  83. }
  84. let data = try Data(contentsOf: URL(fileURLWithPath: "\(configurationFilePath)"))
  85. let configuration = try JSONDecoder().decode(Configuration.self, from: data)
  86. try self.validateConfiguration(configuration)
  87. let targetDirectory = configurationFilePath.removingLastComponent()
  88. var importPaths: [Path] = [targetDirectory]
  89. if let configuredImportPaths = configuration.importPaths {
  90. importPaths.append(contentsOf: configuredImportPaths.map { Path($0) })
  91. }
  92. // We need to find the path of protoc and protoc-gen-grpc-swift
  93. let protocPath: Path
  94. if let configuredProtocPath = configuration.protocPath {
  95. protocPath = Path(configuredProtocPath)
  96. } else if let environmentPath = ProcessInfo.processInfo.environment["PROTOC_PATH"] {
  97. // The user set the env variable, so let's take that
  98. protocPath = Path(environmentPath)
  99. } else {
  100. // The user didn't set anything so let's try see if SPM can find a binary for us
  101. protocPath = try tool("protoc").path
  102. }
  103. let protocGenGRPCSwiftPath = try tool("protoc-gen-grpc-swift").path
  104. return configuration.invocations.map { invocation in
  105. self.invokeProtoc(
  106. directory: targetDirectory,
  107. invocation: invocation,
  108. protocPath: protocPath,
  109. protocGenGRPCSwiftPath: protocGenGRPCSwiftPath,
  110. outputDirectory: pluginWorkDirectory,
  111. importPaths: importPaths
  112. )
  113. }
  114. }
  115. /// Invokes `protoc` with the given inputs
  116. ///
  117. /// - Parameters:
  118. /// - directory: The plugin's target directory.
  119. /// - invocation: The `protoc` invocation.
  120. /// - protocPath: The path to the `protoc` binary.
  121. /// - protocGenSwiftPath: The path to the `protoc-gen-swift` binary.
  122. /// - outputDirectory: The output directory for the generated files.
  123. /// - importPaths: List of paths to pass with "-I <path>" to `protoc`
  124. /// - Returns: The build command configured based on the arguments
  125. private func invokeProtoc(
  126. directory: Path,
  127. invocation: Configuration.Invocation,
  128. protocPath: Path,
  129. protocGenGRPCSwiftPath: Path,
  130. outputDirectory: Path,
  131. importPaths: [Path]
  132. ) -> Command {
  133. // Construct the `protoc` arguments.
  134. var protocArgs = [
  135. "--plugin=protoc-gen-grpc-swift=\(protocGenGRPCSwiftPath)",
  136. "--grpc-swift_out=\(outputDirectory)",
  137. ]
  138. importPaths.forEach { path in
  139. protocArgs.append("-I")
  140. protocArgs.append("\(path)")
  141. }
  142. if let visibility = invocation.visibility {
  143. protocArgs.append("--grpc-swift_opt=Visibility=\(visibility.rawValue.capitalized)")
  144. }
  145. if let generateServerCode = invocation.server {
  146. protocArgs.append("--grpc-swift_opt=Server=\(generateServerCode)")
  147. }
  148. if let generateClientCode = invocation.client {
  149. protocArgs.append("--grpc-swift_opt=Client=\(generateClientCode)")
  150. }
  151. if let keepMethodCasingOption = invocation.keepMethodCasing {
  152. protocArgs.append("--grpc-swift_opt=KeepMethodCasing=\(keepMethodCasingOption)")
  153. }
  154. var inputFiles = [Path]()
  155. var outputFiles = [Path]()
  156. for var file in invocation.protoFiles {
  157. // Append the file to the protoc args so that it is used for generating
  158. protocArgs.append("\(file)")
  159. inputFiles.append(directory.appending(file))
  160. // The name of the output file is based on the name of the input file.
  161. // We validated in the beginning that every file has the suffix of .proto
  162. // This means we can just drop the last 5 elements and append the new suffix
  163. file.removeLast(5)
  164. file.append("grpc.swift")
  165. let protobufOutputPath = outputDirectory.appending(file)
  166. // Add the outputPath as an output file
  167. outputFiles.append(protobufOutputPath)
  168. }
  169. // Construct the command. Specifying the input and output paths lets the build
  170. // system know when to invoke the command. The output paths are passed on to
  171. // the rule engine in the build system.
  172. return Command.buildCommand(
  173. displayName: "Generating gRPC Swift files from proto files",
  174. executable: protocPath,
  175. arguments: protocArgs,
  176. inputFiles: inputFiles + [protocGenGRPCSwiftPath],
  177. outputFiles: outputFiles
  178. )
  179. }
  180. /// Validates the configuration file for various user errors.
  181. private func validateConfiguration(_ configuration: Configuration) throws {
  182. for invocation in configuration.invocations {
  183. for protoFile in invocation.protoFiles {
  184. if !protoFile.hasSuffix(".proto") {
  185. throw PluginError.invalidInputFileExtension
  186. }
  187. }
  188. }
  189. }
  190. }
  191. extension GRPCSwiftPlugin: BuildToolPlugin {
  192. func createBuildCommands(
  193. context: PluginContext,
  194. target: Target
  195. ) async throws -> [Command] {
  196. guard let swiftTarget = target as? SwiftSourceModuleTarget else {
  197. throw PluginError.invalidTarget
  198. }
  199. return try self.createBuildCommands(
  200. pluginWorkDirectory: context.pluginWorkDirectory,
  201. sourceFiles: swiftTarget.sourceFiles,
  202. tool: context.tool
  203. )
  204. }
  205. }
  206. #if canImport(XcodeProjectPlugin)
  207. import XcodeProjectPlugin
  208. extension GRPCSwiftPlugin: XcodeBuildToolPlugin {
  209. func createBuildCommands(
  210. context: XcodePluginContext,
  211. target: XcodeTarget
  212. ) throws -> [Command] {
  213. return try self.createBuildCommands(
  214. pluginWorkDirectory: context.pluginWorkDirectory,
  215. sourceFiles: target.inputFiles,
  216. tool: context.tool
  217. )
  218. }
  219. }
  220. #endif