@@ -189,7 +189,7 @@ func protocGenGRPCSwiftCommand(
189
189
let outputPathURL = URL ( fileURLWithPath: config. outputPath)
190
190
191
191
let outputFilePath = deriveOutputFilePath (
192
- for : inputFile,
192
+ protoFile : inputFile,
193
193
baseDirectoryPath: baseDirectoryPath,
194
194
outputDirectory: outputPathURL,
195
195
outputExtension: " grpc.swift "
@@ -239,7 +239,7 @@ func protocGenSwiftCommand(
239
239
let outputPathURL = URL ( fileURLWithPath: config. outputPath)
240
240
241
241
let outputFilePath = deriveOutputFilePath (
242
- for : inputFile,
242
+ protoFile : inputFile,
243
243
baseDirectoryPath: baseDirectoryPath,
244
244
outputDirectory: outputPathURL,
245
245
outputExtension: " pb.swift "
@@ -267,28 +267,32 @@ func protocGenSwiftCommand(
267
267
)
268
268
}
269
269
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
+ ///
272
276
/// - 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.
275
280
/// - outputDirectory: The directory in which generated source files are created.
276
281
/// - outputExtension: The file extension to be appended to generated files in-place of `.proto`.
277
282
/// - Returns: The expected output file path.
278
283
func deriveOutputFilePath(
279
- for inputFile : URL ,
284
+ protoFile : URL ,
280
285
baseDirectoryPath: URL ,
281
286
outputDirectory: URL ,
282
287
outputExtension: String
283
288
) -> 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)
289
293
290
294
// find the inputFile path relative to the proto directory
291
- var relativePathComponents = inputFile . deletingLastPathComponent ( ) . pathComponents
295
+ var relativePathComponents = protoFile . deletingLastPathComponent ( ) . pathComponents
292
296
for protoDirectoryPathComponent in baseDirectoryPath. pathComponents {
293
297
if relativePathComponents. first == protoDirectoryPathComponent {
294
298
relativePathComponents. removeFirst ( )
@@ -297,10 +301,7 @@ func deriveOutputFilePath(
297
301
}
298
302
}
299
303
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)
306
307
}
0 commit comments