jens@20
|
1 |
//
|
jens@20
|
2 |
// MYTask.h
|
jens@20
|
3 |
// Murky
|
jens@20
|
4 |
//
|
jens@20
|
5 |
// Copyright 2008 Jens Alfke. All rights reserved.
|
jens@20
|
6 |
//
|
jens@20
|
7 |
|
jens@20
|
8 |
#import <Cocoa/Cocoa.h>
|
jens@20
|
9 |
|
jens@20
|
10 |
|
jens@20
|
11 |
extern NSString* const MYTaskErrorDomain;
|
jens@20
|
12 |
extern NSString* const MYTaskExitCodeKey;
|
jens@20
|
13 |
extern NSString* const MYTaskObjectKey;
|
jens@20
|
14 |
enum {
|
jens@20
|
15 |
kMYTaskError = 2
|
jens@20
|
16 |
};
|
jens@20
|
17 |
|
jens@20
|
18 |
|
jens@20
|
19 |
|
jens@20
|
20 |
@interface MYTask : NSObject
|
jens@20
|
21 |
{
|
jens@20
|
22 |
@private
|
jens@20
|
23 |
NSString *_command;
|
jens@20
|
24 |
NSMutableArray *_arguments;
|
jens@20
|
25 |
NSString *_currentDirectoryPath;
|
jens@20
|
26 |
NSTask *_task;
|
jens@20
|
27 |
int _resultCode;
|
jens@20
|
28 |
NSError *_error;
|
jens@20
|
29 |
BOOL _ignoreOutput;
|
jens@20
|
30 |
NSFileHandle *_outHandle, *_errHandle;
|
jens@20
|
31 |
NSMutableData *_outputData, *_errorData;
|
jens@20
|
32 |
NSString *_output;
|
jens@20
|
33 |
NSMutableArray *_modes;
|
jens@20
|
34 |
BOOL _isRunning, _taskRunning;
|
jens@20
|
35 |
}
|
jens@20
|
36 |
|
jens@20
|
37 |
- (id) initWithCommand: (NSString*)subcommand, ... NS_REQUIRES_NIL_TERMINATION;
|
jens@20
|
38 |
|
jens@20
|
39 |
/* designated initializer (subclasses can override) */
|
jens@20
|
40 |
- (id) initWithCommand: (NSString*)subcommand
|
jens@20
|
41 |
arguments: (NSArray*)arguments;
|
jens@20
|
42 |
|
jens@20
|
43 |
- (id) initWithError: (NSError*)error;
|
jens@20
|
44 |
|
jens@20
|
45 |
- (void) addArgument: (id)argument;
|
jens@20
|
46 |
- (void) addArguments: (id)arg1, ... NS_REQUIRES_NIL_TERMINATION;
|
jens@20
|
47 |
- (void) addArgumentsFromArray: (NSArray*)arguments;
|
jens@20
|
48 |
- (void) prependArguments: (id)arg1, ... NS_REQUIRES_NIL_TERMINATION;
|
jens@20
|
49 |
|
jens@20
|
50 |
- (void) ignoreOutput;
|
jens@20
|
51 |
|
jens@20
|
52 |
@property (copy) NSString* currentDirectoryPath;
|
jens@20
|
53 |
|
jens@27
|
54 |
/** Prettified description of command string. Doesn't do full shell-style quoting, though. */
|
jens@27
|
55 |
- (NSString*) commandLine;
|
jens@27
|
56 |
|
jens@20
|
57 |
- (BOOL) run;
|
jens@20
|
58 |
- (BOOL) run: (NSError**)outError;
|
jens@20
|
59 |
|
jens@20
|
60 |
- (BOOL) start;
|
jens@20
|
61 |
- (void) stop;
|
jens@20
|
62 |
- (BOOL) waitTillFinished;
|
jens@20
|
63 |
|
jens@20
|
64 |
@property (readonly,nonatomic) BOOL isRunning;
|
jens@20
|
65 |
@property (readonly,retain,nonatomic) NSError* error;
|
jens@20
|
66 |
@property (readonly,nonatomic) NSString *output, *outputAndError;
|
jens@20
|
67 |
@property (readonly,nonatomic) NSData *outputData;
|
jens@20
|
68 |
|
jens@20
|
69 |
// protected:
|
jens@20
|
70 |
|
jens@20
|
71 |
/** Subclasses can override this to add arguments or customize the task */
|
jens@20
|
72 |
- (NSTask*) createTask;
|
jens@20
|
73 |
|
jens@20
|
74 |
/** Sets the error based on the message and parameters. Always returns NO. */
|
jens@20
|
75 |
- (BOOL) makeError: (NSString*)fmt, ...;
|
jens@20
|
76 |
|
jens@20
|
77 |
/** Called when the task finishes, just before the isRunning property changes back to NO.
|
jens@20
|
78 |
You can override this to do your own post-processing. */
|
jens@20
|
79 |
- (void) finished;
|
jens@20
|
80 |
|
jens@20
|
81 |
@end
|