Skip to content

Commit

Permalink
Import file naming code from swift-protobuf to allow consistent file …
Browse files Browse the repository at this point in the history
…naming between the grpc and protobuf plugins.

First steps.
  • Loading branch information
timburks committed Dec 27, 2017
1 parent 464e267 commit f8bc228
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ Plugin/protoc-*
Plugin/swiftgrpc.log
Plugin/echo.*.swift
SwiftGRPC.xcodeproj
Package.resolved
6 changes: 3 additions & 3 deletions Examples/Datastore/PackageManager/RUNME
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ protoc \
googleapis/google/type/latlng.proto \
-Igoogleapis \
--swift_out=googleapis \
--swiftgrpc_out=Sources
--swiftgrpc_out=googleapis

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

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

6 changes: 3 additions & 3 deletions Examples/NaturalLanguage/PackageManager/RUNME
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ protoc \
-Igoogleapis \
-Iprotoc/include \
--swift_out=googleapis \
--swiftgrpc_out=Sources
--swiftgrpc_out=googleapis

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

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

73 changes: 65 additions & 8 deletions Plugin/Sources/protoc-gen-swiftgrpc/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,67 @@ func stripMarkers(_ code:String) -> String {
return outputLines.joined(separator:"\n")
}

// from apple/swift-protobuf/Sources/protoc-gen-swift/StringUtils.swift
func splitPath(pathname: String) -> (dir:String, base:String, suffix:String) {
var dir = ""
var base = ""
var suffix = ""
#if swift(>=3.2)
let pathnameChars = pathname
#else
let pathnameChars = pathname.characters
#endif
for c in pathnameChars {
if c == "/" {
dir += base + suffix + String(c)
base = ""
suffix = ""
} else if c == "." {
base += suffix
suffix = String(c)
} else {
suffix += String(c)
}
}
#if swift(>=3.2)
let validSuffix = suffix.isEmpty || suffix.first == "."
#else
let validSuffix = suffix.isEmpty || suffix.characters.first == "."
#endif
if !validSuffix {
base += suffix
suffix = ""
}
return (dir: dir, base: base, suffix: suffix)
}

enum OutputNaming : String {
case FullPath
case PathToUnderscores
case DropPath
}

func outputFileName(component: String, index: Int, fileDescriptor: FileDescriptor) -> String {
var ext : String
if index == 0 {
ext = "." + component + ".pb.swift"
} else {
ext = "\(index)." + component + ".pb.swift"
}
let pathParts = splitPath(pathname: fileDescriptor.name)
let outputNamingOption = OutputNaming.FullPath // temporarily hard-coded
switch outputNamingOption {
case .FullPath:
return pathParts.dir + pathParts.base + ext
case .PathToUnderscores:
let dirWithUnderscores =
pathParts.dir.replacingOccurrences(of: "/", with: "_")
return dirWithUnderscores + pathParts.base + ext
case .DropPath:
return pathParts.base + ext
}
}

func main() throws {

// initialize template engine and add custom filters
Expand All @@ -64,6 +125,7 @@ func main() throws {

var generatedFileNames = Set<String>()
var clientCount = 0
var serverCount = 0

// process each .proto file separately
for fileDescriptor in descriptorSet.files {
Expand Down Expand Up @@ -95,13 +157,7 @@ func main() throws {
"access": options.visibility.sourceSnippet]

do {
var clientFileName : String
if clientCount == 0 {
clientFileName = package + ".client.pb.swift"
} else {
clientFileName = package + "\(clientCount).client.pb.swift"
}

let clientFileName = outputFileName(component:"client", index:clientCount, fileDescriptor:fileDescriptor)
if !generatedFileNames.contains(clientFileName) {
generatedFileNames.insert(clientFileName)
clientCount += 1
Expand All @@ -113,9 +169,10 @@ func main() throws {
response.file.append(clientfile)
}

let serverFileName = package + ".server.pb.swift"
let serverFileName = outputFileName(component:"server", index:serverCount, fileDescriptor:fileDescriptor)
if !generatedFileNames.contains(serverFileName) {
generatedFileNames.insert(serverFileName)
serverCount += 1
let servercode = try templateEnvironment.renderTemplate(name:"server.pb.swift",
context: context)
var serverfile = Google_Protobuf_Compiler_CodeGeneratorResponse.File()
Expand Down

0 comments on commit f8bc228

Please sign in to comment.