@@ -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.
278283func 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}
0 commit comments