|
5 | 5 |
|
6 | 6 | #import "mogenerator.h"
|
7 | 7 | #import "RegexKitLite.h"
|
| 8 | +#import "NSManagedObjectModel+momcom.h" |
8 | 9 |
|
9 | 10 | static NSString * const kTemplateVar = @"TemplateVar";
|
10 | 11 | NSString *gCustomBaseClass;
|
@@ -705,63 +706,74 @@ - (void)setModel:(NSString*)momOrXCDataModelFilePath {
|
705 | 706 | if ([[momOrXCDataModelFilePath pathExtension] isEqualToString:@"xcdatamodel"]) {
|
706 | 707 | // We've been handed a .xcdatamodel data model, transparently compile it into a .mom managed object model.
|
707 | 708 |
|
708 |
| - NSString *momcTool = nil; |
709 |
| - {{ |
710 |
| - if (NO && [fm fileExistsAtPath:@"/usr/bin/xcrun"]) { |
711 |
| - // Cool, we can just use Xcode 3.2.6/4.x's xcrun command to find and execute momc for us. |
712 |
| - momcTool = @"/usr/bin/xcrun momc"; |
713 |
| - } else { |
714 |
| - // Rats, don't have xcrun. Hunt around for momc in various places where various versions of Xcode stashed it. |
715 |
| - NSString *xcodeSelectMomcPath = [NSString stringWithFormat:@"%@/usr/bin/momc", [self xcodeSelectPrintPath]]; |
716 |
| - |
717 |
| - if ([fm fileExistsAtPath:xcodeSelectMomcPath]) { |
718 |
| - momcTool = [NSString stringWithFormat:@"\"%@\"", xcodeSelectMomcPath]; // Quote for safety. |
719 |
| - } else if ([fm fileExistsAtPath:@"/Applications/Xcode.app/Contents/Developer/usr/bin/momc"]) { |
720 |
| - // Xcode 4.3 - Command Line Tools for Xcode |
721 |
| - momcTool = @"/Applications/Xcode.app/Contents/Developer/usr/bin/momc"; |
722 |
| - } else if ([fm fileExistsAtPath:@"/Developer/usr/bin/momc"]) { |
723 |
| - // Xcode 3.1. |
724 |
| - momcTool = @"/Developer/usr/bin/momc"; |
725 |
| - } else if ([fm fileExistsAtPath:@"/Library/Application Support/Apple/Developer Tools/Plug-ins/XDCoreDataModel.xdplugin/Contents/Resources/momc"]) { |
726 |
| - // Xcode 3.0. |
727 |
| - momcTool = @"\"/Library/Application Support/Apple/Developer Tools/Plug-ins/XDCoreDataModel.xdplugin/Contents/Resources/momc\""; |
728 |
| - } else if ([fm fileExistsAtPath:@"/Developer/Library/Xcode/Plug-ins/XDCoreDataModel.xdplugin/Contents/Resources/momc"]) { |
729 |
| - // Xcode 2.4. |
730 |
| - momcTool = @"/Developer/Library/Xcode/Plug-ins/XDCoreDataModel.xdplugin/Contents/Resources/momc"; |
731 |
| - } |
732 |
| - assert(momcTool && "momc not found"); |
| 709 | + NSString *contentsPath = [momOrXCDataModelFilePath stringByAppendingPathComponent:@"contents"]; |
| 710 | + if ([[NSFileManager defaultManager] fileExistsAtPath:contentsPath]) { |
| 711 | + // Cool, the model is in the Xcode 4.0+ format, we can compile it ourselves. |
| 712 | + NSError *compileError = nil; |
| 713 | + momFilePath = [NSManagedObjectModel compileModelAtPath:momOrXCDataModelFilePath inDirectory:NSTemporaryDirectory() error:&compileError]; |
| 714 | + if (compileError) { |
| 715 | + NSLog(@"Error: %@", [compileError localizedDescription]); |
733 | 716 | }
|
734 |
| - }} |
735 |
| - |
736 |
| - NSMutableString *momcOptions = [NSMutableString string]; |
737 |
| - {{ |
738 |
| - NSArray *supportedMomcOptions = [NSArray arrayWithObjects: |
739 |
| - @"MOMC_NO_WARNINGS", |
740 |
| - @"MOMC_NO_INVERSE_RELATIONSHIP_WARNINGS", |
741 |
| - @"MOMC_SUPPRESS_INVERSE_TRANSIENT_ERROR", |
742 |
| - nil]; |
743 |
| - for (NSString *momcOption in supportedMomcOptions) { |
744 |
| - if ([[[NSProcessInfo processInfo] environment] objectForKey:momcOption]) { |
745 |
| - [momcOptions appendFormat:@" -%@ ", momcOption]; |
| 717 | + assert(momFilePath); |
| 718 | + } else { |
| 719 | + NSString *momcTool = nil; |
| 720 | + {{ |
| 721 | + if (NO && [fm fileExistsAtPath:@"/usr/bin/xcrun"]) { |
| 722 | + // Cool, we can just use Xcode 3.2.6/4.x's xcrun command to find and execute momc for us. |
| 723 | + momcTool = @"/usr/bin/xcrun momc"; |
| 724 | + } else { |
| 725 | + // Rats, don't have xcrun. Hunt around for momc in various places where various versions of Xcode stashed it. |
| 726 | + NSString *xcodeSelectMomcPath = [NSString stringWithFormat:@"%@/usr/bin/momc", [self xcodeSelectPrintPath]]; |
| 727 | + |
| 728 | + if ([fm fileExistsAtPath:xcodeSelectMomcPath]) { |
| 729 | + momcTool = [NSString stringWithFormat:@"\"%@\"", xcodeSelectMomcPath]; // Quote for safety. |
| 730 | + } else if ([fm fileExistsAtPath:@"/Applications/Xcode.app/Contents/Developer/usr/bin/momc"]) { |
| 731 | + // Xcode 4.3 - Command Line Tools for Xcode |
| 732 | + momcTool = @"/Applications/Xcode.app/Contents/Developer/usr/bin/momc"; |
| 733 | + } else if ([fm fileExistsAtPath:@"/Developer/usr/bin/momc"]) { |
| 734 | + // Xcode 3.1. |
| 735 | + momcTool = @"/Developer/usr/bin/momc"; |
| 736 | + } else if ([fm fileExistsAtPath:@"/Library/Application Support/Apple/Developer Tools/Plug-ins/XDCoreDataModel.xdplugin/Contents/Resources/momc"]) { |
| 737 | + // Xcode 3.0. |
| 738 | + momcTool = @"\"/Library/Application Support/Apple/Developer Tools/Plug-ins/XDCoreDataModel.xdplugin/Contents/Resources/momc\""; |
| 739 | + } else if ([fm fileExistsAtPath:@"/Developer/Library/Xcode/Plug-ins/XDCoreDataModel.xdplugin/Contents/Resources/momc"]) { |
| 740 | + // Xcode 2.4. |
| 741 | + momcTool = @"/Developer/Library/Xcode/Plug-ins/XDCoreDataModel.xdplugin/Contents/Resources/momc"; |
| 742 | + } |
| 743 | + assert(momcTool && "momc not found"); |
746 | 744 | }
|
747 |
| - } |
748 |
| - }} |
749 |
| - |
750 |
| - NSString *momcIncantation = nil; |
751 |
| - {{ |
752 |
| - NSString *tempGeneratedMomFileName = [[[NSProcessInfo processInfo] globallyUniqueString] stringByAppendingPathExtension:@"mom"]; |
753 |
| - tempGeneratedMomFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:tempGeneratedMomFileName]; |
754 |
| - momcIncantation = [NSString stringWithFormat:@"%@ %@ \"%@\" \"%@\"", |
755 |
| - momcTool, |
756 |
| - momcOptions, |
757 |
| - momOrXCDataModelFilePath, |
758 |
| - tempGeneratedMomFilePath]; |
759 |
| - }} |
760 |
| - |
761 |
| - {{ |
762 |
| - system([momcIncantation UTF8String]); // Ignore system() result since momc sadly doesn't return any relevent error codes. |
763 |
| - momFilePath = tempGeneratedMomFilePath; |
764 |
| - }} |
| 745 | + }} |
| 746 | + |
| 747 | + NSMutableString *momcOptions = [NSMutableString string]; |
| 748 | + {{ |
| 749 | + NSArray *supportedMomcOptions = [NSArray arrayWithObjects: |
| 750 | + @"MOMC_NO_WARNINGS", |
| 751 | + @"MOMC_NO_INVERSE_RELATIONSHIP_WARNINGS", |
| 752 | + @"MOMC_SUPPRESS_INVERSE_TRANSIENT_ERROR", |
| 753 | + nil]; |
| 754 | + for (NSString *momcOption in supportedMomcOptions) { |
| 755 | + if ([[[NSProcessInfo processInfo] environment] objectForKey:momcOption]) { |
| 756 | + [momcOptions appendFormat:@" -%@ ", momcOption]; |
| 757 | + } |
| 758 | + } |
| 759 | + }} |
| 760 | + |
| 761 | + NSString *momcIncantation = nil; |
| 762 | + {{ |
| 763 | + NSString *tempGeneratedMomFileName = [[[NSProcessInfo processInfo] globallyUniqueString] stringByAppendingPathExtension:@"mom"]; |
| 764 | + tempGeneratedMomFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:tempGeneratedMomFileName]; |
| 765 | + momcIncantation = [NSString stringWithFormat:@"%@ %@ \"%@\" \"%@\"", |
| 766 | + momcTool, |
| 767 | + momcOptions, |
| 768 | + momOrXCDataModelFilePath, |
| 769 | + tempGeneratedMomFilePath]; |
| 770 | + }} |
| 771 | + |
| 772 | + {{ |
| 773 | + system([momcIncantation UTF8String]); // Ignore system() result since momc sadly doesn't return any relevent error codes. |
| 774 | + momFilePath = tempGeneratedMomFilePath; |
| 775 | + }} |
| 776 | + } |
765 | 777 | } else {
|
766 | 778 | momFilePath = momOrXCDataModelFilePath;
|
767 | 779 | }
|
|
0 commit comments