Skip to content

Commit 8b3868b

Browse files
author
Bartosz Waresiak
committed
Introducing 'compact' mode - in this mode do not print superclasses' properties for each class
1 parent 2785938 commit 8b3868b

File tree

3 files changed

+81
-13
lines changed

3 files changed

+81
-13
lines changed

src/MOMPrinter.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,15 @@
88

99
#import <Foundation/Foundation.h>
1010

11+
typedef NS_ENUM(NSUInteger, MOMPrinterPeropertiesOutputMode) {
12+
MOMPrinterIncludeSuperclassProperties = 1,
13+
MOMPrinterOmitSuperclassProperties = 2
14+
};
15+
1116
@interface MOMPrinter : NSObject
1217

18+
- (id)initWithMode:(MOMPrinterPeropertiesOutputMode)mode;
19+
1320
- (BOOL)printPath:(NSString *)path;
1421

1522
@end

src/MOMPrinter.m

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,31 @@
1111
#import "NSData+Base64.h"
1212
#import "utils.h"
1313

14+
@interface MOMPrinter ()
15+
16+
@property (nonatomic, assign) MOMPrinterPeropertiesOutputMode _mode;
17+
18+
@end
19+
1420
@implementation MOMPrinter
1521

22+
#pragma mark - init methods
23+
24+
- (id)init {
25+
return [self initWithMode:MOMPrinterIncludeSuperclassProperties];
26+
}
27+
28+
- (id)initWithMode:(MOMPrinterPeropertiesOutputMode)mode {
29+
30+
self = [super init];
31+
if (self) {
32+
self._mode = mode;
33+
}
34+
return self;
35+
}
36+
37+
#pragma mark -
38+
1639
NSString *commonFlagsStringForProperty(NSPropertyDescription *property) {
1740
char ochar = [property isOptional] ? 'O' : ' ';
1841
char tchar = [property isTransient] ? 'T' : ' ';
@@ -119,7 +142,20 @@ - (BOOL)printPath:(NSString *)path {
119142
}
120143
[entityStr appendFormat:@" (%@)", [entity managedObjectClassName]];
121144
NSPrintf(@"%@\n", entityStr);
122-
NSMutableArray *properties = [NSMutableArray arrayWithArray:[entity properties]];
145+
146+
NSMutableArray *properties = nil;
147+
148+
if (self._mode == MOMPrinterIncludeSuperclassProperties) {
149+
properties = [NSMutableArray arrayWithArray:[entity properties]];
150+
} else {
151+
// Filter out properties that belong to superclasses.
152+
NSSet *superentityProperties = [NSSet setWithArray:[superentity properties]];
153+
NSMutableSet *entityProperties = [NSMutableSet setWithArray:[entity properties]];
154+
[entityProperties minusSet:superentityProperties];
155+
156+
properties = [[entityProperties allObjects] mutableCopy];
157+
}
158+
123159
[properties sortUsingComparator:^(id obj1, id obj2) {
124160
NSNumber *n1 = orderNumberForClassOfProperty(obj1);
125161
NSNumber *n2 = orderNumberForClassOfProperty(obj2);
@@ -130,6 +166,7 @@ - (BOOL)printPath:(NSString *)path {
130166
return result;
131167
}
132168
}];
169+
133170
for (id property in properties) {
134171
const char *name = [[property name] UTF8String];
135172
const char *commonFlags = [commonFlagsStringForProperty(property) UTF8String];

src/main.m

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,50 @@
1111
#import "MOMCompiler.h"
1212
#import "MOMPrinter.h"
1313

14+
int run(NSString *path, BOOL includeSuperclassProperites);
15+
1416
int main(int argc, const char * argv[])
1517
{
16-
1718
@autoreleasepool {
1819
if (argc == 2) {
1920
NSString *path = [NSString stringWithCString:argv[1] encoding:NSUTF8StringEncoding];
20-
MOMCompiler *compiler = [[[MOMCompiler alloc] init] autorelease];
21-
NSString *compiledPath = [compiler compilePath:path];
22-
if (compiledPath == nil) {
23-
return 2;
21+
22+
return run(path, YES);
23+
24+
} else if (argc == 3) {
25+
NSString *parameter = [NSString stringWithCString:argv[1] encoding:NSASCIIStringEncoding];
26+
if ([parameter isEqualToString:@"--compact"] ||
27+
[parameter isEqualToString:@"-c"] ) {
28+
NSString *path = [NSString stringWithCString:argv[2] encoding:NSUTF8StringEncoding];
29+
30+
return run(path, NO);
2431
}
25-
MOMPrinter *printer = [[[MOMPrinter alloc] init] autorelease];
26-
if (![printer printPath:compiledPath]) {
32+
}
33+
}
34+
35+
NSPrintf(@"Usage: [--compact] %@ path_to_xcdatamodel_file\n", [[NSProcessInfo processInfo] processName]);
36+
return 1;
37+
}
38+
39+
int run(NSString *path, BOOL includeSuperclassProperites) {
40+
41+
MOMCompiler *compiler = [[[MOMCompiler alloc] init] autorelease];
42+
NSString *compiledPath = [compiler compilePath:path];
43+
if (compiledPath == nil) {
2744
return 2;
28-
}
45+
}
46+
MOMPrinter *printer = nil;
47+
48+
if (includeSuperclassProperites) {
49+
printer = [[[MOMPrinter alloc] init] autorelease];
2950
} else {
30-
NSPrintf(@"Usage: %@ path_to_xcdatamodel_file\n", [[NSProcessInfo processInfo] processName]);
31-
return 1;
51+
printer = [[[MOMPrinter alloc] initWithMode:MOMPrinterOmitSuperclassProperties] autorelease];
3252
}
33-
}
34-
return 0;
53+
54+
if (![printer printPath:compiledPath]) {
55+
return 2;
56+
}
57+
58+
return 0;
3559
}
3660

0 commit comments

Comments
 (0)