-
Notifications
You must be signed in to change notification settings - Fork 395
/
Xmod.m
49 lines (40 loc) · 1.86 KB
/
Xmod.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
#import "Xmod.h"
#import "MethodSwizzle.h"
@interface NSObject (mogenerator_saveModelToFile)
@end
@implementation NSObject (mogenerator_saveModelToFile)
- (BOOL)mogenerator_saveModelToFile:(NSString*)modelPackagePath_ {
BOOL result = [self mogenerator_saveModelToFile:modelPackagePath_];
if (result) {
NSString *modelPackageFolderPath = [modelPackagePath_ stringByDeletingLastPathComponent];
NSString *mosFolderPath = [modelPackageFolderPath stringByAppendingPathComponent:@"MOs"];
BOOL isDirectory;
if ([[NSFileManager defaultManager] fileExistsAtPath:mosFolderPath isDirectory:&isDirectory] && isDirectory) {
NSTask *mogenerator = [[[NSTask alloc] init] autorelease];
[mogenerator setLaunchPath:@"/usr/bin/mogenerator"];
[mogenerator setCurrentDirectoryPath:mosFolderPath];
// -model ../MyDocument.xcdatamodel -includem include.mm
[mogenerator setArguments:[NSArray arrayWithObjects:
@"-model", modelPackagePath_,
@"-includem", @"include.m",
nil]];
[mogenerator launch];
}
}
return result;
}
@end
@implementation Xmod
+ (void)pluginDidLoad:(NSBundle*)bundle_ {
// Force loading of the Core Data XDesign plugin so we can find the class to swizzle its instance method.
NSBundle *coreDataPlugin = [NSBundle bundleWithPath:@"/Library/Application Support/Apple/Developer Tools/Plug-ins/XDCoreDataModel.xdplugin"];
NSAssert(coreDataPlugin, @"failed to load XDCoreDataModel.xdplugin");
[coreDataPlugin load];
Class persistenceDocumentController = NSClassFromString(@"XDPersistenceDocumentController");
NSAssert(persistenceDocumentController, @"failed to load XDPersistenceDocumentController");
BOOL swizzled = MethodSwizzle(persistenceDocumentController,
@selector(saveModelToFile:),
@selector(mogenerator_saveModelToFile:));
NSAssert(swizzled, @"failed to swizzle -[XDPersistenceDocumentController saveModelToFile:]");
}
@end