Skip to content

Commit 64f97e9

Browse files
authored
Use path-to-underscore when generating files with the plugin (#47)
Motivation: The build system can't compile the code generated by the plugin when different proto files with the same name are used. This is allowed and expected if the files are in different proto packages. Modifications: - Use the "path to underscore" naming scheme instead of the "full path" naming scheme. This changes path components to underscores, so "foo/bar/baz.proto" would have "foo_bar_baz.grpc.swift" generated for it. - Uncomment test. Result: Test passes.
1 parent e426ad8 commit 64f97e9

File tree

3 files changed

+25
-23
lines changed

3 files changed

+25
-23
lines changed

Plugins/GRPCProtobufGenerator/BuildPluginConfig.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,10 @@ extension GenerationConfig {
196196
self.server = buildPluginConfig.generate.servers
197197
self.client = buildPluginConfig.generate.clients
198198
self.message = buildPluginConfig.generate.messages
199-
// hard-code full-path to avoid collisions since this goes into a temporary directory anyway
200-
self.fileNaming = .fullPath
199+
// Use path to underscores as it ensures output files are unique (files generated from
200+
// "foo/bar.proto" won't collide with those generated from "bar/bar.proto" as they'll be
201+
// uniquely named "foo_bar.(grpc|pb).swift" and "bar_bar.(grpc|pb).swift".
202+
self.fileNaming = .pathToUnderscores
201203
self.visibility = buildPluginConfig.generatedSource.accessLevel
202204
self.accessLevelOnImports = buildPluginConfig.generatedSource.accessLevelOnImports
203205
// Generate absolute paths for the imports relative to the config file in which they are specified

Plugins/GRPCProtobufGenerator/Plugin.swift

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ func protocGenGRPCSwiftCommand(
189189
let outputPathURL = URL(fileURLWithPath: config.outputPath)
190190

191191
let outputFilePath = deriveOutputFilePath(
192-
for: inputFile,
192+
protoFile: inputFile,
193193
baseDirectoryPath: baseDirectoryPath,
194194
outputDirectory: outputPathURL,
195195
outputExtension: "grpc.swift"
@@ -239,7 +239,7 @@ func protocGenSwiftCommand(
239239
let outputPathURL = URL(fileURLWithPath: config.outputPath)
240240

241241
let outputFilePath = deriveOutputFilePath(
242-
for: inputFile,
242+
protoFile: inputFile,
243243
baseDirectoryPath: baseDirectoryPath,
244244
outputDirectory: outputPathURL,
245245
outputExtension: "pb.swift"
@@ -267,28 +267,32 @@ func protocGenSwiftCommand(
267267
)
268268
}
269269

270-
/// Derive the expected output file path to match the behavior of the `protoc-gen-swift` and `protoc-gen-grpc-swift` `protoc` plugins
271-
/// when using the `FullPath` naming scheme.
270+
/// Derive the expected output file path to match the behavior of the `protoc-gen-swift`
271+
/// and `protoc-gen-grpc-swift` `protoc` plugins using the `PathToUnderscores` naming scheme.
272+
///
273+
/// This means the generated file for an input proto file called "foo/bar/baz.proto" will
274+
/// have the name "foo\_bar\_baz.proto".
275+
///
272276
/// - Parameters:
273-
/// - inputFile: The input `.proto` file.
274-
/// - baseDirectoryPath: The root path to the source `.proto` files used as the reference for relative path naming schemes.
277+
/// - protoFile: The path of the input `.proto` file.
278+
/// - baseDirectoryPath: The root path to the source `.proto` files used as the reference for
279+
/// relative path naming schemes.
275280
/// - outputDirectory: The directory in which generated source files are created.
276281
/// - outputExtension: The file extension to be appended to generated files in-place of `.proto`.
277282
/// - Returns: The expected output file path.
278283
func deriveOutputFilePath(
279-
for inputFile: URL,
284+
protoFile: URL,
280285
baseDirectoryPath: URL,
281286
outputDirectory: URL,
282287
outputExtension: String
283288
) -> URL {
284-
// The name of the output file is based on the name of the input file.
285-
// We validated in the beginning that every file has the suffix of .proto
286-
// This means we can just drop the last 5 elements and append the new suffix
287-
let lastPathComponentRoot = inputFile.lastPathComponent.dropLast(5)
288-
let lastPathComponent = String(lastPathComponentRoot + outputExtension)
289+
// Replace the extension (".proto") with the new extension (".grpc.swift"
290+
// or ".pb.swift").
291+
precondition(protoFile.pathExtension == "proto")
292+
let fileName = String(protoFile.lastPathComponent.dropLast(5) + outputExtension)
289293

290294
// find the inputFile path relative to the proto directory
291-
var relativePathComponents = inputFile.deletingLastPathComponent().pathComponents
295+
var relativePathComponents = protoFile.deletingLastPathComponent().pathComponents
292296
for protoDirectoryPathComponent in baseDirectoryPath.pathComponents {
293297
if relativePathComponents.first == protoDirectoryPathComponent {
294298
relativePathComponents.removeFirst()
@@ -297,10 +301,7 @@ func deriveOutputFilePath(
297301
}
298302
}
299303

300-
let outputFileComponents = relativePathComponents + [lastPathComponent]
301-
var outputFilePath = outputDirectory
302-
for outputFileComponent in outputFileComponents {
303-
outputFilePath.append(component: outputFileComponent)
304-
}
305-
return outputFilePath
304+
relativePathComponents.append(fileName)
305+
let path = relativePathComponents.joined(separator: "_")
306+
return outputDirectory.appending(path: path)
306307
}

dev/setup-plugin-tests.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,5 +205,4 @@ test_03_separate_service_message_protos
205205
test_04_cross_directory_imports
206206
test_05_two_definitions
207207
test_06_nested_definitions
208-
# Expected to fail:
209-
# test_07_duplicated_proto_file_name
208+
test_07_duplicated_proto_file_name

0 commit comments

Comments
 (0)