|
| 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 | + |
| 19 | +let configFileName = "grpc-swift-proto-generator-config.json" |
| 20 | + |
| 21 | +/// The configuration of the build plugin. |
| 22 | +struct BuildPluginConfig: Codable { |
| 23 | + /// The visibility of the generated files. |
| 24 | + /// |
| 25 | + /// Defaults to `Internal`. |
| 26 | + var visibility: GenerationConfig.Visibility |
| 27 | + /// Whether server code is generated. |
| 28 | + /// |
| 29 | + /// Defaults to `true`. |
| 30 | + var server: Bool |
| 31 | + /// Whether client code is generated. |
| 32 | + /// |
| 33 | + /// Defaults to `true`. |
| 34 | + var client: Bool |
| 35 | + /// Whether message code is generated. |
| 36 | + /// |
| 37 | + /// Defaults to `true`. |
| 38 | + var message: Bool |
| 39 | + /// Whether imports should have explicit access levels. |
| 40 | + /// |
| 41 | + /// Defaults to `false`. |
| 42 | + var useAccessLevelOnImports: Bool |
| 43 | + |
| 44 | + /// Specify the directory in which to search for imports. |
| 45 | + /// |
| 46 | + /// Paths are relative to the location of the specifying config file. |
| 47 | + /// Build plugins only have access to files within the target's source directory. |
| 48 | + /// May be specified multiple times; directories will be searched in order. |
| 49 | + /// The target source directory is always appended |
| 50 | + /// to the import paths. |
| 51 | + var importPaths: [String] |
| 52 | + |
| 53 | + /// The path to the `protoc` binary. |
| 54 | + /// |
| 55 | + /// If this is not set, Swift Package Manager will try to find the tool itself. |
| 56 | + var protocPath: String? |
| 57 | + |
| 58 | + // Codable conformance with defaults |
| 59 | + enum CodingKeys: String, CodingKey { |
| 60 | + case visibility |
| 61 | + case server |
| 62 | + case client |
| 63 | + case message |
| 64 | + case useAccessLevelOnImports |
| 65 | + case importPaths |
| 66 | + case protocPath |
| 67 | + } |
| 68 | + |
| 69 | + let defaultVisibility: GenerationConfig.Visibility = .internal |
| 70 | + let defaultServer = true |
| 71 | + let defaultClient = true |
| 72 | + let defaultMessage = true |
| 73 | + let defaultUseAccessLevelOnImports = false |
| 74 | + let defaultImportPaths: [String] = [] |
| 75 | + |
| 76 | + init(from decoder: any Decoder) throws { |
| 77 | + let container = try decoder.container(keyedBy: CodingKeys.self) |
| 78 | + |
| 79 | + self.visibility = |
| 80 | + try container.decodeIfPresent(GenerationConfig.Visibility.self, forKey: .visibility) |
| 81 | + ?? defaultVisibility |
| 82 | + self.server = try container.decodeIfPresent(Bool.self, forKey: .server) ?? defaultServer |
| 83 | + self.client = try container.decodeIfPresent(Bool.self, forKey: .client) ?? defaultClient |
| 84 | + self.message = try container.decodeIfPresent(Bool.self, forKey: .message) ?? defaultMessage |
| 85 | + self.useAccessLevelOnImports = |
| 86 | + try container.decodeIfPresent(Bool.self, forKey: .useAccessLevelOnImports) |
| 87 | + ?? defaultUseAccessLevelOnImports |
| 88 | + self.importPaths = |
| 89 | + try container.decodeIfPresent([String].self, forKey: .importPaths) ?? defaultImportPaths |
| 90 | + self.protocPath = try container.decodeIfPresent(String.self, forKey: .protocPath) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +extension GenerationConfig { |
| 95 | + init(configurationFile: BuildPluginConfig, configurationFilePath: URL, outputPath: URL) { |
| 96 | + self.visibility = configurationFile.visibility |
| 97 | + self.server = configurationFile.server |
| 98 | + self.client = configurationFile.client |
| 99 | + self.message = configurationFile.message |
| 100 | + // hard-code full-path to avoid collisions since this goes into a temporary directory anyway |
| 101 | + self.fileNaming = .fullPath |
| 102 | + self.useAccessLevelOnImports = configurationFile.useAccessLevelOnImports |
| 103 | + self.importPaths = [] |
| 104 | + |
| 105 | + // Generate absolute paths for the imports relative to the config file in which they are specified |
| 106 | + self.importPaths = configurationFile.importPaths.map { relativePath in |
| 107 | + configurationFilePath.deletingLastPathComponent().relativePath + "/" + relativePath |
| 108 | + } |
| 109 | + self.protocPath = configurationFile.protocPath |
| 110 | + self.outputPath = outputPath.relativePath |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +extension GenerationConfig.Visibility: Codable { |
| 115 | + init?(rawValue: String) { |
| 116 | + switch rawValue.lowercased() { |
| 117 | + case "internal": |
| 118 | + self = .internal |
| 119 | + case "public": |
| 120 | + self = .public |
| 121 | + case "package": |
| 122 | + self = .package |
| 123 | + default: |
| 124 | + return nil |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments