Skip to content
This repository
Browse code

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

…Josh Abernathy)
  • Loading branch information...
commit 93b4c6bfcde93701875174040e76ed192643bc87 1 parent ffffc65
Jonathan 'Wolf' Rentzsch authored July 04, 2010

Showing 1 changed file with 36 additions and 4 deletions. Show diff stats Hide diff stats

  1. 40  mogenerator.m
40  mogenerator.m
@@ -362,7 +362,34 @@ - (void) printUsage;
362 362
            "Inspired by eogenerator.\n");
363 363
 }
364 364
 
365  
-- (void) setModel: (NSString *) path;
  365
+- (NSString*)currentXcodePath {
  366
+	NSMutableString *result = @"";
  367
+	
  368
+	@try {
  369
+		NSTask *task = [[[NSTask alloc] init] autorelease];
  370
+		[task setLaunchPath:@"/usr/bin/xcode-select"];
  371
+		
  372
+		[task setArguments:[NSArray arrayWithObject:@"-print-path"]];
  373
+		
  374
+		NSPipe *pipe = [NSPipe pipe];
  375
+		[task setStandardOutput:pipe];
  376
+		
  377
+		NSFileHandle *file = [pipe fileHandleForReading];
  378
+		
  379
+		[task launch];
  380
+		
  381
+		NSData *data = [file readDataToEndOfFile];
  382
+		
  383
+		NSMutableString *result = [[[NSMutableString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
  384
+		[result deleteCharactersInRange:NSMakeRange([result length]-1, 1)]; // trim newline
  385
+	} @catch(NSException *ex) {
  386
+		ddprintf(@"WARNING couldn't launch /usr/bin/xcode-select\n");
  387
+	}
  388
+	
  389
+	return result;
  390
+}
  391
+
  392
+- (void)setModel:(NSString*)path;
366 393
 {
367 394
     assert(!model); // Currently we only can load one model.
368 395
 
@@ -377,12 +404,17 @@ - (void) setModel: (NSString *) path;
377 404
         //	We've been handed a .xcdatamodel data model, transparently compile it into a .mom managed object model.
378 405
         
379 406
         //  Find where Xcode installed momc this week.
  407
+        NSFileManager *fm = [NSFileManager defaultManager];
380 408
         NSString *momc = nil;
381  
-        if ([[NSFileManager defaultManager] fileExistsAtPath:@"/Developer/usr/bin/momc"]) { // Xcode 3.1 installs it here.
  409
+        NSString *defaultLocation = [NSString stringWithFormat:@"%@/usr/bin/momc", [self currentXcodePath]];
  410
+        
  411
+        if([fm fileExistsAtPath:defaultLocation]) {
  412
+            momc = defaultLocation;
  413
+        } else if ([fm fileExistsAtPath:@"/Developer/usr/bin/momc"]) { // Xcode 3.1 installs it here.
382 414
             momc = @"/Developer/usr/bin/momc";
383  
-        } else if ([[NSFileManager defaultManager] fileExistsAtPath:@"/Library/Application Support/Apple/Developer Tools/Plug-ins/XDCoreDataModel.xdplugin/Contents/Resources/momc"]) { // Xcode 3.0.
  415
+        } else if ([fm fileExistsAtPath:@"/Library/Application Support/Apple/Developer Tools/Plug-ins/XDCoreDataModel.xdplugin/Contents/Resources/momc"]) { // Xcode 3.0.
384 416
             momc = @"/Library/Application Support/Apple/Developer Tools/Plug-ins/XDCoreDataModel.xdplugin/Contents/Resources/momc";
385  
-        } else if ([[NSFileManager defaultManager] fileExistsAtPath:@"/Developer/Library/Xcode/Plug-ins/XDCoreDataModel.xdplugin/Contents/Resources/momc"]) { // Xcode 2.4.
  417
+        } else if ([fm fileExistsAtPath:@"/Developer/Library/Xcode/Plug-ins/XDCoreDataModel.xdplugin/Contents/Resources/momc"]) { // Xcode 2.4.
386 418
             momc = @"/Developer/Library/Xcode/Plug-ins/XDCoreDataModel.xdplugin/Contents/Resources/momc";
387 419
         }
388 420
         assert(momc && "momc not found");

0 notes on commit 93b4c6b

Nikita Zhuk
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.

Please sign in to comment.
Something went wrong with that request. Please try again.