-
Notifications
You must be signed in to change notification settings - Fork 1
/
RefactoParser.m
369 lines (291 loc) · 9.51 KB
/
RefactoParser.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
//
// Renamed.m
// refacto
#import "RefactoParser.h"
@interface NSString (PathCompare)
- (NSComparisonResult)componentCountCompare:(NSString *)aString;
@end
@implementation NSString (PathCompare)
- (NSComparisonResult)componentCountCompare:(NSString *)aString
{
int result = NSOrderedSame;
if ([[self pathComponents] count] > [[aString pathComponents] count]) {
result = NSOrderedAscending;
} else if ([[self pathComponents] count] < [[aString pathComponents] count]) {
result = NSOrderedDescending;
} else {
if (![[self pathExtension] isEqualToString:@""] &&
[[aString pathExtension] isEqualToString:@""]) {
result = NSOrderedAscending;
} else if ([[self pathExtension] isEqualToString:@""] &&
![[aString pathExtension] isEqualToString:@""]) {
result = NSOrderedDescending;
}
}
return result;
}
@end
@interface RefactoParser (Private)
-(NSString *)tutorialString;
-(void)refactor:(NSString *)file;
-(void)rename:(NSString *)file;
-(BOOL)askWithQuestion:(NSString*)question forFile:(NSString *)file;
-(BOOL)shouldAsk;
-(BOOL)shouldChange:(NSString *)fileExt;
-(BOOL)shouldBeCaseInsensitive;
-(BOOL)shouldBeNoFolder;
@end
@implementation RefactoParser
#pragma mark -
#pragma mark Constants
static NSString * const kNoFolderString = @"-nf";
static NSString * const kCaseInsensitiveString = @"-ci";
static NSString * const kAlwaysAskString = @"-a";
static NSString * const kShowHelpString1 = @"-h";
static NSString * const kShowHelpString2 = @"-help";
#pragma mark -
#pragma mark Properties
@synthesize path;
@synthesize s1, s2;
@synthesize options, extensions;
@synthesize changes;
#pragma mark -
#pragma mark Public Initializers
-(id)initWithArgc:(int)argc argv:(const char *[])argv
{
NSString *secondParam = [NSString stringWithFormat:@"%s",argv[1]];
if (argc > 1 &&
([secondParam isEqualToString:kShowHelpString1] ||
[secondParam isEqualToString:kShowHelpString2]) ) {
NSLog(@"%@", [self tutorialString]);
return nil;
}
if (argc < 4) {
NSLog(@"%@", [self tutorialString]);
return nil;
} else {
self = [super init];
if (self) {
self.path = [[NSFileManager defaultManager] currentDirectoryPath];
self.options = [NSMutableArray array];
self.extensions = [NSMutableArray array];
self.changes = [NSMutableArray array];
//Setup Options and Extensions
for (int i = 1; i < argc; i++) {
NSString *arg = [NSString stringWithFormat:@"%s", argv[i]];
if (i == 1 || i == 2) {
switch (i) {
case 1:
self.s1 = arg;
break;
case 2:
self.s2 = arg;
break;
}
} else if ([arg caseInsensitiveCompare:kNoFolderString] == NSOrderedSame ||
[arg caseInsensitiveCompare:kCaseInsensitiveString] == NSOrderedSame ||
[arg caseInsensitiveCompare:kAlwaysAskString] == NSOrderedSame) {
[options addObject:[arg lowercaseString]];
} else {
[extensions addObject:arg];
}
}
}
return self;
}
}
#pragma mark -
#pragma mark Public General Methods
-(void)parse
{
NSFileManager *manager = [NSFileManager defaultManager];
NSArray *allItems =
[[[manager enumeratorAtPath:self.path] allObjects]
sortedArrayUsingSelector:@selector(componentCountCompare:)];
NSEnumerator *dEnum = [allItems reverseObjectEnumerator];
NSString *file;
NSLog(@"Refactoring...");
while (file = [dEnum nextObject]) {
[self refactor:file];
}
dEnum = [allItems reverseObjectEnumerator];
NSLog(@"Renaming...");
while (file = [dEnum nextObject]) {
[self rename:file];
}
}
#pragma mark -
#pragma mark Private General Methods
-(void)refactor:(NSString *)file
{
NSFileManager *manager = [NSFileManager defaultManager];
NSError *error = nil;
int compareOption = 0;
BOOL isDir = NO;
if ([self shouldBeCaseInsensitive]) {
compareOption = NSCaseInsensitiveSearch;
}
if ([manager fileExistsAtPath:file isDirectory:&isDir] &&
!isDir &&
[self shouldChange:[file pathExtension]]) {
NSString *fileContent =
[NSString stringWithContentsOfFile:file
encoding:NSUTF8StringEncoding
error:&error];
if (error || !fileContent) {
NSLog(@"Unable to change %@ because:\n*****%@\n*****",
file, error);
} else {
NSRange wholeFile = NSMakeRange(0, [fileContent length]);
NSString *newFile =
[fileContent stringByReplacingOccurrencesOfString:self.s1
withString:self.s2
options:compareOption
range:wholeFile];
if ([self shouldAsk]) {
if ([self askWithQuestion:@"Refactor" forFile:file])
[newFile writeToFile:file
atomically:NO
encoding:NSUTF8StringEncoding
error:&error];
} else {
[newFile writeToFile:file
atomically:NO
encoding:NSUTF8StringEncoding
error:&error];
}
}
if (error) {
NSLog(@"Failed to refactor %@ because:\n*****%@\n*****",
file, error);
}
}
}
-(void)rename:(NSString *)file
{
NSFileManager *manager = [NSFileManager defaultManager];
NSString *newFile;
int compareOption = 0;
BOOL isDir = NO;
NSError *error = nil;
if ([self shouldBeCaseInsensitive]) {
compareOption = NSCaseInsensitiveSearch;
}
if ([[file pathExtension] isEqualToString:@""]) {
newFile =
[file stringByReplacingOccurrencesOfString:self.s1
withString:self.s2
options:compareOption
range:[file rangeOfString:[file lastPathComponent]
options:NSBackwardsSearch]];
} else {
NSRange extRange = [file rangeOfString:[file pathExtension]
options:NSBackwardsSearch];
NSRange lastCompRange = [file rangeOfString:[file lastPathComponent] options:NSBackwardsSearch];
NSRange replaceRange = NSMakeRange([file length] - lastCompRange.length,
lastCompRange.length - extRange.length);
newFile = [file stringByReplacingOccurrencesOfString:self.s1
withString:self.s2
options:compareOption
range:replaceRange];
}
if ([manager fileExistsAtPath:file isDirectory:&isDir] &&
![file isEqualToString:newFile]) {
if (isDir && ![self shouldBeNoFolder]) {
if ([self shouldAsk]) {
if ([self askWithQuestion:@"Rename" forFile:file])
[manager moveItemAtPath:file toPath:newFile error:&error];
} else {
[manager moveItemAtPath:file toPath:newFile error:&error];
}
} else if (!isDir && [self shouldChange:[file pathExtension]]) {
if ([self shouldAsk]) {
if ([self askWithQuestion:@"Rename" forFile:file])
[manager moveItemAtPath:file toPath:newFile error:&error];
} else {
[manager moveItemAtPath:file toPath:newFile error:&error];
}
}
if (error) {
NSLog(@"Failed to Rename %@ to %@ because:\n*****%@\n*****", file, newFile, error);
}
}
}
-(NSString *)tutorialString
{
return (@"\n\nProper Usage is: refacto findString changeString <file extensions> <options>\n\n"
"File extensions are not optional. \nIn order for any files to be renamed "
"or refactored, you must list at least one file extension.\n\n"
"Options:\n"
"\t-a : Ask for confirmation to refactor or rename a file.\n"
"\t-nf : Do not rename folders or subfolders\n"
"\t-ci : Case Insenitive. Will change all versions of findString\n\n");
}
-(BOOL)shouldChange:(NSString *)fileExt
{
BOOL shouldChange = NO;
for (NSString *ext in self.extensions) {
if ([fileExt isEqualToString:ext]) {
shouldChange = YES;
break;
}
}
return shouldChange;
}
-(BOOL)askWithQuestion:(NSString*)question forFile:(NSString *)file
{
BOOL change = NO;
BOOL keepAsking = YES;
int size = 2;
while (keepAsking) {
char input[size];
NSLog(@"%@ %@? (Y|y,N|n): ", question, file);
fgets(input, size, stdin);
input[1] = '\0';
if (input[0] == 'y' || input[0] == 'Y') {
change = YES;
keepAsking = NO;
} else if (input[0] == 'n' || input[0] == 'N') {
keepAsking = NO;
} else {
NSLog(@"Invalid answer.");
}
int flush;
while ((flush = getchar()) != '\n' && flush != EOF);
}
return change;
}
-(BOOL)shouldAsk
{
BOOL ask = NO;
for (NSString *option in self.options) {
if ([option isEqualToString:kAlwaysAskString]) {
ask = YES;
break;
}
}
return ask;
}
-(BOOL)shouldBeCaseInsensitive
{
BOOL ci = NO;
for (NSString *option in self.options) {
if ([option isEqualToString:kCaseInsensitiveString]) {
ci = YES;
break;
}
}
return ci;
}
-(BOOL)shouldBeNoFolder
{
BOOL nf = NO;
for (NSString *option in self.options) {
if ([option isEqualToString:kNoFolderString]) {
nf = YES;
break;
}
}
return nf;
}
@end