forked from Polidea/ios-class-guard
-
Notifications
You must be signed in to change notification settings - Fork 3
/
CDCoreDataModelProcessor.m
69 lines (51 loc) · 2.5 KB
/
CDCoreDataModelProcessor.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
#import "CDCoreDataModelProcessor.h"
#import "CDCoreDataModelParser.h"
@implementation CDCoreDataModelProcessor
- (NSArray *)coreDataModelSymbolsToExclude {
NSMutableArray *coreDataModelSymbols = [NSMutableArray array];
CDCoreDataModelParser *parser = [[CDCoreDataModelParser alloc] init];
__weak CDCoreDataModelProcessor *weakSelf = self;
void (^modelCallback)(NSURL *) = ^(NSURL *url){
NSLog(@"Fetching symbols from CoreData model at path %@", url);
[coreDataModelSymbols addObjectsFromArray:[parser symbolsInData:[NSData dataWithContentsOfURL:url]]];
};
void (^xcdatamodelCallback)(NSURL *) = ^(NSURL *url){
[weakSelf findFileWithSuffix:@"contents" inDirectoryURL:url foundCallback:modelCallback];
};
void (^xcdatamodeldCallback)(NSURL *) = ^(NSURL *url){
[weakSelf findDirectoryWithSuffix:@".xcdatamodel/" inDirectoryURL:url foundCallback:xcdatamodelCallback];
};
[self findDirectoryWithSuffix:@".xcdatamodeld/" inDirectoryURL:[NSURL URLWithString:@"."] foundCallback:xcdatamodeldCallback];
return coreDataModelSymbols;
}
- (void)findFileWithSuffix:(NSString *)string inDirectoryURL:(NSURL *)URL foundCallback:(void (^)(NSURL *))callback {
[self findFilesOrDirectoryWithString:string isDirectory:NO URL:URL callback:callback];
}
- (void)findDirectoryWithSuffix:(NSString *)string inDirectoryURL:(NSURL *)URL foundCallback:(void (^)(NSURL *))callback {
[self findFilesOrDirectoryWithString:string isDirectory:YES URL:URL callback:callback];
}
- (void)findFilesOrDirectoryWithString:(NSString *)string isDirectory:(BOOL)directory URL:(NSURL *)URL callback:(void (^)(NSURL *))callback {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *keys = [NSArray arrayWithObject:NSURLIsDirectoryKey];
NSDirectoryEnumerator *enumerator = [fileManager
enumeratorAtURL:URL
includingPropertiesForKeys:keys
options:0
errorHandler:^(NSURL *url, NSError *error) {
// Handle the error.
// Return YES if the enumeration should continue after the error.
return YES;
}];
for (NSURL *url in enumerator) {
NSError *error;
NSNumber *isDirectory = nil;
if ([url getResourceValue:&isDirectory forKey:NSURLIsDirectoryKey error:&error] && [isDirectory boolValue] == directory) {
if ([url.absoluteString hasSuffix:string]) {
if (callback) {
callback(url);
}
}
}
}
}
@end