@@ -24,170 +24,6 @@ import SwiftProtobuf
24
24
import SwiftProtobufPluginLibrary
25
25
26
26
struct GeneratorPlugin : CodeGenerator {
27
- private enum Mode {
28
- case showHelp
29
- case showVersion
30
- case generateFromStdin
31
- case generateFromFiles( paths: [ String ] )
32
- }
33
-
34
- func run( args: [ String ] ) -> Int32 {
35
- var result : Int32 = 0
36
-
37
- let mode = parseCommandLine ( args: args)
38
- switch mode {
39
- case . showHelp:
40
- showHelp ( )
41
- case . showVersion:
42
- showVersion ( )
43
- case . generateFromStdin:
44
- result = generateFromStdin ( )
45
- case . generateFromFiles( let paths) :
46
- result = generateFromFiles ( paths)
47
- }
48
-
49
- return result
50
- }
51
-
52
- private func parseCommandLine( args: [ String ] ) -> Mode {
53
- var paths : [ String ] = [ ]
54
- for arg in args {
55
- switch arg {
56
- case " -h " , " --help " :
57
- return . showHelp
58
- case " --version " :
59
- return . showVersion
60
- default :
61
- if arg. hasPrefix ( " - " ) {
62
- Stderr . print ( " Unknown argument: \" \( arg) \" " )
63
- return . showHelp
64
- } else {
65
- paths. append ( arg)
66
- }
67
- }
68
- }
69
- return paths. isEmpty ? . generateFromStdin : . generateFromFiles( paths: paths)
70
- }
71
-
72
- private func showHelp( ) {
73
- let version = SwiftProtobuf . Version. self
74
- let packageVersion = " \( version. major) , \( version. minor) , \( version. revision) "
75
-
76
- print ( """
77
- \( CommandLine . programName) : Convert parsed proto definitions into Swift
78
-
79
- """ )
80
- showVersion ( )
81
- print ( """
82
- \( Version . copyright)
83
-
84
- Note: This is a plugin for protoc and should not normally be run
85
- directly.
86
-
87
- If you invoke a recent version of protoc with the --swift_out=<dir>
88
- option, then protoc will search the current PATH for protoc-gen-swift
89
- and use it to generate Swift output.
90
-
91
- In particular, if you have renamed this program, you will need to
92
- adjust the protoc command-line option accordingly.
93
-
94
- The generated Swift output requires the SwiftProtobuf \( SwiftProtobuf . Version. versionString)
95
- library be included in your project.
96
-
97
- If you use `swift build` to compile your project, add this to
98
- Package.swift:
99
-
100
- dependencies: [
101
- .package(name: " SwiftProtobuf " , url: " https://github.com/apple/swift-protobuf.git " , from: " \( packageVersion) " ),
102
- ]
103
-
104
- Usage: \( CommandLine . programName) [options] [filename...]
105
-
106
- -h|--help: Print this help message
107
- --version: Print the program version
108
-
109
- Filenames specified on the command line indicate binary-encoded
110
- google.protobuf.compiler.CodeGeneratorRequest objects that will
111
- be read and converted to Swift source code. The source text will be
112
- written directly to stdout.
113
-
114
- When invoked with no filenames, it will read a single binary-encoded
115
- google.protobuf.compiler.CodeGeneratorRequest object from stdin and
116
- emit the corresponding CodeGeneratorResponse object to stdout.
117
- """ )
118
- }
119
-
120
- private func showVersion( ) {
121
- print ( " \( CommandLine . programName) \( SwiftProtobuf . Version. versionString) " )
122
- }
123
-
124
- private func generateFromStdin( ) -> Int32 {
125
- let requestData = FileHandle . standardInput. readDataToEndOfFile ( )
126
-
127
- // Support for loggin the request. Useful when protoc/protoc-gen-swift are
128
- // being invoked from some build system/script. protoc-gen-swift supports
129
- // loading a request as a command line argument to simplify debugging/etc.
130
- if let dumpPath = ProcessInfo . processInfo. environment [ " PROTOC_GEN_SWIFT_LOG_REQUEST " ] , !dumpPath. isEmpty {
131
- let dumpURL = URL ( fileURLWithPath: dumpPath)
132
- do {
133
- try requestData. write ( to: dumpURL)
134
- } catch let e {
135
- Stderr . print ( " Failed to write request to ' \( dumpPath) ', \( e) " )
136
- }
137
- }
138
-
139
- let request : Google_Protobuf_Compiler_CodeGeneratorRequest
140
- do {
141
- request = try Google_Protobuf_Compiler_CodeGeneratorRequest ( serializedData: requestData)
142
- } catch let e {
143
- Stderr . print ( " Request failed to decode: \( e) " )
144
- return 1
145
- }
146
-
147
- auditProtoCVersion ( request: request)
148
- let response = generateCode ( request: request, generator: self )
149
- guard sendReply ( response: response) else { return 1 }
150
- return 0
151
- }
152
-
153
- private func generateFromFiles( _ paths: [ String ] ) -> Int32 {
154
- var result : Int32 = 0
155
-
156
- for p in paths {
157
- let requestData : Data
158
- do {
159
- requestData = try readFileData ( filename: p)
160
- } catch let e {
161
- Stderr . print ( " Error reading from \( p) - \( e) " )
162
- result = 1
163
- continue
164
- }
165
- Stderr . print ( " Read request: \( requestData. count) bytes from \( p) " )
166
-
167
- let request : Google_Protobuf_Compiler_CodeGeneratorRequest
168
- do {
169
- request = try Google_Protobuf_Compiler_CodeGeneratorRequest ( serializedData: requestData)
170
- } catch let e {
171
- Stderr . print ( " Request failed to decode \( p) : \( e) " )
172
- result = 1
173
- continue
174
- }
175
-
176
- let response = generateCode ( request: request, generator: self )
177
- if response. hasError {
178
- Stderr . print ( " Error while generating from \( p) - \( response. error) " )
179
- result = 1
180
- } else {
181
- for f in response. file {
182
- print ( " +++ Begin File: \( f. name) +++ " )
183
- print ( !f. content. isEmpty ? f. content : " <No content> " )
184
- print ( " +++ End File: \( f. name) +++ " )
185
- }
186
- }
187
- }
188
-
189
- return result
190
- }
191
27
192
28
func generate(
193
29
files: [ SwiftProtobufPluginLibrary . FileDescriptor ] ,
@@ -197,6 +33,7 @@ struct GeneratorPlugin: CodeGenerator {
197
33
) throws {
198
34
let options = try GeneratorOptions ( parameter: parameter)
199
35
36
+ auditProtoCVersion ( context: protoCompilerContext)
200
37
var errorString : String ? = nil
201
38
for fileDescriptor in files {
202
39
let fileGenerator = FileGenerator ( fileDescriptor: fileDescriptor, generatorOptions: options)
@@ -215,8 +52,12 @@ struct GeneratorPlugin: CodeGenerator {
215
52
. proto3Optional,
216
53
]
217
54
218
- private func auditProtoCVersion( request: Google_Protobuf_Compiler_CodeGeneratorRequest ) {
219
- guard request. hasCompilerVersion else {
55
+ var version : String ? { return " \( SwiftProtobuf . Version. versionString) " }
56
+ var copyrightLine : String ? { return " \( Version . copyright) " }
57
+ var projectURL : String ? { return " https://github.com/apple/swift-protobuf " }
58
+
59
+ private func auditProtoCVersion( context: SwiftProtobufPluginLibrary . ProtoCompilerContext ) {
60
+ guard context. version != nil else {
220
61
Stderr . print ( " WARNING: unknown version of protoc, use 3.2.x or later to ensure JSON support is correct. " )
221
62
return
222
63
}
@@ -225,24 +66,44 @@ struct GeneratorPlugin: CodeGenerator {
225
66
// is there, the JSON support should be good.
226
67
}
227
68
228
- private func sendReply( response: Google_Protobuf_Compiler_CodeGeneratorResponse ) -> Bool {
229
- let serializedResponse : Data
230
- do {
231
- serializedResponse = try response. serializedBytes ( )
232
- } catch let e {
233
- Stderr . print ( " Failure while serializing response: \( e) " )
234
- return false
235
- }
236
- FileHandle . standardOutput. write ( serializedResponse)
237
- return true
69
+ // Provide an expanded version of help.
70
+ func printHelp( ) {
71
+ print ( """
72
+ \( CommandLine . programName) : Convert parsed proto definitions into Swift
73
+
74
+ \( Version . copyright)
75
+
76
+ Note: This is a plugin for protoc and should not normally be run
77
+ directly.
78
+
79
+ If you invoke a recent version of protoc with the --swift_out=<dir>
80
+ option, then protoc will search the current PATH for protoc-gen-swift
81
+ and use it to generate Swift output.
82
+
83
+ In particular, if you have renamed this program, you will need to
84
+ adjust the protoc command-line option accordingly.
85
+
86
+ The generated Swift output requires the SwiftProtobuf \( version!)
87
+ library be included in your project.
88
+
89
+ If you use `swift build` to compile your project, add this to
90
+ Package.swift:
91
+
92
+ dependencies: [
93
+ .package(name: " SwiftProtobuf " , url: " https://github.com/apple/swift-protobuf.git " , from: " \( version!) " ),
94
+ ]
95
+
96
+ Usage: \( CommandLine . programName) [options] [filename...]
97
+
98
+ -h|--help: Print this help message
99
+ --version: Print the program version
100
+
101
+ """ )
238
102
}
239
103
240
104
}
241
105
242
106
// MARK: - Hand off to the GeneratorPlugin
243
107
244
- // Drop the program name off to get the arguments only.
245
- let args : [ String ] = [ String] ( CommandLine . arguments. dropFirst ( 1 ) )
246
108
let plugin = GeneratorPlugin ( )
247
- let result = plugin. run ( args: args)
248
- exit ( result)
109
+ plugin. main ( nil )
0 commit comments