plugin.swift 10 KB

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