|
7 | 7 | // |
8 | 8 |
|
9 | 9 | #import "PBEasyPipe.h" |
| 10 | +#import <objc/runtime.h> |
10 | 11 |
|
11 | 12 | NSString *const PBEasyPipeErrorDomain = @"PBEasyPipeErrorDomain"; |
12 | 13 | NSString *const PBEasyPipeUnderlyingExceptionKey = @"PBEasyPipeUnderlyingExceptionKey"; |
13 | 14 |
|
| 15 | +#pragma mark - NSPipe category |
| 16 | + |
| 17 | +/* Some NSTask's generate a lot of output. Using a regular pipe with these tasks causes the pipe to fill up before the |
| 18 | + task completes; it will basically fail silently. To fix this, the pipe must occasionally be read into a |
| 19 | + cache, but because this can occur at the end of a chain of async operations, it gets complicated. Instead, you can set |
| 20 | + the .dataOutput property on this NSPipe category, and the intermittent results automatically will be read into |
| 21 | + that NSMutableData. |
| 22 | + */ |
| 23 | + |
| 24 | +@interface NSPipe (PBEasyPipe) |
| 25 | + |
| 26 | +@property (nonatomic, strong) NSMutableData *dataOutput; |
| 27 | + |
| 28 | +@end |
| 29 | + |
| 30 | +@implementation NSPipe (PBEasyPipe) |
| 31 | + |
| 32 | +- (void)setDataOutput:(nonnull NSMutableData *)dataOutput { |
| 33 | + objc_setAssociatedObject(self, @selector(dataOutput), dataOutput, OBJC_ASSOCIATION_RETAIN_NONATOMIC); |
| 34 | + |
| 35 | + NSFileHandle *fileHandle = self.fileHandleForReading; |
| 36 | + NSAssert(fileHandle != nil, @"should have file handle for reading"); |
| 37 | + |
| 38 | + fileHandle.readabilityHandler = ^(NSFileHandle * _Nonnull fileHandle) { |
| 39 | + [dataOutput appendData:fileHandle.availableData]; |
| 40 | + }; |
| 41 | +} |
| 42 | + |
| 43 | +- (NSMutableData *)dataOutput { |
| 44 | + return objc_getAssociatedObject(self, @selector(dataOutput)); |
| 45 | +} |
| 46 | + |
| 47 | +- (void)clear { |
| 48 | + self.fileHandleForReading.readabilityHandler = nil; |
| 49 | +} |
| 50 | + |
| 51 | +@end |
| 52 | + |
| 53 | +#pragma mark - PBEasyPipe implementation |
| 54 | + |
14 | 55 | @implementation PBEasyPipe |
15 | 56 |
|
16 | 57 | + (nullable NSTask *)performCommand:(NSString *)command arguments:(NSArray *)arguments inDirectory:(NSString *)directory terminationHandler:(void (^)(NSTask *, NSError *error))terminationHandler { |
17 | 58 | NSParameterAssert(command != nil); |
18 | 59 |
|
19 | 60 | NSTask *task = [self taskForCommand:command arguments:arguments inDirectory:directory]; |
| 61 | + NSPipe *pipe = task.standardOutput; |
| 62 | + pipe.dataOutput = [NSMutableData data]; |
| 63 | + |
20 | 64 | task.terminationHandler = ^(NSTask *task) { |
21 | 65 | dispatch_async(dispatch_get_main_queue(), ^{ |
22 | 66 | terminationHandler(task, nil); |
23 | 67 | }); |
24 | 68 | }; |
| 69 | + |
25 | 70 | @try { |
26 | 71 | [task launch]; |
27 | | - } |
28 | | - @catch (NSException *exception) { |
| 72 | + } @catch (NSException *exception) { |
29 | 73 | NSString *desc = @"Exception raised while launching task"; |
30 | 74 | NSString *failureReason = [NSString stringWithFormat:@"The task \"%@\" failed to launch", command]; |
31 | | - NSDictionary *info = @{ |
32 | | - NSLocalizedDescriptionKey: desc, |
33 | | - NSLocalizedFailureReasonErrorKey: failureReason, |
34 | | - PBEasyPipeUnderlyingExceptionKey: exception, |
35 | | - }; |
36 | | - NSError *error = [NSError errorWithDomain:PBEasyPipeErrorDomain |
37 | | - code:PBEasyPipeTaskLaunchError |
38 | | - userInfo:info]; |
| 75 | + NSDictionary *info = @{NSLocalizedDescriptionKey: desc, |
| 76 | + NSLocalizedFailureReasonErrorKey: failureReason, |
| 77 | + PBEasyPipeUnderlyingExceptionKey: exception}; |
| 78 | + NSError *error = [NSError errorWithDomain:PBEasyPipeErrorDomain code:PBEasyPipeTaskLaunchError userInfo:info]; |
39 | 79 | terminationHandler(task, error); |
40 | | - return nil; |
41 | 80 | } |
| 81 | + |
42 | 82 | return task; |
43 | 83 | } |
44 | 84 |
|
45 | 85 | + (nullable NSTask *)performCommand:(NSString *)command arguments:(NSArray *)arguments inDirectory:(NSString *)directory completionHandler:(void (^)(NSTask *, NSData *readData, NSError *error))completionHandler { |
| 86 | + |
46 | 87 | return [self performCommand:command arguments:arguments inDirectory:directory terminationHandler:^(NSTask *task, NSError *error) { |
| 88 | + NSPipe *pipe = task.standardOutput; |
| 89 | + |
47 | 90 | if (error) { |
| 91 | + [pipe clear]; |
48 | 92 | completionHandler(task, nil, error); |
49 | 93 | return; |
50 | 94 | } |
51 | 95 |
|
52 | | - NSData *data = [[task.standardOutput fileHandleForReading] readDataToEndOfFile]; |
53 | | - completionHandler(task, data, nil); |
| 96 | + [pipe clear]; |
| 97 | + |
| 98 | + completionHandler(task, pipe.dataOutput, nil); |
54 | 99 | }]; |
55 | 100 | } |
56 | 101 |
|
|
0 commit comments