|
| 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 | + |
| 17 | +import Foundation |
| 18 | +import PackagePlugin |
| 19 | + |
| 20 | +struct CommandConfig { |
| 21 | + var common: GenerationConfig |
| 22 | + |
| 23 | + var verbose: Bool |
| 24 | + var dryRun: Bool |
| 25 | + |
| 26 | + static let defaults = Self( |
| 27 | + common: .init( |
| 28 | + accessLevel: .internal, |
| 29 | + servers: true, |
| 30 | + clients: true, |
| 31 | + messages: true, |
| 32 | + fileNaming: .fullPath, |
| 33 | + accessLevelOnImports: false, |
| 34 | + importPaths: [], |
| 35 | + outputPath: "" |
| 36 | + ), |
| 37 | + verbose: false, |
| 38 | + dryRun: false |
| 39 | + ) |
| 40 | + |
| 41 | + static let parameterGroupSeparator = "--" |
| 42 | +} |
| 43 | + |
| 44 | +extension CommandConfig { |
| 45 | + static func parse( |
| 46 | + argumentExtractor argExtractor: inout ArgumentExtractor, |
| 47 | + pluginWorkDirectory: URL |
| 48 | + ) throws -> CommandConfig { |
| 49 | + var config = CommandConfig.defaults |
| 50 | + |
| 51 | + for flag in OptionsAndFlags.allCases { |
| 52 | + switch flag { |
| 53 | + case .accessLevel: |
| 54 | + if let value = argExtractor.extractSingleOption(named: flag.rawValue) { |
| 55 | + if let accessLevel = GenerationConfig.AccessLevel(rawValue: value) { |
| 56 | + config.common.accessLevel = accessLevel |
| 57 | + } else { |
| 58 | + throw CommandPluginError.unknownAccessLevel(value) |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + case .noServers: |
| 63 | + // Handled by `.servers` |
| 64 | + continue |
| 65 | + case .servers: |
| 66 | + let servers = argExtractor.extractFlag(named: OptionsAndFlags.servers.rawValue) |
| 67 | + let noServers = argExtractor.extractFlag(named: OptionsAndFlags.noServers.rawValue) |
| 68 | + if servers > 0 && noServers > 0 { |
| 69 | + throw CommandPluginError.conflictingFlags( |
| 70 | + OptionsAndFlags.servers.rawValue, |
| 71 | + OptionsAndFlags.noServers.rawValue |
| 72 | + ) |
| 73 | + } else if servers > 0 { |
| 74 | + config.common.servers = true |
| 75 | + } else if noServers > 0 { |
| 76 | + config.common.servers = false |
| 77 | + } |
| 78 | + |
| 79 | + case .noClients: |
| 80 | + // Handled by `.clients` |
| 81 | + continue |
| 82 | + case .clients: |
| 83 | + let clients = argExtractor.extractFlag(named: OptionsAndFlags.clients.rawValue) |
| 84 | + let noClients = argExtractor.extractFlag(named: OptionsAndFlags.noClients.rawValue) |
| 85 | + if clients > 0 && noClients > 0 { |
| 86 | + throw CommandPluginError.conflictingFlags( |
| 87 | + OptionsAndFlags.clients.rawValue, |
| 88 | + OptionsAndFlags.noClients.rawValue |
| 89 | + ) |
| 90 | + } else if clients > 0 { |
| 91 | + config.common.clients = true |
| 92 | + } else if noClients > 0 { |
| 93 | + config.common.clients = false |
| 94 | + } |
| 95 | + |
| 96 | + case .noMessages: |
| 97 | + // Handled by `.messages` |
| 98 | + continue |
| 99 | + case .messages: |
| 100 | + let messages = argExtractor.extractFlag(named: OptionsAndFlags.messages.rawValue) |
| 101 | + let noMessages = argExtractor.extractFlag(named: OptionsAndFlags.noMessages.rawValue) |
| 102 | + if messages > 0 && noMessages > 0 { |
| 103 | + throw CommandPluginError.conflictingFlags( |
| 104 | + OptionsAndFlags.messages.rawValue, |
| 105 | + OptionsAndFlags.noMessages.rawValue |
| 106 | + ) |
| 107 | + } else if messages > 0 { |
| 108 | + config.common.messages = true |
| 109 | + } else if noMessages > 0 { |
| 110 | + config.common.messages = false |
| 111 | + } |
| 112 | + |
| 113 | + case .fileNaming: |
| 114 | + if let value = argExtractor.extractSingleOption(named: flag.rawValue) { |
| 115 | + if let fileNaming = GenerationConfig.FileNaming(rawValue: value) { |
| 116 | + config.common.fileNaming = fileNaming |
| 117 | + } else { |
| 118 | + throw CommandPluginError.unknownFileNamingStrategy(value) |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + case .accessLevelOnImports: |
| 123 | + if argExtractor.extractFlag(named: flag.rawValue) > 0 { |
| 124 | + config.common.accessLevelOnImports = true |
| 125 | + } |
| 126 | + |
| 127 | + case .importPath: |
| 128 | + config.common.importPaths = argExtractor.extractOption(named: flag.rawValue) |
| 129 | + |
| 130 | + case .protocPath: |
| 131 | + config.common.protocPath = argExtractor.extractSingleOption(named: flag.rawValue) |
| 132 | + |
| 133 | + case .outputPath: |
| 134 | + config.common.outputPath = |
| 135 | + argExtractor.extractSingleOption(named: flag.rawValue) |
| 136 | + ?? pluginWorkDirectory.absoluteStringNoScheme |
| 137 | + |
| 138 | + case .verbose: |
| 139 | + let verbose = argExtractor.extractFlag(named: flag.rawValue) |
| 140 | + config.verbose = verbose != 0 |
| 141 | + |
| 142 | + case .dryRun: |
| 143 | + let dryRun = argExtractor.extractFlag(named: flag.rawValue) |
| 144 | + config.dryRun = dryRun != 0 |
| 145 | + |
| 146 | + case .help: |
| 147 | + () // handled elsewhere |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + if let argument = argExtractor.remainingArguments.first { |
| 152 | + throw CommandPluginError.unknownOption(argument) |
| 153 | + } |
| 154 | + |
| 155 | + return config |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +extension ArgumentExtractor { |
| 160 | + mutating func extractSingleOption(named optionName: String) -> String? { |
| 161 | + let values = self.extractOption(named: optionName) |
| 162 | + if values.count > 1 { |
| 163 | + Diagnostics.warning( |
| 164 | + "'--\(optionName)' was unexpectedly repeated, the first value will be used." |
| 165 | + ) |
| 166 | + } |
| 167 | + return values.first |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +/// All valid input options/flags |
| 172 | +enum OptionsAndFlags: String, CaseIterable { |
| 173 | + case servers |
| 174 | + case noServers = "no-servers" |
| 175 | + case clients |
| 176 | + case noClients = "no-clients" |
| 177 | + case messages |
| 178 | + case noMessages = "no-messages" |
| 179 | + case fileNaming = "file-naming" |
| 180 | + case accessLevel = "access-level" |
| 181 | + case accessLevelOnImports = "access-level-on-imports" |
| 182 | + case importPath = "import-path" |
| 183 | + case protocPath = "protoc-path" |
| 184 | + case outputPath = "output-path" |
| 185 | + case verbose |
| 186 | + case dryRun = "dry-run" |
| 187 | + |
| 188 | + case help |
| 189 | +} |
| 190 | + |
| 191 | +extension OptionsAndFlags { |
| 192 | + func usageDescription() -> String { |
| 193 | + switch self { |
| 194 | + case .servers: |
| 195 | + return "Generate server code. Generated by default." |
| 196 | + case .noServers: |
| 197 | + return "Do not generate server code. Generated by default." |
| 198 | + case .clients: |
| 199 | + return "Generate client code. Generated by default." |
| 200 | + case .noClients: |
| 201 | + return "Do not generate client code. Generated by default." |
| 202 | + case .messages: |
| 203 | + return "Generate message code. Generated by default." |
| 204 | + case .noMessages: |
| 205 | + return "Do not generate message code. Generated by default." |
| 206 | + case .fileNaming: |
| 207 | + return |
| 208 | + "The naming scheme for output files [fullPath/pathToUnderscores/dropPath]. Defaults to fullPath." |
| 209 | + case .accessLevel: |
| 210 | + return |
| 211 | + "The access level of the generated source [internal/public/package]. Defaults to internal." |
| 212 | + case .accessLevelOnImports: |
| 213 | + return "Whether imports should have explicit access levels. Defaults to false." |
| 214 | + case .importPath: |
| 215 | + return |
| 216 | + "The directory in which to search for imports. May be specified multiple times. If none are specified the current working directory is used." |
| 217 | + case .protocPath: |
| 218 | + return "The path to the protoc binary." |
| 219 | + case .dryRun: |
| 220 | + return "Print but do not execute the protoc commands." |
| 221 | + case .outputPath: |
| 222 | + return "The directory into which the generated source files are created." |
| 223 | + case .verbose: |
| 224 | + return "Emit verbose output." |
| 225 | + case .help: |
| 226 | + return "Print this help." |
| 227 | + } |
| 228 | + } |
| 229 | + |
| 230 | + static func printHelp(requested: Bool) { |
| 231 | + let printMessage: (String) -> Void |
| 232 | + if requested { |
| 233 | + printMessage = { message in print(message) } |
| 234 | + } else { |
| 235 | + printMessage = Stderr.print |
| 236 | + } |
| 237 | + |
| 238 | + printMessage( |
| 239 | + "Usage: swift package generate-grpc-code-from-protos [flags] [\(CommandConfig.parameterGroupSeparator)] [input files]" |
| 240 | + ) |
| 241 | + printMessage("") |
| 242 | + printMessage("Flags:") |
| 243 | + printMessage("") |
| 244 | + |
| 245 | + let spacing = 3 |
| 246 | + let maxLength = |
| 247 | + (OptionsAndFlags.allCases.map(\.rawValue).max(by: { $0.count < $1.count })?.count ?? 0) |
| 248 | + + spacing |
| 249 | + for flag in OptionsAndFlags.allCases { |
| 250 | + printMessage( |
| 251 | + " --\(flag.rawValue.padding(toLength: maxLength, withPad: " ", startingAt: 0))\(flag.usageDescription())" |
| 252 | + ) |
| 253 | + } |
| 254 | + } |
| 255 | +} |
0 commit comments