Skip to content

Commit 78c5fed

Browse files
glbrnttMrMage
authored andcommitted
Allow "FileNaming" option to be set via CLI
1 parent 6928fb2 commit 78c5fed

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ separated from the output directory by a colon.
102102
| `Async` | `true`/`false` | `true` | Whether to generate asynchronous code |
103103
| `Sync` | `true`/`false` | `true` | Whether to generate synchronous code |
104104
| `TestStubs` | `true`/`false` | `false` | Whether to generate test stub code |
105+
| `FileNaming` | `FullPath`/`PathToUnderscores`/`DropPath` | `FullPath` | How to handle the naming of generated sources |
105106

106107
Example:
107108

Sources/protoc-gen-swiftgrpc/main.swift

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,16 @@ func splitPath(pathname: String) -> (dir: String, base: String, suffix: String)
5555
return (dir: dir, base: base, suffix: suffix)
5656
}
5757

58-
enum OutputNaming: String {
58+
enum FileNaming: String {
5959
case FullPath
6060
case PathToUnderscores
6161
case DropPath
6262
}
6363

64-
var outputNamingOption: OutputNaming = .FullPath // temporarily hard-coded
65-
66-
func outputFileName(component: String, fileDescriptor: FileDescriptor) -> String {
64+
func outputFileName(component: String, fileDescriptor: FileDescriptor, fileNamingOption: FileNaming) -> String {
6765
let ext = "." + component + ".swift"
6866
let pathParts = splitPath(pathname: fileDescriptor.name)
69-
switch outputNamingOption {
67+
switch fileNamingOption {
7068
case .FullPath:
7169
return pathParts.dir + pathParts.base + ext
7270
case .PathToUnderscores:
@@ -80,11 +78,11 @@ func outputFileName(component: String, fileDescriptor: FileDescriptor) -> String
8078

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

83-
func uniqueOutputFileName(component: String, fileDescriptor: FileDescriptor) -> String {
84-
let defaultName = outputFileName(component: component, fileDescriptor: fileDescriptor)
81+
func uniqueOutputFileName(component: String, fileDescriptor: FileDescriptor, fileNamingOption: FileNaming) -> String {
82+
let defaultName = outputFileName(component: component, fileDescriptor: fileDescriptor, fileNamingOption: fileNamingOption)
8583
if let count = generatedFiles[defaultName] {
8684
generatedFiles[defaultName] = count + 1
87-
return outputFileName(component: "\(count)." + component, fileDescriptor: fileDescriptor)
85+
return outputFileName(component: "\(count)." + component, fileDescriptor: fileDescriptor, fileNamingOption: fileNamingOption)
8886
} else {
8987
generatedFiles[defaultName] = 1
9088
return defaultName
@@ -108,7 +106,7 @@ func main() throws {
108106
// process each .proto file separately
109107
for fileDescriptor in descriptorSet.files {
110108
if fileDescriptor.services.count > 0 {
111-
let grpcFileName = uniqueOutputFileName(component: "grpc", fileDescriptor: fileDescriptor)
109+
let grpcFileName = uniqueOutputFileName(component: "grpc", fileDescriptor: fileDescriptor, fileNamingOption: options.fileNaming)
112110
let grpcGenerator = Generator(fileDescriptor, options: options)
113111
var grpcFile = Google_Protobuf_Compiler_CodeGeneratorResponse.File()
114112
grpcFile.name = grpcFileName

Sources/protoc-gen-swiftgrpc/options.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ final class GeneratorOptions {
5959
private(set) var generateTestStubs = false
6060
private(set) var generateNIOImplementation = false
6161
private(set) var protoToModuleMappings = ProtoFileToModuleMappings()
62+
private(set) var fileNaming = FileNaming.FullPath
6263

6364
init(parameter: String?) throws {
6465
for pair in GeneratorOptions.parseParameter(string: parameter) {
@@ -123,6 +124,13 @@ final class GeneratorOptions {
123124
}
124125
}
125126

127+
case "FileNaming":
128+
if let value = FileNaming(rawValue: pair.value) {
129+
fileNaming = value
130+
} else {
131+
throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
132+
}
133+
126134
default:
127135
throw GenerationError.unknownParameter(name: pair.key)
128136
}

0 commit comments

Comments
 (0)