Skip to content

Commit f8bc228

Browse files
committed
Import file naming code from swift-protobuf to allow consistent file naming between the grpc and protobuf plugins.
First steps.
1 parent 464e267 commit f8bc228

File tree

4 files changed

+72
-14
lines changed

4 files changed

+72
-14
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ Plugin/protoc-*
88
Plugin/swiftgrpc.log
99
Plugin/echo.*.swift
1010
SwiftGRPC.xcodeproj
11+
Package.resolved

Examples/Datastore/PackageManager/RUNME

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ protoc \
1616
googleapis/google/type/latlng.proto \
1717
-Igoogleapis \
1818
--swift_out=googleapis \
19-
--swiftgrpc_out=Sources
19+
--swiftgrpc_out=googleapis
2020

21-
# move swift files to this directory
21+
# move Swift files to the Sources directory
2222
find googleapis -name "*.swift" -exec mv {} Sources \;
2323

2424
# remove the grpc service file; we don't need it
25-
rm Sources/google.datastore.v1.server.pb.swift
25+
rm Sources/datastore.server.pb.swift
2626

Examples/NaturalLanguage/PackageManager/RUNME

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ protoc \
1717
-Igoogleapis \
1818
-Iprotoc/include \
1919
--swift_out=googleapis \
20-
--swiftgrpc_out=Sources
20+
--swiftgrpc_out=googleapis
2121

22-
# move swift files to this directory
22+
# move Swift files to the Sources directory
2323
find googleapis -name "*.swift" -exec mv {} Sources \;
2424

2525
# remove the grpc service file; we don't need it
26-
rm Sources/google.*.server.pb.swift
26+
rm Sources/*.server.pb.swift
2727

Plugin/Sources/protoc-gen-swiftgrpc/main.swift

Lines changed: 65 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
46107
func 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

Comments
 (0)