MYUtilities / MYTask.m

  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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
//
//  MYTask.m
//  MYUtilities
//
//  Copyright 2008 Jens Alfke. All rights reserved.
//

#import "MYTask.h"

//FIX: NOTICE: This code was written assuming garbage collection. It will currently leak like a sieve without it.

NSString* const MYTaskErrorDomain = @"MYTaskError";
NSString* const MYTaskExitCodeKey = @"MYTaskExitCode";
NSString* const MYTaskObjectKey = @"MYTask";

#define MYTaskSynchronousRunLoopMode @"MYTask"


@interface MYTask ()
@property (readwrite,nonatomic) BOOL isRunning;
@property (readwrite,retain,nonatomic) NSError *error;
- (void) _finishUp;
@end


@implementation MYTask


- (id) initWithCommand: (NSString*)command
             arguments: (NSArray*)arguments
{
    Assert(command);
    self = [super init];
    if (self != nil) {
        _command = command;
        _arguments = arguments ?[arguments mutableCopy] :[NSMutableArray array];
        _modes = [NSMutableArray arrayWithObjects: NSDefaultRunLoopMode, NSModalPanelRunLoopMode, nil];
    }
    return self;
}


- (id) initWithCommand: (NSString*)command, ...
{
    NSMutableArray *arguments = [NSMutableArray array];
    va_list args;
    va_start(args,command);
    id arg;
    while( nil != (arg=va_arg(args,id)) )
        [arguments addObject: [arg description]];
    va_end(args);
    
    return [self initWithCommand: command arguments: arguments];
}


- (id) initWithError: (NSError*)error
{
    self = [super init];
    if( self ) {
        _error = error;
    }
    return self;
}


- (NSString*) description
{
    return [NSString stringWithFormat: @"%@ %@", 
            _command, [_arguments componentsJoinedByString: @" "]];
}


- (void) addArgument: (id)argument
{
    [_arguments addObject: [argument description]];
}

- (void) addArgumentsFromArray: (NSArray*)arguments
{
    for( id arg in arguments )
        [_arguments addObject: [arg description]];
}

- (void) addArguments: (id)arg, ...
{
    va_list args;
    va_start(args,arg);
    while( arg ) {
        [_arguments addObject: [arg description]];
        arg = va_arg(args,id);
    }
    va_end(args);
}

- (void) prependArguments: (id)arg, ...
{
    va_list args;
    va_start(args,arg);
    int i=0;
    while( arg ) {
        [_arguments insertObject: [arg description] atIndex: i++];
        arg = va_arg(args,id);
    }
    va_end(args);
}


- (NSString*) commandLine {
    NSMutableString *desc = [NSMutableString stringWithString: _command];
    for (NSString *arg in _arguments) {
        [desc appendString: @" "];
        if ([arg rangeOfString: @" "].length > 0)
            arg = [NSString stringWithFormat: @"'%@'", arg];
        [desc appendString: arg];
    }
    return desc;
}


- (void) ignoreOutput
{
    _ignoreOutput = YES;
}


- (BOOL) makeError: (NSString*)fmt, ...
{
    va_list args;
    va_start(args,fmt);

    NSString *message = [[NSString alloc] initWithFormat: fmt arguments: args];
    LogTo(MYTask, @"Error: %@",message);
    NSMutableDictionary *info = [NSMutableDictionary dictionaryWithObject: message
                                                                   forKey: NSLocalizedDescriptionKey];
    _error = [NSError errorWithDomain: MYTaskErrorDomain code: kMYTaskError userInfo: info];

    va_end(args);
    return NO;
}


- (NSPipe*) _openPipeAndHandle: (NSFileHandle**)handle notifying: (SEL)selector
{
    NSPipe *pipe = [NSPipe pipe];
    *handle = [pipe fileHandleForReading];
    [[NSNotificationCenter defaultCenter] addObserver: self selector: selector
                                                 name: NSFileHandleReadCompletionNotification
                                               object: *handle];
    [*handle readInBackgroundAndNotifyForModes: _modes];
    return pipe;
}


- (void) _close
{
    // No need to call -closeFile on file handles obtained from NSPipe (in fact, it can hang)
    _outHandle = nil;
    _errHandle = nil;
    [[NSNotificationCenter defaultCenter] removeObserver: self 
                                                    name: NSFileHandleReadCompletionNotification
                                                  object: nil];
}


/** Subclasses can override this. */
- (NSTask*) createTask
{
    Assert(!_task,@"createTask called twice");
    NSTask *task = [[NSTask alloc] init];
    task.launchPath = _command;
    task.arguments = _arguments;
    if( _currentDirectoryPath )
        task.currentDirectoryPath = _currentDirectoryPath;
    return task;
}    


- (BOOL) start
{
    Assert(!_task, @"Task has already been run");
    if( _error )
        return NO;
    
    _task = [self createTask];
    Assert(_task,@"createTask returned nil");
    
    LogTo(MYTask,@"$ %@", self.commandLine);
    
    _task.standardOutput = [self _openPipeAndHandle: &_outHandle notifying: @selector(_gotOutput:)];
    _outputData =  [[NSMutableData alloc] init];
    _task.standardError  = [self _openPipeAndHandle: &_errHandle notifying: @selector(_gotStderr:)];
    _errorData =  [[NSMutableData alloc] init];
    
    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(_exited:)
                                                 name: NSTaskDidTerminateNotification
                                               object: _task];
    
#if DEBUG
    _startTime = CFAbsoluteTimeGetCurrent();
#endif
    
    @try{
        [_task launch];
    }@catch( id x ) {
        Warn(@"Task failed to launch: %@",x);
        _resultCode = 666;
        [self _close];
        return [self makeError: @"Exception launching %@: %@",_task.launchPath,x];
    }
    LogTo(MYTaskVerbose, @"Launched task, modes %@", _modes);
    Assert(_task.isRunning);
    _taskRunning = YES;
    self.isRunning = YES;
    
    return YES;
}


- (void) stop
{
    LogTo(MYTaskVerbose, @"Stopping task");
    [_task interrupt];
    [self _close];
    _taskRunning = NO;
    self.isRunning = NO;
}


- (BOOL) _shouldFinishUp
{
    return _isRunning && !_task.isRunning && (_ignoreOutput || (!_outHandle && !_errHandle));
}


- (void) _gotOutput: (NSNotification*)n
{
    NSData *data = [n.userInfo objectForKey: NSFileHandleNotificationDataItem];
    if( n.object == _outHandle ) {
        if( data.length > 0 ) {
            [_outHandle readInBackgroundAndNotifyForModes: _modes];
            LogTo(MYTaskVerbose, @"Got %u bytes of output",data.length);
            if( _outputData ) {
                [self willChangeValueForKey: @"output"];
                [self willChangeValueForKey: @"outputData"];
                [_outputData appendData: data];
                _output = nil;
                [self didChangeValueForKey: @"outputData"];
                [self didChangeValueForKey: @"output"];
            }
        } else {
            LogTo(MYTaskVerbose, @"Closed output");
            _outHandle = nil;
            if( [self _shouldFinishUp] )
                [self _finishUp];
        }
    }
}

- (void) _gotStderr: (NSNotification*)n
{
    if( n.object == _errHandle ) {
        NSData *data = [n.userInfo objectForKey: NSFileHandleNotificationDataItem];
        if( data.length > 0 ) {
            [_errHandle readInBackgroundAndNotifyForModes: _modes];
            LogTo(MYTaskVerbose, @"Got %u bytes of stderr",data.length);
            [self willChangeValueForKey: @"errorData"];
            [_errorData appendData: data];
            [self didChangeValueForKey: @"errorData"];
        } else {
            LogTo(MYTaskVerbose, @"Closed stderr");
            _errHandle = nil;
            if( [self _shouldFinishUp] )
                [self _finishUp];
        }
    }
}

- (void) _exited: (NSNotification*)n
{
    _resultCode = _task.terminationStatus;
    LogTo(MYTaskVerbose, @"Exited with result=%i",_resultCode);
    _taskRunning = NO;
    if( [self _shouldFinishUp] )
        [self _finishUp];
    else
        [self performSelector: @selector(_finishUp) withObject: nil afterDelay: 1.0];
}


- (void) _finishUp
{
    [NSObject cancelPreviousPerformRequestsWithTarget: self selector: @selector(_finishUp) object: nil];
    if (!_isRunning)
        return;
    
    _resultCode = _task.terminationStatus;
    [self _close];

#if DEBUG
    if (_startTime > 0.0) {
        CFAbsoluteTime elapsed = CFAbsoluteTimeGetCurrent() - _startTime;
        _startTime = 0.0;
        LogTo(MYTask, @"    Finished! (status=%i) -- %.3f sec", _resultCode, elapsed);
    } else {
        Warn(@"_finishUp called twice");
    }
#else
    LogTo(MYTaskVerbose, @"Finished! (status=%i)", _resultCode);
#endif
    
    if( _resultCode != 0 ) {
        // Handle errors:
        NSString *errStr = nil;
        if( _errorData.length > 0 )
            errStr = [[NSString alloc] initWithData: _errorData encoding: NSUTF8StringEncoding];
        LogTo(MYTask, @"    *** task returned %i: %@",_resultCode,errStr);
        if( errStr.length == 0 )
            errStr = [NSString stringWithFormat: @"Command returned status %i",_resultCode];
        NSString *desc = [NSString stringWithFormat: @"%@ command error", _task.launchPath.lastPathComponent];
        // For some reason the body text in the alert shown by -presentError: is taken from the
        // NSLocalizedRecoverySuggestionErrorKey, not the NSLocalizedFailureReasonKey...
        NSMutableDictionary *info = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                     desc,                                  NSLocalizedDescriptionKey,
                                     errStr,                                NSLocalizedRecoverySuggestionErrorKey,
                                     [NSNumber numberWithInt: _resultCode], MYTaskExitCodeKey,
                                     self,                                  MYTaskObjectKey,
                                     nil];
        self.error = [[NSError alloc] initWithDomain: MYTaskErrorDomain 
                                                code: kMYTaskError
                                            userInfo: info];
    }

    [self finished];

    self.isRunning = NO;
}

- (void) finished
{
    // This is a hook that subclasses can override to do post-processing.
}


- (BOOL) _waitTillFinishedInMode: (NSString*)runLoopMode
{
    // wait for task to exit:
    while( _task.isRunning || self.isRunning )
        if (![[NSRunLoop currentRunLoop] runMode: runLoopMode
                                      beforeDate: [NSDate dateWithTimeIntervalSinceNow: 1.0]]) {
            // This happens if both stderr and stdout are closed (leaving no NSFileHandles running
            // in this runloop mode) but the task hasn't yet notified me that it exited.
            // For some reason, in 10.6 the notification sometimes just doesn't appear, so poll
            // for it:
            if (_task.isRunning) {
                Warn(@"MYTask _waitTillFinishedInMode: no runloop sources left for %@ mode; waiting...", runLoopMode);
                sleep(1);
            } else {
                Warn(@"MYTask _waitTillFinishedInMode: Task exited without notifying!");
                [self _exited: nil];
            }
        } else
            LogTo(MYTaskVerbose, @"..._waitTillFinishedInMode still waiting...");

    return (_resultCode==0) && !_error;
}

- (BOOL) waitTillFinished
{
    return [self _waitTillFinishedInMode: _modes.lastObject];
}


- (BOOL) run
{
    [_modes addObject: MYTaskSynchronousRunLoopMode];
    return [self start] && [self _waitTillFinishedInMode: MYTaskSynchronousRunLoopMode];
    
}    


- (BOOL) run: (NSError**)outError
{
    BOOL result = [self run];
    if( outError ) *outError = self.error;
    return result;
}


@synthesize currentDirectoryPath=_currentDirectoryPath, outputData=_outputData, error=_error, isRunning=_isRunning;


- (NSString*) output
{
    if( ! _output && _outputData ) {
        _output = [[NSString alloc] initWithData: _outputData encoding: NSUTF8StringEncoding];
        // If output isn't valid UTF-8, fall back to CP1252, aka WinLatin1, a superset of ISO-Latin-1.
        if( ! _output ) {
            _output = [[NSString alloc] initWithData: _outputData encoding: NSWindowsCP1252StringEncoding];
            Warn(@"MYTask: Output of '%@' was not valid UTF-8; interpreting as CP1252",self);
        }
    }
    return _output;
}

- (NSString*) outputAndError
{
    NSString *result = self.output ?: @"";
    NSString *errorStr = nil;
    if( _error )
        errorStr = [NSString stringWithFormat: @"%@:\n%@",
                    _error.localizedDescription,_error.localizedRecoverySuggestion];
    else if( _errorData.length > 0 )
        errorStr = [[NSString alloc] initWithData: _errorData encoding: NSUTF8StringEncoding];
    if( errorStr )
        result = [NSString stringWithFormat: @"%@\n\n%@", errorStr,result];
    return result;
}

+ (NSArray*) keyPathsForValuesAffectingOutputAndError
{
    return [NSArray arrayWithObjects: @"output", @"error", @"errorData",nil];
}


@end
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.