Permalink
Browse files

[NEW] Use `xcode-select` to dynamically discover our way to `momc`. (…

…Josh Abernathy)
  • Loading branch information...
1 parent ffffc65 commit 93b4c6bfcde93701875174040e76ed192643bc87 @rentzsch committed Jul 5, 2010
Showing with 36 additions and 4 deletions.
  1. +36 −4 mogenerator.m
View
@@ -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];
@nzhuk
nzhuk Jul 14, 2010 collaborator

This is a bug. 'result' variable shadows the one declared at line 366, so the end result is that this method always returns an empty string.

+ [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");

0 comments on commit 93b4c6b

Please sign in to comment.