Repository navigation
Expand file tree
/
Copy pathVWKRunOperation.m
More file actions
222 lines (190 loc) · 10.2 KB
/
Copy pathVWKRunOperation.m
File metadata and controls
222 lines (190 loc) · 10.2 KB
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
//
// CCPRunOperation.m
//
// Copyright (c) 2013 Delisa Mason. http://delisa.me
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
#import "VWKRunOperation.h"
@interface VWKRunOperation ()
{
BOOL isExecuting;
BOOL isFinished;
}
@property (retain) VWKXCodeConsole *xcodeConsole;
@property (retain) NSTask *task;
@property (retain) id taskStandardOutDataAvailableObserver;
@property (retain) id taskStandardErrorDataAvailableObserver;
@property (retain) id taskTerminationObserver;
@end
@implementation VWKRunOperation
#pragma mark -
#pragma mark NSOperation
- (id)initWithTask:(NSTask *)task
{
self = [super init];
if (self) {
self.xcodeConsole = [VWKXCodeConsole consoleForKeyWindow];
self.task = task;
}
return self;
}
- (BOOL)isExecuting
{
return isExecuting;
}
- (void)setIsExecuting:(BOOL)_isExecuting
{
[self willChangeValueForKey:@"isExecuting"];
isExecuting = _isExecuting;
[self didChangeValueForKey:@"isExecuting"];
}
- (BOOL)isFinished
{
return isFinished;
}
- (void)setIsFinished:(BOOL)_isFinished
{
[self willChangeValueForKey:@"isFinished"];
isFinished = _isFinished;
[self didChangeValueForKey:@"isFinished"];
}
- (void)start
{
if (self.isCancelled) {
self.isFinished = YES;
return;
}
if (!NSThread.isMainThread) {
[self performSelector:@selector(start) onThread:NSThread.mainThread withObject:nil waitUntilDone:NO];
return;
}
self.isExecuting = YES;
[self main];
}
- (void)main
{
if (self.isCancelled) {
self.isExecuting = NO;
self.isFinished = YES;
} else {
[self runOperation];
}
}
#pragma mark -
#pragma mark NSTask
- (void)runOperation
{
@try
{
NSPipe *standardOutputPipe = NSPipe.pipe;
self.task.standardOutput = standardOutputPipe;
NSPipe *standardErrorPipe = NSPipe.pipe;
self.task.standardError = standardErrorPipe;
NSFileHandle *standardOutputFileHandle = standardOutputPipe.fileHandleForReading;
NSFileHandle *standardErrorFileHandle = standardErrorPipe.fileHandleForReading;
__block NSMutableString *standardOutputBuffer = [NSMutableString string];
__block NSMutableString *standardErrorBuffer = [NSMutableString string];
self.taskStandardOutDataAvailableObserver = [NSNotificationCenter.defaultCenter addObserverForName:NSFileHandleDataAvailableNotification
object:standardOutputFileHandle queue:NSOperationQueue.mainQueue
usingBlock: ^(NSNotification *notification) {
NSFileHandle *fileHandle = notification.object;
NSString *data = [[NSString alloc] initWithData:fileHandle.availableData encoding:NSUTF8StringEncoding];
if (data.length > 0) {
[standardOutputBuffer appendString:data];
[fileHandle waitForDataInBackgroundAndNotify];
standardOutputBuffer = [self writePipeBuffer:standardOutputBuffer];
} else {
[self appendLine:standardOutputBuffer];
[NSNotificationCenter.defaultCenter removeObserver:self.taskStandardOutDataAvailableObserver];
self.taskStandardOutDataAvailableObserver = nil;
[self checkAndSetFinished];
}
}];
self.taskStandardErrorDataAvailableObserver = [NSNotificationCenter.defaultCenter addObserverForName:NSFileHandleDataAvailableNotification
object:standardErrorFileHandle queue:NSOperationQueue.mainQueue
usingBlock: ^(NSNotification *notification) {
NSFileHandle *fileHandle = notification.object;
NSString *data = [[NSString alloc] initWithData:fileHandle.availableData encoding:NSUTF8StringEncoding];
if (data.length > 0) {
[standardErrorBuffer appendString:data];
[fileHandle waitForDataInBackgroundAndNotify];
standardErrorBuffer = [self writePipeBuffer:standardErrorBuffer];
} else {
[self appendLine:standardErrorBuffer];
[NSNotificationCenter.defaultCenter removeObserver:self.taskStandardErrorDataAvailableObserver];
self.taskStandardErrorDataAvailableObserver = nil;
[self checkAndSetFinished];
}
}];
self.taskTerminationObserver = [NSNotificationCenter.defaultCenter addObserverForName:NSTaskDidTerminateNotification
object:self.task queue:NSOperationQueue.mainQueue
usingBlock: ^(NSNotification *notification) {
[NSNotificationCenter.defaultCenter removeObserver:self.taskTerminationObserver];
self.taskTerminationObserver = nil;
self.task = nil;
[self checkAndSetFinished];
}];
[standardOutputFileHandle waitForDataInBackgroundAndNotify];
[standardErrorFileHandle waitForDataInBackgroundAndNotify];
[self.xcodeConsole appendText:[NSString stringWithFormat:@"%@ %@\n\n", self.task.launchPath, [self.task.arguments componentsJoinedByString:@" "]]];
[self.task launch];
}
@catch (NSException *exception)
{
[self.xcodeConsole appendText:exception.description color:NSColor.redColor];
[self.xcodeConsole appendText:@"\n"];
self.isExecuting = NO;
self.isFinished = YES;
}
}
- (NSMutableString *)writePipeBuffer:(NSMutableString *)buffer
{
NSArray *lines = [buffer componentsSeparatedByCharactersInSet:NSCharacterSet.newlineCharacterSet];
if (lines.count > 1) {
for (int i = 0; i < lines.count - 1; i++) {
NSString *line = lines[i];
[self appendLine:line];
}
return [lines[lines.count - 1] mutableCopy];
}
return buffer;
}
- (void)appendLine:(NSString *)line
{
NSColor *color;
if ([line hasPrefix:@"ERROR"])
color = NSColor.redColor;
else if ([line hasPrefix:@"WARNING"])
color = NSColor.orangeColor;
else if ([line hasPrefix:@"DEBUG"])
color = NSColor.grayColor;
else if ([line hasPrefix:@"INFO"])
color = [NSColor colorWithDeviceRed:0.0 green:0.5 blue:0.0 alpha:1.0];
[self.xcodeConsole appendText:[line stringByAppendingString:@"\n"] color:color];
}
- (void)checkAndSetFinished
{
if (self.taskStandardOutDataAvailableObserver == nil
&& self.taskStandardErrorDataAvailableObserver == nil
&& self.task == nil) {
self.isExecuting = NO;
self.isFinished = YES;
}
}
@end