@@ -43,6 +43,67 @@ func stripMarkers(_ code:String) -> String {
4343 return outputLines. joined ( separator: " \n " )
4444}
4545
46+ // from apple/swift-protobuf/Sources/protoc-gen-swift/StringUtils.swift
47+ func splitPath( pathname: String ) -> ( dir: String , base: String , suffix: String ) {
48+ var dir = " "
49+ var base = " "
50+ var suffix = " "
51+ #if swift(>=3.2)
52+ let pathnameChars = pathname
53+ #else
54+ let pathnameChars = pathname. characters
55+ #endif
56+ for c in pathnameChars {
57+ if c == " / " {
58+ dir += base + suffix + String( c)
59+ base = " "
60+ suffix = " "
61+ } else if c == " . " {
62+ base += suffix
63+ suffix = String ( c)
64+ } else {
65+ suffix += String ( c)
66+ }
67+ }
68+ #if swift(>=3.2)
69+ let validSuffix = suffix. isEmpty || suffix. first == " . "
70+ #else
71+ let validSuffix = suffix. isEmpty || suffix. characters. first == " . "
72+ #endif
73+ if !validSuffix {
74+ base += suffix
75+ suffix = " "
76+ }
77+ return ( dir: dir, base: base, suffix: suffix)
78+ }
79+
80+ enum OutputNaming : String {
81+ case FullPath
82+ case PathToUnderscores
83+ case DropPath
84+ }
85+
86+ func outputFileName( component: String , index: Int , fileDescriptor: FileDescriptor ) -> String {
87+ var ext : String
88+ if index == 0 {
89+ ext = " . " + component + " .pb.swift "
90+ } else {
91+ ext = " \( index) . " + component + " .pb.swift "
92+ }
93+ let pathParts = splitPath ( pathname: fileDescriptor. name)
94+ let outputNamingOption = OutputNaming . FullPath // temporarily hard-coded
95+ switch outputNamingOption {
96+ case . FullPath:
97+ return pathParts. dir + pathParts. base + ext
98+ case . PathToUnderscores:
99+ let dirWithUnderscores =
100+ pathParts. dir. replacingOccurrences ( of: " / " , with: " _ " )
101+ return dirWithUnderscores + pathParts. base + ext
102+ case . DropPath:
103+ return pathParts. base + ext
104+ }
105+ }
106+
46107func main( ) throws {
47108
48109 // initialize template engine and add custom filters
@@ -64,6 +125,7 @@ func main() throws {
64125
65126 var generatedFileNames = Set < String > ( )
66127 var clientCount = 0
128+ var serverCount = 0
67129
68130 // process each .proto file separately
69131 for fileDescriptor in descriptorSet. files {
@@ -95,13 +157,7 @@ func main() throws {
95157 " access " : options. visibility. sourceSnippet]
96158
97159 do {
98- var clientFileName : String
99- if clientCount == 0 {
100- clientFileName = package + " .client.pb.swift "
101- } else {
102- clientFileName = package + " \( clientCount) .client.pb.swift "
103- }
104-
160+ let clientFileName = outputFileName ( component: " client " , index: clientCount, fileDescriptor: fileDescriptor)
105161 if !generatedFileNames. contains ( clientFileName) {
106162 generatedFileNames. insert ( clientFileName)
107163 clientCount += 1
@@ -113,9 +169,10 @@ func main() throws {
113169 response. file. append ( clientfile)
114170 }
115171
116- let serverFileName = package + " . server.pb.swift "
172+ let serverFileName = outputFileName ( component : " server " , index : serverCount , fileDescriptor : fileDescriptor )
117173 if !generatedFileNames. contains ( serverFileName) {
118174 generatedFileNames. insert ( serverFileName)
175+ serverCount += 1
119176 let servercode = try templateEnvironment. renderTemplate ( name: " server.pb.swift " ,
120177 context: context)
121178 var serverfile = Google_Protobuf_Compiler_CodeGeneratorResponse . File ( )
0 commit comments