Skip to content

Commit 84627d2

Browse files
committed
review comments
1 parent a5010cb commit 84627d2

File tree

9 files changed

+280
-291
lines changed

9 files changed

+280
-291
lines changed

Package.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ let products: [Product] = [
2727
targets: ["protoc-gen-grpc-swift"]
2828
),
2929
.plugin(
30-
name: "GRPCGeneratorPlugin",
31-
targets: ["GRPCGeneratorPlugin"]
30+
name: "GRPCProtobufGenerator",
31+
targets: ["GRPCProtobufGenerator"]
3232
),
3333
]
3434

@@ -108,10 +108,10 @@ let targets: [Target] = [
108108

109109
// Code generator build plugin
110110
.plugin(
111-
name: "GRPCGeneratorPlugin",
111+
name: "GRPCProtobufGenerator",
112112
capability: .buildTool(),
113113
dependencies: [
114-
"protoc-gen-grpc-swift",
114+
.target(name: "protoc-gen-grpc-swift"),
115115
.product(name: "protoc-gen-swift", package: "swift-protobuf"),
116116
]
117117
),

Plugins/GRPCGeneratorPlugin/ConfigurationFile.swift

Lines changed: 0 additions & 79 deletions
This file was deleted.
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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 = try container.decodeIfPresent(GenerationConfig.Visibility.self, forKey: .visibility) ?? defaultVisibility
80+
self.server = try container.decodeIfPresent(Bool.self, forKey: .server) ?? defaultServer
81+
self.client = try container.decodeIfPresent(Bool.self, forKey: .client) ?? defaultClient
82+
self.message = try container.decodeIfPresent(Bool.self, forKey: .message) ?? defaultMessage
83+
self.useAccessLevelOnImports = try container.decodeIfPresent(Bool.self, forKey: .useAccessLevelOnImports) ?? defaultUseAccessLevelOnImports
84+
self.importPaths = try container.decodeIfPresent([String].self, forKey: .importPaths) ?? defaultImportPaths
85+
self.protocPath = try container.decodeIfPresent(String.self, forKey: .protocPath)
86+
}
87+
}
88+
89+
extension GenerationConfig {
90+
init(configurationFile: BuildPluginConfig, configurationFilePath: URL, outputPath: URL) {
91+
self.visibility = configurationFile.visibility
92+
self.server = configurationFile.server
93+
self.client = configurationFile.client
94+
self.message = configurationFile.message
95+
// hard-code full-path to avoid collisions since this goes into a temporary directory anyway
96+
self.fileNaming = .fullPath
97+
self.useAccessLevelOnImports = configurationFile.useAccessLevelOnImports
98+
self.importPaths = []
99+
100+
// Generate absolute paths for the imports relative to the config file in which they are specified
101+
self.importPaths = configurationFile.importPaths.map { relativePath in
102+
configurationFilePath.deletingLastPathComponent().relativePath + "/" + relativePath
103+
}
104+
self.protocPath = configurationFile.protocPath
105+
self.outputPath = outputPath.relativePath
106+
}
107+
}
108+
109+
extension GenerationConfig.Visibility: Codable {
110+
init?(rawValue: String) {
111+
switch rawValue.lowercased() {
112+
case "internal":
113+
self = .internal
114+
case "public":
115+
self = .public
116+
case "package":
117+
self = .package
118+
default:
119+
return nil
120+
}
121+
}
122+
}

0 commit comments

Comments
 (0)