From 93b4c6bfcde93701875174040e76ed192643bc87 Mon Sep 17 00:00:00 2001 From: rentzsch Date: Sun, 4 Jul 2010 22:43:27 -0500 Subject: [PATCH] [NEW] Use `xcode-select` to dynamically discover our way to `momc`. (Josh Abernathy) --- mogenerator.m | 40 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/mogenerator.m b/mogenerator.m index c8452e64..0fa9d698 100644 --- a/mogenerator.m +++ b/mogenerator.m @@ -362,7 +362,34 @@ - (void) printUsage; "Inspired by eogenerator.\n"); } -- (void) setModel: (NSString *) path; +- (NSString*)currentXcodePath { + NSMutableString *result = @""; + + @try { + NSTask *task = [[[NSTask alloc] init] autorelease]; + [task setLaunchPath:@"/usr/bin/xcode-select"]; + + [task setArguments:[NSArray arrayWithObject:@"-print-path"]]; + + NSPipe *pipe = [NSPipe pipe]; + [task setStandardOutput:pipe]; + + NSFileHandle *file = [pipe fileHandleForReading]; + + [task launch]; + + NSData *data = [file readDataToEndOfFile]; + + NSMutableString *result = [[[NSMutableString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease]; + [result deleteCharactersInRange:NSMakeRange([result length]-1, 1)]; // trim newline + } @catch(NSException *ex) { + ddprintf(@"WARNING couldn't launch /usr/bin/xcode-select\n"); + } + + return result; +} + +- (void)setModel:(NSString*)path; { assert(!model); // Currently we only can load one model. @@ -377,12 +404,17 @@ - (void) setModel: (NSString *) path; // We've been handed a .xcdatamodel data model, transparently compile it into a .mom managed object model. // Find where Xcode installed momc this week. + NSFileManager *fm = [NSFileManager defaultManager]; NSString *momc = nil; - if ([[NSFileManager defaultManager] fileExistsAtPath:@"/Developer/usr/bin/momc"]) { // Xcode 3.1 installs it here. + NSString *defaultLocation = [NSString stringWithFormat:@"%@/usr/bin/momc", [self currentXcodePath]]; + + if([fm fileExistsAtPath:defaultLocation]) { + momc = defaultLocation; + } else if ([fm fileExistsAtPath:@"/Developer/usr/bin/momc"]) { // Xcode 3.1 installs it here. momc = @"/Developer/usr/bin/momc"; - } else if ([[NSFileManager defaultManager] fileExistsAtPath:@"/Library/Application Support/Apple/Developer Tools/Plug-ins/XDCoreDataModel.xdplugin/Contents/Resources/momc"]) { // Xcode 3.0. + } else if ([fm fileExistsAtPath:@"/Library/Application Support/Apple/Developer Tools/Plug-ins/XDCoreDataModel.xdplugin/Contents/Resources/momc"]) { // Xcode 3.0. momc = @"/Library/Application Support/Apple/Developer Tools/Plug-ins/XDCoreDataModel.xdplugin/Contents/Resources/momc"; - } else if ([[NSFileManager defaultManager] fileExistsAtPath:@"/Developer/Library/Xcode/Plug-ins/XDCoreDataModel.xdplugin/Contents/Resources/momc"]) { // Xcode 2.4. + } else if ([fm fileExistsAtPath:@"/Developer/Library/Xcode/Plug-ins/XDCoreDataModel.xdplugin/Contents/Resources/momc"]) { // Xcode 2.4. momc = @"/Developer/Library/Xcode/Plug-ins/XDCoreDataModel.xdplugin/Contents/Resources/momc"; } assert(momc && "momc not found");