-
Notifications
You must be signed in to change notification settings - Fork 407
/
main.swift
164 lines (149 loc) · 4.66 KB
/
main.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
* Copyright 2017, gRPC Authors All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import Foundation
import SwiftProtobuf
import SwiftProtobufPluginLibrary
func Log(_ message: String) {
FileHandle.standardError.write((message + "\n").data(using: .utf8)!)
}
// 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 FileNaming: String {
case FullPath
case PathToUnderscores
case DropPath
}
func outputFileName(
component: String,
fileDescriptor: FileDescriptor,
fileNamingOption: FileNaming
) -> String {
let ext = "." + component + ".swift"
let pathParts = splitPath(pathname: fileDescriptor.name)
switch fileNamingOption {
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 uniqueOutputFileName(
component: String,
fileDescriptor: FileDescriptor,
fileNamingOption: FileNaming,
generatedFiles: inout [String: Int]
) -> 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,
fileNamingOption: fileNamingOption
)
} else {
generatedFiles[defaultName] = 1
return defaultName
}
}
func printVersion(args: [String]) {
// Stip off the file path
let program = args.first?.split(separator: "/").last ?? "protoc-gen-grpc-swift"
print("\(program) \(Version.versionString)")
}
func main(args: [String]) throws {
if args.dropFirst().contains("--version") {
printVersion(args: args)
return
}
// initialize responses
var response = Google_Protobuf_Compiler_CodeGeneratorResponse(
files: [],
supportedFeatures: [.proto3Optional]
)
// read plugin input
let rawRequest = FileHandle.standardInput.readDataToEndOfFile()
let request = try Google_Protobuf_Compiler_CodeGeneratorRequest(serializedData: rawRequest)
let options = try GeneratorOptions(parameter: request.parameter)
// Build the SwiftProtobufPluginLibrary model of the plugin input
let descriptorSet = DescriptorSet(protos: request.protoFile)
// A count of generated files by desired name (actual name may differ to avoid collisions).
var generatedFiles: [String: Int] = [:]
// Only generate output for services.
for name in request.fileToGenerate {
if let fileDescriptor = descriptorSet.fileDescriptor(named: name),
!fileDescriptor.services.isEmpty {
let grpcFileName = uniqueOutputFileName(
component: "grpc",
fileDescriptor: fileDescriptor,
fileNamingOption: options.fileNaming,
generatedFiles: &generatedFiles
)
let grpcGenerator = Generator(fileDescriptor, options: options)
var grpcFile = Google_Protobuf_Compiler_CodeGeneratorResponse.File()
grpcFile.name = grpcFileName
grpcFile.content = grpcGenerator.code
response.file.append(grpcFile)
}
}
// return everything to the caller
let serializedResponse = try response.serializedData()
FileHandle.standardOutput.write(serializedResponse)
}
do {
try main(args: CommandLine.arguments)
} catch {
Log("ERROR: \(error)")
}