forked from Carthage/Carthage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFrameworkExtensions.swift
356 lines (296 loc) · 9.38 KB
/
FrameworkExtensions.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
//
// FrameworkExtensions.swift
// Carthage
//
// Created by Justin Spahr-Summers on 2014-10-31.
// Copyright (c) 2014 Carthage. All rights reserved.
//
import Foundation
import Result
import ReactiveCocoa
extension String {
/// Returns a producer that will enumerate each line of the receiver, then
/// complete.
internal var linesProducer: SignalProducer<String, NoError> {
return SignalProducer { observer, disposable in
(self as NSString).enumerateLinesUsingBlock { (line, stop) in
observer.sendNext(line)
if disposable.disposed {
stop.memory = true
}
}
observer.sendCompleted()
}
}
}
/// Merges `rhs` into `lhs` and returns the result.
internal func combineDictionaries<K, V>(lhs: [K: V], rhs: [K: V]) -> [K: V] {
var result = lhs
for (key, value) in rhs {
result.updateValue(value, forKey: key)
}
return result
}
extension SignalType {
/// Sends each value that occurs on `signal` combined with each value that
/// occurs on `otherSignal` (repeats included).
private func permuteWith<U>(otherSignal: Signal<U, Error>) -> Signal<(Value, U), Error> {
return Signal { observer in
let lock = NSLock()
lock.name = "org.carthage.CarthageKit.permuteWith"
var signalValues: [Value] = []
var signalCompleted = false
var otherValues: [U] = []
var otherCompleted = false
let compositeDisposable = CompositeDisposable()
compositeDisposable += self.observe { event in
switch event {
case let .Next(value):
lock.lock()
signalValues.append(value)
for otherValue in otherValues {
observer.sendNext((value, otherValue))
}
lock.unlock()
case let .Failed(error):
observer.sendFailed(error)
case .Completed:
lock.lock()
signalCompleted = true
if otherCompleted {
observer.sendCompleted()
}
lock.unlock()
case .Interrupted:
observer.sendInterrupted()
}
}
compositeDisposable += otherSignal.observe { event in
switch event {
case let .Next(value):
lock.lock()
otherValues.append(value)
for signalValue in signalValues {
observer.sendNext((signalValue, value))
}
lock.unlock()
case let .Failed(error):
observer.sendFailed(error)
case .Completed:
lock.lock()
otherCompleted = true
if signalCompleted {
observer.sendCompleted()
}
lock.unlock()
case .Interrupted:
observer.sendInterrupted()
}
}
return compositeDisposable
}
}
}
extension SignalProducerType {
/// Sends each value that occurs on `producer` combined with each value that
/// occurs on `otherProducer` (repeats included).
private func permuteWith<U>(otherProducer: SignalProducer<U, Error>) -> SignalProducer<(Value, U), Error> {
// This should be the implementation of this method:
// return lift(Signal.permuteWith)(otherProducer)
//
// However, due to a Swift miscompilation (with `-O`) we need to inline `lift` here.
// See https://github.com/ReactiveCocoa/ReactiveCocoa/issues/2751 for more details.
//
// This can be reverted once tests with -O don't crash.
return SignalProducer { observer, outerDisposable in
self.startWithSignal { signal, disposable in
outerDisposable.addDisposable(disposable)
otherProducer.startWithSignal { otherSignal, otherDisposable in
outerDisposable.addDisposable(otherDisposable)
signal.permuteWith(otherSignal).observe(observer)
}
}
}
}
/// Sends a boolean of whether the producer succeeded or failed.
internal func succeeded() -> SignalProducer<Bool, NoError> {
return self
.then(.init(value: true))
.flatMapError { _ in .init(value: false) }
}
}
extension SignalProducerType where Value: SignalProducerType, Error == Value.Error {
/// Sends all permutations of the values from the inner producers, as they arrive.
///
/// If no producers are received, sends a single empty array then completes.
@warn_unused_result(message="Did you forget to call `start` on the producer?")
internal func permute() -> SignalProducer<[Value.Value], Error> {
return self
.collect()
.flatMap(.Concat) { (producers: [Value]) -> SignalProducer<[Value.Value], Error> in
var combined = SignalProducer<[Value.Value], Error>(value: [])
for producer in producers {
combined = combined
.permuteWith(producer.producer)
.map { array, value in
var array = array
array.append(value)
return array
}
}
return combined
}
}
}
extension SignalType where Value: EventType, Value.Error == Error {
/// Dematerializes the signal, like dematerialize(), but only yields inner
/// Error events if no values were sent.
internal func dematerializeErrorsIfEmpty() -> Signal<Value.Value, Error> {
return Signal { observer in
var receivedValue = false
var receivedError: Error? = nil
return self.observe { event in
switch event {
case let .Next(innerEvent):
switch innerEvent.event {
case let .Next(value):
receivedValue = true
observer.sendNext(value)
case let .Failed(error):
receivedError = error
case .Completed:
observer.sendCompleted()
case .Interrupted:
observer.sendInterrupted()
}
case let .Failed(error):
observer.sendFailed(error)
case .Completed:
if let receivedError = receivedError where !receivedValue {
observer.sendFailed(receivedError)
}
observer.sendCompleted()
case .Interrupted:
observer.sendInterrupted()
}
}
}
}
}
extension SignalProducerType where Value: EventType, Value.Error == Error {
/// Dematerializes the producer, like dematerialize(), but only yields inner
/// Error events if no values were sent.
internal func dematerializeErrorsIfEmpty() -> SignalProducer<Value.Value, Error> {
return lift { $0.dematerializeErrorsIfEmpty() }
}
}
extension NSScanner {
/// Returns the current line being scanned.
internal var currentLine: NSString {
// Force Foundation types, so we don't have to use Swift's annoying
// string indexing.
let nsString: NSString = string
let scanRange: NSRange = NSMakeRange(scanLocation, 0)
let lineRange: NSRange = nsString.lineRangeForRange(scanRange)
return nsString.substringWithRange(lineRange)
}
}
extension NSURL {
/// The type identifier of the receiver, or an error if it was unable to be
/// determined.
internal var typeIdentifier: Result<String, CarthageError> {
var error: NSError?
do {
var typeIdentifier: AnyObject?
try getResourceValue(&typeIdentifier, forKey: NSURLTypeIdentifierKey)
if let identifier = typeIdentifier as? String {
return .Success(identifier)
}
} catch let err as NSError {
error = err
}
return .Failure(.ReadFailed(self, error))
}
public var carthage_absoluteString: String {
#if swift(>=2.3)
return absoluteString!
#else
return absoluteString
#endif
}
public func appendingPathExtension(pathExtension: String) -> NSURL {
#if swift(>=2.3)
return URLByAppendingPathExtension(pathExtension)!
#else
return URLByAppendingPathExtension(pathExtension)
#endif
}
public func appendingPathComponent(pathComponent: String) -> NSURL {
#if swift(>=2.3)
return URLByAppendingPathComponent(pathComponent)!
#else
return URLByAppendingPathComponent(pathComponent)
#endif
}
public func appendingPathComponent(pathComponent: String, isDirectory: Bool) -> NSURL {
#if swift(>=2.3)
return URLByAppendingPathComponent(pathComponent, isDirectory: isDirectory)!
#else
return URLByAppendingPathComponent(pathComponent, isDirectory: isDirectory)
#endif
}
public func hasSubdirectory(possibleSubdirectory: NSURL) -> Bool {
let standardizedSelf = self.URLByStandardizingPath ?? self
let standardizedOther = possibleSubdirectory.URLByStandardizingPath ?? possibleSubdirectory
if
scheme == standardizedOther.scheme,
let path = standardizedSelf.pathComponents,
let otherPath = standardizedOther.pathComponents
where path.count <= otherPath.count
{
return Array(otherPath[path.indices]) == path
}
return false
}
}
extension NSFileManager {
/// Creates a directory enumerator at the given URL. Sends each URL
/// enumerated, along with the enumerator itself (so it can be introspected
/// and modified as enumeration progresses).
public func carthage_enumeratorAtURL(URL: NSURL, includingPropertiesForKeys keys: [String], options: NSDirectoryEnumerationOptions, catchErrors: Bool = false) -> SignalProducer<(NSDirectoryEnumerator, NSURL), CarthageError> {
return SignalProducer { observer, disposable in
let enumerator = self.enumeratorAtURL(URL, includingPropertiesForKeys: keys, options: options) { (URL, error) in
if catchErrors {
return true
} else {
observer.sendFailed(CarthageError.ReadFailed(URL, error))
return false
}
}!
while !disposable.disposed {
if let URL = enumerator.nextObject() as? NSURL {
let value = (enumerator, URL)
observer.sendNext(value)
} else {
break
}
}
observer.sendCompleted()
}
}
}
/// Creates a counted set from a sequence. The counted set is represented as a
/// dictionary where the keys are elements from the sequence and values count
/// how many times elements are present in the sequence.
internal func buildCountedSet<S: SequenceType>(sequence: S) -> [S.Generator.Element: Int] {
return sequence.reduce([:]) { set, elem in
var set = set
if let count = set[elem] {
set[elem] = count + 1
}
else {
set[elem] = 1
}
return set
}
}