/
Compatibility.swift
320 lines (229 loc) · 11.3 KB
/
Compatibility.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
//
// Compatibility.swift
// SwiftCLI
//
// Created by Jake Heiser on 9/9/17.
//
import Foundation
// MARK: Minor version deprecations
@available(*, deprecated, message: "use Param<String>")
public typealias Parameter = Param<String>
@available(*, deprecated, message: "use Param<String?>")
public typealias OptionalParameter = Param<String?>
@available(*, deprecated, message: "use CollectedParam<String>(minCount: 1)")
public class CollectedParameter: CollectedParam<String> {
public init(completion: ShellCompletion = .filename, validation: [Validation<Value>] = []) {
super.init(minCount: 1, completion: completion, validation: validation)
}
}
@available(*, deprecated, message: "use CollectedParam<String>")
public typealias OptionalCollectedParameter = CollectedParam<String>
// MARK: -
@available(*, unavailable, renamed: "Task.run")
public func run(_ executable: String, _ arguments: String...) throws {}
@available(*, unavailable, renamed: "Task.run")
public func run(_ executable: String, arguments: [String], directory: String? = nil) throws {}
@available(*, unavailable, renamed: "Task.capture")
public func capture(_ executable: String, _ arguments: String...) throws -> CaptureResult { fatalError() }
@available(*, unavailable, renamed: "Task.capture")
public func capture(_ executable: String, arguments: [String], directory: String? = nil) throws -> CaptureResult { fatalError() }
@available(*, unavailable, renamed: "Task.run")
public func run(bash: String, directory: String? = nil) throws {}
@available(*, unavailable, renamed: "Task.capture")
public func capture(bash: String, directory: String? = nil) throws -> CaptureResult { fatalError() }
#if !os(iOS)
extension Task {
@available(*, unavailable, message: "Use Task.execvp(_:arguments:directory:env) instead")
public static func execvp(_ executable: String, directory: String? = nil, _ args: String..., env: [String: String]? = nil) throws -> Never {
fatalError()
}
@available(*, unavailable, message: "Use Task.execvp(_:arguments:directory:env) instead")
public static func execvp(_ executable: String, directory: String? = nil, _ args: [String], env: [String: String]? = nil) throws -> Never {
fatalError()
}
@available(*, unavailable, renamed: "init(executable:arguments:directory:stdout:stderr:stdin:)")
public convenience init(executable: String, args: [String] = [], currentDirectory: String? = nil, stdout: WritableStream = WriteStream.stdout, stderr: WritableStream = WriteStream.stderr, stdin: ReadableStream = ReadStream.stdin) {
fatalError()
}
@available(*, unavailable)
public static func findExecutable(named: String) -> String? { nil }
}
#endif
extension Input {
@available(*, unavailable, message: "Use Validation<String>.custom instead of (String) -> Bool")
public static func readLine(prompt: String? = nil, secure: Bool = false, validation: ((String) -> Bool)? = nil, errorResponse: InputReader<String>.ErrorResponse? = nil) -> String {
return ""
}
@available(*, unavailable, message: "Use Validation<Int>.custom instead of (String) -> Bool")
public static func readInt(prompt: String? = nil, secure: Bool = false, validation: ((Int) -> Bool)? = nil, errorResponse: InputReader<Int>.ErrorResponse? = nil) -> Int {
return 0
}
@available(*, unavailable, message: "Use Validation<Double>.custom instead of (String) -> Bool")
public static func readDouble(prompt: String? = nil, secure: Bool = false, validation: ((Double) -> Bool)? = nil, errorResponse: InputReader<Double>.ErrorResponse? = nil) -> Double {
return 0
}
@available(*, unavailable, message: "Use Validation<Bool>.custom instead of (String) -> Bool")
public static func readBool(prompt: String? = nil, secure: Bool = false, validation: ((Bool) -> Bool)? = nil, errorResponse: InputReader<Bool>.ErrorResponse? = nil) -> Bool {
return false
}
@available(*, unavailable, message: "Use Validation<T>.custom instead of (T) -> Bool")
public static func readObject<T: ConvertibleFromString>(prompt: String? = nil, secure: Bool = false, validation: ((T) -> Bool)? = nil, errorResponse: InputReader<T>.ErrorResponse? = nil) -> T {
fatalError()
}
}
extension InputReader {
@available(*, unavailable, message: "Use Validation<T>.custom instead of InputReader<T>.Validation")
public typealias Validation = (T) -> Bool
}
extension CLI {
@available(*, unavailable, message: "Use go(with:)")
public func debugGo(with argumentString: String) -> Int32 {
return 1
}
}
@available(*, unavailable, renamed: "ShellCompletion")
public typealias Completion = ShellCompletion
extension CaptureResult {
/// The full stdout contents; use `stdout` for trimmed contents
@available(*, unavailable, message: "Use stdout or stdoutData")
public var rawStdout: String {
return String(data: stdoutData, encoding: .utf8) ?? ""
}
/// The full stderr contents; use `stderr` for trimmed contents
@available(*, unavailable, message: "Use stderr or stderrData")
public var rawStderr: String {
return String(data: stderrData, encoding: .utf8) ?? ""
}
}
// MARK: - Swift versions
#if !swift(>=4.1)
extension Sequence {
func compactMap<T>(_ transform: (Element) -> T?) -> [T] {
return flatMap(transform)
}
}
#endif
#if !swift(>=5.0)
extension Collection {
func firstIndex(where test: (Element) -> Bool) -> Index? {
return index(where: test)
}
}
extension Collection where Element: Equatable {
func firstIndex(of element: Element) -> Index? {
return index(of: element)
}
}
#endif
// MARK: Unavailable
extension CLI {
@available(*, unavailable, message: "Create a new CLI object: let cli = CLI(..)")
public static var name: String { return "" }
@available(*, unavailable, message: "Create a new CLI object: let cli = CLI(..)")
public static var version: String? { return nil }
@available(*, unavailable, message: "Create a new CLI object: let cli = CLI(..)")
public static var description: String? { return "" }
@available(*, unavailable, message: "Create a custom HelpMessageGenerator instead")
public static var helpCommand: Command? = nil
@available(*, unavailable, message: "Create the CLI object with a nil version and register a custom version command")
public static var versionCommand: Command? = nil
@available(*, unavailable, message: "Create a new CLI object: let cli = CLI(..)")
public static var helpMessageGenerator: HelpMessageGenerator { return DefaultHelpMessageGenerator() }
@available(*, unavailable, message: "Create a new CLI object: let cli = CLI(..)")
public static var argumentListManipulators: [_ArgumentListManipulator] { return [] }
@available(*, unavailable, message: "Create a new CLI object: let cli = CLI(..)")
public static func setup(name: String, version: String? = nil, description: String? = nil) {}
@available(*, unavailable, message: "Create a new CLI object: let cli = CLI(..)")
public static func register(command: Command) {}
@available(*, unavailable, message: "Create a new CLI object: let cli = CLI(..)")
public static func register(commands: [Command]) {}
@available(*, unavailable, message: "Create a new CLI object: let cli = CLI(..)")
public static func go() -> Int32 { return 0 }
@available(*, unavailable, message: "Create a new CLI object: let cli = CLI(..)")
public static func debugGo(with argumentString: String) -> Int32 { return 0 }
}
extension Input {
@available(*, unavailable, message: "Use Input.readLine()")
public static func awaitInput(message: String?, secure: Bool = false) -> String { return "" }
@available(*, unavailable, message: "Use Input.readLine() with a validation closure")
public static func awaitInputWithValidation(message: String?, secure: Bool = false, validation: (_ input: String) -> Bool) -> String { return "" }
@available(*, unavailable, message: "Implement CovertibleFromString on a custom object and use Input.readObject()")
public static func awaitInputWithConversion<T>(message: String?, secure: Bool = false, conversion: (_ input: String) -> T?) -> T { return conversion("")! }
@available(*, unavailable, message: "Use Input.readInt() instead")
public static func awaitInt(message: String?) -> Int { return 0 }
@available(*, unavailable, message: "Use Input.readBool() instead")
public static func awaitYesNoInput(message: String = "Confirm?") -> Bool { return false }
}
@available(*, unavailable, renamed: "WritableStream")
public typealias OutputByteStream = WritableStream
@available(*, unavailable, message: "Use WriteStream.stdout instead")
public class StdoutStream {}
@available(*, unavailable, message: "Use WriteStream.stderr instead")
public class StderrStream {}
@available(*, unavailable, message: "Use WriteStream.null instead")
public class NullStream {}
@available(*, unavailable, renamed: "WriteStream")
public typealias FileStream = WriteStream
extension WritableStream {
@available(*, unavailable, renamed: "print")
func output(_ content: String) {}
@available(*, unavailable, renamed: "print")
public func output(_ content: String, terminator: String) {}
}
@available(*, unavailable, message: "use CLI.Error instead")
public enum CLIError: Error {
case error(String)
case emptyError
}
@available(*, unavailable, message: "Implement HelpMessageGenerator instead")
public protocol UsageStatementGenerator {
func generateUsageStatement(for command: Command) -> String
}
@available(*, unavailable, renamed: "WriteStream.stderr.print")
public func printError(_ error: String) {}
@available(*, unavailable, renamed: "WriteStream.stderr.print")
public func printError(_ error: String, terminator: String) {}
extension WriteStream {
@available(*, unavailable, renamed: "WriteStream.for(path:)")
public init?(path: String) {
return nil
}
@available(*, unavailable, renamed: "WriteStream.for(fileHandle:)")
public init(writeHandle: FileHandle) {
fatalError()
}
@available(*, unavailable, renamed: "closeWrite")
public func close() {}
}
extension WriteStream.FileStream {
@available(*, unavailable, renamed: "closeWrite")
public func close() {}
}
extension WriteStream.FileHandleStream {
@available(*, unavailable, renamed: "closeWrite")
public func close() {}
}
extension ReadStream {
@available(*, unavailable, renamed: "ReadStream.for(path:)")
public init?(path: String) {
return nil
}
@available(*, unavailable, renamed: "ReadStream.for(fileHandle:)")
public init(readHandle: FileHandle) {
fatalError()
}
@available(*, unavailable, renamed: "closeRead")
public func close() {}
}
extension ReadStream.FileStream {
@available(*, unavailable, renamed: "closeRead")
public func close() {}
}
extension ReadStream.FileHandleStream {
@available(*, unavailable, renamed: "closeRead")
public func close() {}
}
extension LineStream {
@available(*, unavailable, message: "no longer needs to be called if this stream is the stdout or stderr of a Task; otherwise call waitToFinishProcessing()")
public func wait() {}
}