Skip to content

Commit

Permalink
Allow "FileNaming" option to be set via CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
glbrntt authored and MrMage committed Jan 3, 2019
1 parent 6928fb2 commit 78c5fed
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ separated from the output directory by a colon.
| `Async` | `true`/`false` | `true` | Whether to generate asynchronous code |
| `Sync` | `true`/`false` | `true` | Whether to generate synchronous code |
| `TestStubs` | `true`/`false` | `false` | Whether to generate test stub code |
| `FileNaming` | `FullPath`/`PathToUnderscores`/`DropPath` | `FullPath` | How to handle the naming of generated sources |

Example:

Expand Down
16 changes: 7 additions & 9 deletions Sources/protoc-gen-swiftgrpc/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,16 @@ func splitPath(pathname: String) -> (dir: String, base: String, suffix: String)
return (dir: dir, base: base, suffix: suffix)
}

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

var outputNamingOption: OutputNaming = .FullPath // temporarily hard-coded

func outputFileName(component: String, fileDescriptor: FileDescriptor) -> String {
func outputFileName(component: String, fileDescriptor: FileDescriptor, fileNamingOption: FileNaming) -> String {
let ext = "." + component + ".swift"
let pathParts = splitPath(pathname: fileDescriptor.name)
switch outputNamingOption {
switch fileNamingOption {
case .FullPath:
return pathParts.dir + pathParts.base + ext
case .PathToUnderscores:
Expand All @@ -80,11 +78,11 @@ func outputFileName(component: String, fileDescriptor: FileDescriptor) -> String

var generatedFiles: [String: Int] = [:]

func uniqueOutputFileName(component: String, fileDescriptor: FileDescriptor) -> String {
let defaultName = outputFileName(component: component, fileDescriptor: fileDescriptor)
func uniqueOutputFileName(component: String, fileDescriptor: FileDescriptor, fileNamingOption: FileNaming) -> String {
let defaultName = outputFileName(component: component, fileDescriptor: fileDescriptor, fileNamingOption: fileNamingOption)
if let count = generatedFiles[defaultName] {
generatedFiles[defaultName] = count + 1
return outputFileName(component: "\(count)." + component, fileDescriptor: fileDescriptor)
return outputFileName(component: "\(count)." + component, fileDescriptor: fileDescriptor, fileNamingOption: fileNamingOption)
} else {
generatedFiles[defaultName] = 1
return defaultName
Expand All @@ -108,7 +106,7 @@ func main() throws {
// process each .proto file separately
for fileDescriptor in descriptorSet.files {
if fileDescriptor.services.count > 0 {
let grpcFileName = uniqueOutputFileName(component: "grpc", fileDescriptor: fileDescriptor)
let grpcFileName = uniqueOutputFileName(component: "grpc", fileDescriptor: fileDescriptor, fileNamingOption: options.fileNaming)
let grpcGenerator = Generator(fileDescriptor, options: options)
var grpcFile = Google_Protobuf_Compiler_CodeGeneratorResponse.File()
grpcFile.name = grpcFileName
Expand Down
8 changes: 8 additions & 0 deletions Sources/protoc-gen-swiftgrpc/options.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ final class GeneratorOptions {
private(set) var generateTestStubs = false
private(set) var generateNIOImplementation = false
private(set) var protoToModuleMappings = ProtoFileToModuleMappings()
private(set) var fileNaming = FileNaming.FullPath

init(parameter: String?) throws {
for pair in GeneratorOptions.parseParameter(string: parameter) {
Expand Down Expand Up @@ -123,6 +124,13 @@ final class GeneratorOptions {
}
}

case "FileNaming":
if let value = FileNaming(rawValue: pair.value) {
fileNaming = value
} else {
throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
}

default:
throw GenerationError.unknownParameter(name: pair.key)
}
Expand Down

0 comments on commit 78c5fed

Please sign in to comment.