@@ -43,6 +43,67 @@ func stripMarkers(_ code:String) -> String {
43
43
return outputLines. joined ( separator: " \n " )
44
44
}
45
45
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
+
46
107
func main( ) throws {
47
108
48
109
// initialize template engine and add custom filters
@@ -64,6 +125,7 @@ func main() throws {
64
125
65
126
var generatedFileNames = Set < String > ( )
66
127
var clientCount = 0
128
+ var serverCount = 0
67
129
68
130
// process each .proto file separately
69
131
for fileDescriptor in descriptorSet. files {
@@ -95,13 +157,7 @@ func main() throws {
95
157
" access " : options. visibility. sourceSnippet]
96
158
97
159
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)
105
161
if !generatedFileNames. contains ( clientFileName) {
106
162
generatedFileNames. insert ( clientFileName)
107
163
clientCount += 1
@@ -113,9 +169,10 @@ func main() throws {
113
169
response. file. append ( clientfile)
114
170
}
115
171
116
- let serverFileName = package + " . server.pb.swift "
172
+ let serverFileName = outputFileName ( component : " server " , index : serverCount , fileDescriptor : fileDescriptor )
117
173
if !generatedFileNames. contains ( serverFileName) {
118
174
generatedFileNames. insert ( serverFileName)
175
+ serverCount += 1
119
176
let servercode = try templateEnvironment. renderTemplate ( name: " server.pb.swift " ,
120
177
context: context)
121
178
var serverfile = Google_Protobuf_Compiler_CodeGeneratorResponse . File ( )
0 commit comments