-
Notifications
You must be signed in to change notification settings - Fork 133
/
Copy pathXCFileOperationQueue.m
executable file
·181 lines (144 loc) · 6 KB
/
XCFileOperationQueue.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
////////////////////////////////////////////////////////////////////////////////
//
// JASPER BLUES
// Copyright 2012 Jasper Blues
// All Rights Reserved.
//
// NOTICE: Jasper Blues permits you to use, modify, and distribute this file
// in accordance with the terms of the license agreement accompanying it.
//
////////////////////////////////////////////////////////////////////////////////
#import "XCFileOperationQueue.h"
#import "XcodeEditor-Prefix.pch"
@interface XCFileOperationQueue ()
- (NSString*)destinationPathFor:(NSString*)fileName inProjectDirectory:(NSString*)directory;
- (void)performFileWrites;
- (void)performCopyFrameworks;
- (void)performFileDeletions;
- (void)performCreateDirectories;
@end
@implementation XCFileOperationQueue
/* ====================================================================================================================================== */
#pragma mark - Initialization & Destruction
- (id)initWithBaseDirectory:(NSString*)baseDirectory
{
self = [super init];
if (self)
{
_baseDirectory = [baseDirectory copy];
_filesToWrite = [[NSMutableDictionary alloc] init];
_frameworksToCopy = [[NSMutableDictionary alloc] init];
_filesToDelete = [[NSMutableArray alloc] init];
_directoriesToCreate = [[NSMutableArray alloc] init];
}
return self;
}
/* ====================================================================================================================================== */
#pragma mark - Interface Methods
- (BOOL)fileWithName:(NSString*)name existsInProjectDirectory:(NSString*)directory
{
NSString* filePath = [self destinationPathFor:name inProjectDirectory:directory];
return [[NSFileManager defaultManager] fileExistsAtPath:filePath];
}
- (void)queueTextFile:(NSString*)fileName inDirectory:(NSString*)directory withContents:(NSString*)contents
{
[_filesToWrite setObject:[contents dataUsingEncoding:NSUTF8StringEncoding]
forKey:[self destinationPathFor:fileName inProjectDirectory:directory]];
}
- (void)queueDataFile:(NSString*)fileName inDirectory:(NSString*)directory withContents:(NSData*)contents
{
[_filesToWrite setObject:contents forKey:[self destinationPathFor:fileName inProjectDirectory:directory]];
}
- (void)queueFrameworkWithFilePath:(NSString*)filePath inDirectory:(NSString*)directory
{
NSURL* sourceUrl = [NSURL fileURLWithPath:filePath isDirectory:YES];
NSString* destinationPath =
[[_baseDirectory stringByAppendingPathComponent:directory] stringByAppendingPathComponent:[filePath lastPathComponent]];
NSURL* destinationUrl = [NSURL fileURLWithPath:destinationPath isDirectory:YES];
[_frameworksToCopy setObject:sourceUrl forKey:destinationUrl];
}
- (void)queueDeletion:(NSString*)filePath
{
if ( DEBUG ) printf("Queue deletion at: %s\n", filePath.UTF8String);
[_filesToDelete addObject:filePath];
}
- (void)queueDirectory:(NSString*)withName inDirectory:(NSString*)parentDirectory
{
[_directoriesToCreate addObject:[self destinationPathFor:withName inProjectDirectory:parentDirectory]];
}
- (void)commitFileOperations
{
[self performFileWrites];
[self performCopyFrameworks];
[self performFileDeletions];
[self performCreateDirectories];
}
/* ====================================================================================================================================== */
#pragma mark - Private Methods
- (NSString*)destinationPathFor:(NSString*)fileName inProjectDirectory:(NSString*)directory
{
return [[_baseDirectory stringByAppendingPathComponent:directory] stringByAppendingPathComponent:fileName];
}
- (void)performFileWrites
{
[_filesToWrite enumerateKeysAndObjectsUsingBlock:^(NSString* filePath, NSData* data, BOOL* stop)
{
NSError* error = nil;
if (![data writeToFile:filePath options:NSDataWritingAtomic error:&error])
{
[NSException raise:NSInternalInconsistencyException format:@"Error writing file at filePath: %@, error: %@", filePath, error];
}
}];
[_filesToWrite removeAllObjects];
}
- (void)performCopyFrameworks
{
[_frameworksToCopy enumerateKeysAndObjectsUsingBlock:^(NSURL* destinationUrl, NSURL* frameworkPath, BOOL* stop)
{
if ( DEBUG ) printf("$$$$$$$$$$$$$$ destination url: %s\n", destinationUrl.fileSystemRepresentation);
NSFileManager* fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:[destinationUrl path]])
{
[fileManager removeItemAtURL:destinationUrl error:nil];
}
NSError* error = nil;
if (![fileManager copyItemAtURL:frameworkPath toURL:destinationUrl error:&error])
{
[NSException raise:NSInternalInconsistencyException format:@"%@", error.localizedDescription];
}
}];
[_frameworksToCopy removeAllObjects];
}
- (void)performFileDeletions
{
for (NSString* filePath in [_filesToDelete reverseObjectEnumerator])
{
NSString* fullPath = [_baseDirectory stringByAppendingPathComponent:filePath];
NSError* error = nil;
if (![[NSFileManager defaultManager] removeItemAtPath:fullPath error:&error])
{
fprintf(stderr, "failed to remove item at path; error == %s\n", error.description.UTF8String);
[NSException raise:NSInternalInconsistencyException format:@"Error deleting file at filePath: %@", filePath];
}
else
{
if ( DEBUG ) printf("Deleted: %s\n", fullPath.UTF8String);
}
}
[_filesToDelete removeAllObjects];
}
- (void)performCreateDirectories
{
for (NSString* filePath in _directoriesToCreate)
{
NSFileManager* fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:filePath])
{
if (![fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil])
{
[NSException raise:NSInvalidArgumentException format:@"Error: Create folder failed %@", filePath];
}
}
}
}
@end