-
Notifications
You must be signed in to change notification settings - Fork 2
/
ScriptDocument.m
622 lines (511 loc) · 17 KB
/
ScriptDocument.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
//
// ScriptDocument.m
// Pipe
//
// Created by RenŽ Puls on 18.01.05.
// Copyright 2005 RenŽ Puls. All rights reserved.
//
#import "ScriptDocument.h"
#import "ScriptDocument+Running.h"
#import "ScriptVariable.h"
#import "UserDefaults.h"
#import "ScriptSession.h"
#import "SupportFileReference.h"
static NSString * const VariableChangeContext = @"VariableChangeContext";
NSString * const ScriptDocumentErrorNotification = @"ScriptDocumentErrorNotification";
NSString * const ScriptDocumentScriptDidTerminateNotification = @"ScriptDocumentScriptDidTerminateNotification";
NSString * const ScriptDocumentWillRunNotification = @"ScriptDocumentWillRunNotification";
NSString * const ScriptDocumentWillSaveNotification = @"ScriptDocumentWillSaveNotification";
NSString * const ScriptDocumentWillRevertNotification = @"ScriptDocumentWillRevertNotification";
@implementation ScriptDocument
#pragma mark Init and Cleanup
- (id)init
{
NSStringEncoding defaultStringEncoding;
if ((self = [super init])) {
variables = [[NSMutableArray alloc] init];
supportFiles = [[NSMutableArray alloc] init];
[[self undoManager] disableUndoRegistration];
defaultStringEncoding = [JGUserDefaultValue(PipeTextEncodingDefaultKey) unsignedIntValue];
[self setOutputType:ScriptTextOutputType];
[self setScriptStringEncoding:defaultStringEncoding];
[self setInputStringEncoding:defaultStringEncoding];
[self setOutputStringEncoding:defaultStringEncoding];
[self setServiceCapable:YES];
[[self undoManager] enableUndoRegistration];
}
return self;
}
- (void)dealloc
{
NSEnumerator *keyEnumerator;
NSString *currentKey;
NSIndexSet *indexSet;
keyEnumerator = [[ScriptVariable observableKeys] objectEnumerator];
indexSet = [[NSIndexSet alloc] initWithIndexesInRange:NSMakeRange(0, [variables count])];
while ((currentKey = [keyEnumerator nextObject])) {
[variables removeObserver:self fromObjectsAtIndexes:indexSet forKeyPath:currentKey];
}
[indexSet release];
[[NSNotificationCenter defaultCenter] removeObserver:self];
[variables release];
[supportFiles release];
[script release];
[input release];
[inputData release];
[output release];
[outputData release];
[errors release];
[runCommand release];
[argumentString release];
[currentSession release];
[usageInfo release];
[homeURL release];
[preferredScriptFileName release];
[super dealloc];
}
#pragma mark Accessors
- (NSString *)script
{
return script;
}
- (void)setScript:(NSString *)newScript
{
if (script != newScript) {
[[self undoManager] registerUndoWithTarget:self selector:@selector(setScript:) object:[[script copy] autorelease]];
[script release];
script = [newScript copy];
}
}
- (NSData *)scriptData
{
return [[[self script] stringWithLineEndings:JGLineEndingsTypeUNIX] dataUsingEncoding:[self scriptStringEncoding]];
}
- (NSString *)input
{
return input;
}
- (void)setInput:(NSString *)newInput
{
if (input != newInput) {
[[self undoManager] registerUndoWithTarget:self selector:@selector(setInput:) object:[[input copy] autorelease]];
[input release];
input = [newInput copy];
[self setInputData:nil];
}
}
- (NSData *)inputData
{
if ((inputData == nil) && (input != nil)) {
inputData = [[[[self input] stringWithLineEndings:[self inputLineEndings]] dataUsingEncoding:[self inputStringEncoding]] retain];
}
return inputData;
}
- (void)setInputData:(NSData *)newData
{
if (newData != inputData) {
[inputData release];
inputData = [newData copy];
}
}
- (NSString *)output
{
if ((output == nil) && (outputData != nil)) {
output = [[NSString alloc] initWithData:[self outputData] encoding:[self outputStringEncoding]];
if (output == nil) {
output = [[NSString stringByGuessingEncodingOfData:[self outputData]] retain];
}
}
return output;
}
- (void)setOutput:(NSString *)newOutput
{
if (output != newOutput) {
[output release];
output = [newOutput copy];
}
}
- (NSData *)outputData
{
return outputData;
}
- (void)setOutputData:(NSData *)newData
{
if (outputData != newData) {
[outputData release];
outputData = [newData copy];
[self setOutput:nil];
}
}
- (ScriptOutputType)outputType
{
return outputType;
}
- (void)setOutputType:(ScriptOutputType)newType
{
if (newType != outputType) {
[[[self undoManager] prepareWithInvocationTarget:self] setOutputType:outputType];
outputType = newType;
}
}
- (NSString *)errors
{
return errors;
}
- (void)setErrors:(NSString *)newString
{
if (newString != errors) {
[errors release];
errors = [newString copy];
}
}
- (NSString *)argumentString
{
return argumentString;
}
- (void)setArgumentString:(NSString *)newString
{
if (argumentString != newString) {
[[self undoManager] registerUndoWithTarget:self selector:@selector(setArgumentString:) object:argumentString];
[argumentString release];
argumentString = [newString copy];
}
}
- (NSString *)runCommand
{
return runCommand;
}
- (void)setRunCommand:(NSString *)newString
{
if (newString != runCommand) {
[[self undoManager] registerUndoWithTarget:self selector:@selector(setRunCommand:) object:runCommand];
[runCommand release];
runCommand = [newString copy];
}
}
- (BOOL)executesDirectly
{
return executesDirectly;
}
- (void)setExecutesDirectly:(BOOL)flag
{
if (flag != executesDirectly) {
[[[self undoManager] prepareWithInvocationTarget:self] setExecutesDirectly:executesDirectly];
executesDirectly = flag;
}
}
- (BOOL)isReverseTransformation
{
return reverseTransformation;
}
- (void)setReverseTransformation:(BOOL)flag
{
if (flag != reverseTransformation) {
[[[self undoManager] prepareWithInvocationTarget:self] setReverseTransformation:reverseTransformation];
reverseTransformation = flag;
}
}
- (NSAttributedString *)usageInfo
{
return usageInfo;
}
- (void)setUsageInfo:(NSAttributedString *)newString
{
if (newString != usageInfo) {
[[self undoManager] registerUndoWithTarget:self selector:@selector(setUsageInfo:) object:usageInfo];
[usageInfo release];
usageInfo = [newString copy];
}
}
- (NSURL *)homeURL
{
return homeURL;
}
- (void)setHomeURL:(NSURL *)newURL
{
if (newURL != homeURL) {
[[self undoManager] registerUndoWithTarget:self selector:@selector(setHomeURL:) object:homeURL];
[homeURL release];
homeURL = [newURL copy];
}
}
- (NSFileWrapper *)supportDirectoryWrapper
{
NSFileWrapper *directoryWrapper, *fileWrapper;
NSMutableDictionary *fileWrappers;
NSEnumerator *referenceEnumerator;
SupportFileReference *currentReference;
// Build a directory wrapper from all file wrappers
fileWrappers = [[NSMutableDictionary alloc] init];
referenceEnumerator = [[self valueForKey:@"supportFiles"] objectEnumerator];
while ((currentReference = [referenceEnumerator nextObject])) {
fileWrapper = [currentReference fileWrapper];
[fileWrappers setObject:fileWrapper forKey:[fileWrapper preferredFilename]];
}
directoryWrapper = [[[NSFileWrapper alloc] initDirectoryWithFileWrappers:fileWrappers] autorelease];
[directoryWrapper setPreferredFilename:@"Support Files"];
[fileWrappers release];
return directoryWrapper;
}
- (JGLineEndingsType)inputLineEndings
{
return [JGUserDefaultValue(PipeInputLineEndingsConversionDefaultKey) intValue];
}
- (NSStringEncoding)scriptStringEncoding
{
return scriptStringEncoding;
}
- (void)setScriptStringEncoding:(NSStringEncoding)newScriptStringEncoding
{
if (newScriptStringEncoding != scriptStringEncoding) {
[[[self undoManager] prepareWithInvocationTarget:self] setScriptStringEncoding:newScriptStringEncoding];
scriptStringEncoding = newScriptStringEncoding;
}
}
- (NSStringEncoding)inputStringEncoding
{
return inputStringEncoding;
}
- (void)setInputStringEncoding:(NSStringEncoding)newInputStringEncoding
{
if (inputStringEncoding != newInputStringEncoding) {
[[[self undoManager] prepareWithInvocationTarget:self] setInputStringEncoding:newInputStringEncoding];
inputStringEncoding = newInputStringEncoding;
if ([self input] != nil)
[self setInputData:nil];
}
}
- (NSStringEncoding)outputStringEncoding
{
return outputStringEncoding;
}
- (void)setOutputStringEncoding:(NSStringEncoding)newOutputStringEncoding
{
if (outputStringEncoding != newOutputStringEncoding) {
[[[self undoManager] prepareWithInvocationTarget:self] setOutputStringEncoding:newOutputStringEncoding];
outputStringEncoding = newOutputStringEncoding;
}
}
- (NSStringEncoding)errorStringEncoding
{
return NSUTF8StringEncoding;
}
- (NSWritingDirection)inputWritingDirection
{
return inputWritingDirection;
}
- (void)setInputWritingDirection:(NSWritingDirection)newDirection
{
inputWritingDirection = newDirection;
}
- (NSWritingDirection)outputWritingDirection
{
return outputWritingDirection;
}
- (void)setOutputWritingDirection:(NSWritingDirection)newDirection
{
outputWritingDirection = newDirection;
}
- (BOOL)isServiceCapable
{
return serviceCapable;
}
- (void)setServiceCapable:(BOOL)flag
{
if (flag != serviceCapable) {
[[[self undoManager] prepareWithInvocationTarget:self] setServiceCapable:serviceCapable];
serviceCapable = flag;
}
}
- (ScriptSession *)currentSession
{
return currentSession;
}
- (void)setCurrentSession:(ScriptSession *)newSession
{
if (newSession != currentSession) {
if ([currentSession scriptTask]) {
[[NSNotificationCenter defaultCenter] removeObserver:self name:nil object:[currentSession scriptTask]];
[[self currentSession] abort];
}
[currentSession release];
currentSession = [newSession retain];
if (currentSession != nil) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(scriptTaskDidTerminate:) name:JGBufferedTaskDidTerminateNotification object:[currentSession scriptTask]];
}
}
}
- (NSString *)preferredScriptFileName
{
return preferredScriptFileName;
}
- (void)setPreferredScriptFileName:(NSString *)newName
{
if (newName != preferredScriptFileName) {
[[self undoManager] registerUndoWithTarget:self selector:@selector(setPreferredScriptFileName:) object:preferredScriptFileName];
[preferredScriptFileName release];
preferredScriptFileName = [newName copy];
}
}
#pragma mark Variables
- (unsigned int)countOfVariables
{
return [variables count];
}
- (id)objectInVariablesAtIndex:(unsigned int)anIndex
{
return [variables objectAtIndex:anIndex];
}
- (void)insertObject:(id)anObject inVariablesAtIndex:(unsigned int)anIndex
{
NSEnumerator *keyEnumerator;
NSString *currentKey;
[[[self undoManager] prepareWithInvocationTarget:self] removeObjectFromVariablesAtIndex:anIndex];
[variables insertObject:anObject atIndex:anIndex];
keyEnumerator = [[ScriptVariable observableKeys] objectEnumerator];
while ((currentKey = [keyEnumerator nextObject])) {
[variables addObserver:self toObjectsAtIndexes:[NSIndexSet indexSetWithIndex:anIndex] forKeyPath:currentKey options:(NSKeyValueObservingOptionOld|NSKeyValueObservingOptionNew) context:VariableChangeContext];
}
if (![[self undoManager] isUndoing] && ![[self undoManager] isRedoing])
[[self undoManager] setActionName:NSLocalizedString(@"Add Variable", @"Undo action name")];
}
- (void)removeObjectFromVariablesAtIndex:(unsigned int)anIndex
{
id anObject = [variables objectAtIndex:anIndex];
NSEnumerator *keyEnumerator;
NSString *currentKey;
keyEnumerator = [[ScriptVariable observableKeys] objectEnumerator];
while ((currentKey = [keyEnumerator nextObject])) {
[variables removeObserver:self fromObjectsAtIndexes:[NSIndexSet indexSetWithIndex:anIndex] forKeyPath:currentKey];
}
[[[self undoManager] prepareWithInvocationTarget:self] insertObject:anObject inVariablesAtIndex:anIndex];
[variables removeObjectAtIndex:anIndex];
if (![[self undoManager] isUndoing] && ![[self undoManager] isRedoing])
[[self undoManager] setActionName:NSLocalizedString(@"Delete Variable", @"Undo action name")];
}
#pragma mark Support Files
- (unsigned int)countOfSupportFiles
{
return [supportFiles count];
}
- (id)objectInSupportFilesAtIndex:(unsigned int)index
{
return [supportFiles objectAtIndex:index];
}
- (void)insertObject:(id)anObject inSupportFilesAtIndex:(unsigned int)index
{
[[[self undoManager] prepareWithInvocationTarget:self] removeObjectFromSupportFilesAtIndex:index];
[supportFiles insertObject:anObject atIndex:index];
}
- (void)removeObjectFromSupportFilesAtIndex:(unsigned int)index
{
[[[self undoManager] prepareWithInvocationTarget:self] insertObject:[supportFiles objectAtIndex:index] inVariablesAtIndex:index];
[supportFiles removeObjectAtIndex:index];
}
- (void)updateSupportFiles
{
[[self valueForKey:@"supportFiles"] makeObjectsPerformSelector:@selector(updateFromSource)];
}
#pragma mark KVO
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
id oldValue = [change objectForKey:NSKeyValueChangeOldKey];
NSDictionary *undoDict;
static NSDictionary *keyToUndoActionDict = nil;
if (keyToUndoActionDict == nil) {
keyToUndoActionDict = [[NSDictionary alloc] initWithObjectsAndKeys:
@"Change Variable Name", @"name",
@"Change Variable Value", @"value",
@"Change Variable Enum Values", @"possibleValues",
@"Change Variable Type", @"type",
@"Change Variable Sensitive Flag", @"sensitive",
nil];
}
if (context == VariableChangeContext) {
undoDict = [NSDictionary dictionaryWithObject:oldValue forKey:keyPath];
[[self undoManager] registerUndoWithTarget:object selector:@selector(setValuesForKeysWithDictionary:) object:undoDict];
if (![[self undoManager] isUndoing] && ![[self undoManager] isRedoing] && ([keyToUndoActionDict objectForKey:keyPath] != nil))
[[self undoManager] setActionName:[keyToUndoActionDict objectForKey:keyPath]];
[self autoRunScript:self];
}
}
#pragma mark NSCopying and Equality
- (id)copyWithZone:(NSZone *)newZone
{
ScriptDocument *newInstance;
newInstance = [[[self class] allocWithZone:newZone] init];
[newInstance setScriptStringEncoding:[self scriptStringEncoding]];
[newInstance setInputStringEncoding:[self inputStringEncoding]];
[newInstance setOutputStringEncoding:[self outputStringEncoding]];
[newInstance setScript:[self script]];
[newInstance setInput:[self input]];
[newInstance setOutputData:[self outputData]];
[newInstance setErrors:[self errors]];
[newInstance setOutputType:[self outputType]];
[newInstance setArgumentString:[self argumentString]];
[newInstance setRunCommand:[self runCommand]];
[newInstance setUsageInfo:[self usageInfo]];
[newInstance setHomeURL:[self homeURL]];
[newInstance setExecutesDirectly:[self executesDirectly]];
[newInstance setReverseTransformation:[self isReverseTransformation]];
[newInstance setServiceCapable:[self isServiceCapable]];
[newInstance setPreferredScriptFileName:[self preferredScriptFileName]];
[[newInstance mutableArrayValueForKey:@"variables"] setArray:[self valueForKey:@"variables"]];
[[newInstance mutableArrayValueForKey:@"supportFiles"] setArray:[self valueForKey:@"supportFiles"]];
return newInstance;
}
- (BOOL)isEqual:(id)otherObject
{
if (otherObject == self)
return YES;
if (![otherObject isMemberOfClass:[self class]])
return NO;
if ([otherObject scriptStringEncoding] != [self scriptStringEncoding])
return NO;
if ([otherObject inputStringEncoding] != [self inputStringEncoding])
return NO;
if ([otherObject outputStringEncoding] != [self outputStringEncoding])
return NO;
if ([otherObject script] != [self script])
if (![[otherObject script] isEqualToString:[self script]])
return NO;
if ([otherObject input] != [self input])
if (![[otherObject input] isEqualToString:[self input]])
return NO;
if ([otherObject outputData] != [self outputData])
if (![[otherObject outputData] isEqualToData:[self outputData]])
return NO;
if ([otherObject errors] != [self errors])
if (![[otherObject errors] isEqualToString:[self errors]])
return NO;
if ([otherObject outputType] != [self outputType])
return NO;
if ([otherObject argumentString] != [self argumentString])
if (![[otherObject argumentString] isEqualToString:[self argumentString]])
return NO;
if ([otherObject runCommand] != [self runCommand])
if (![[otherObject runCommand] isEqualToString:[self runCommand]])
return NO;
if ([otherObject usageInfo] != [self usageInfo])
if (![[otherObject usageInfo] isEqualToAttributedString:[self usageInfo]])
return NO;
if ([otherObject homeURL] != [self homeURL])
if (![[otherObject homeURL] isEqual:[self homeURL]])
return NO;
if ([otherObject executesDirectly] != [self executesDirectly])
return NO;
if ([otherObject isReverseTransformation] != [self isReverseTransformation])
return NO;
if ([otherObject isServiceCapable] != [self isServiceCapable])
return NO;
if ([otherObject supportDirectoryWrapper] != [self supportDirectoryWrapper])
if (![[[otherObject supportDirectoryWrapper] serializedRepresentation] isEqualToData:[[self supportDirectoryWrapper] serializedRepresentation]])
return NO;
if ([otherObject preferredScriptFileName] != [self preferredScriptFileName])
if (![[otherObject preferredScriptFileName] isEqualToString:[self preferredScriptFileName]])
return NO;
if (![[otherObject valueForKey:@"variables"] isEqualToArray:[self valueForKey:@"variables"]])
return NO;
return YES;
}
@end