Skip to content

Commit

Permalink
[NEW] Use xcode-select to dynamically discover our way to momc. (…
Browse files Browse the repository at this point in the history
…Josh Abernathy)
  • Loading branch information
rentzsch committed Jul 5, 2010
1 parent ffffc65 commit 93b4c6b
Showing 1 changed file with 36 additions and 4 deletions.
40 changes: 36 additions & 4 deletions mogenerator.m
Expand Up @@ -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.

Expand All @@ -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");
Expand Down

0 comments on commit 93b4c6b

Please sign in to comment.