Skip to content

Commit f0b1b32

Browse files
committed
Use momcom-based internal compilation for Xcode 4+ style model files
1 parent dba7cff commit f0b1b32

20 files changed

+1050
-55
lines changed

README.markdown

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ Here's mogenerator's elevator pitch:
88
99
Want more detail? John Blanco has authored a [detailed writeup about mogenerator](http://raptureinvenice.com/getting-started-with-mogenerator/).
1010

11+
# About this fork
12+
13+
This is an experimental fork of `mogenerator` that compiles Xcode 4+ style model files internally instead of relying on `xcrun` to compile the model. The compilation process uses category files located in the `momcom` directory, which were developed for [momcom](https://github.com/atomicbird/momcom).
14+
1115
## Using mogenerator
1216

1317
Senseful wrote up a [nice summary of mogenerator's command-line options](http://stackoverflow.com/questions/3589247/how-do-the-mogenerator-parameters-work-which-can-i-send-via-xcode).

mogenerator.m

+67-55
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#import "mogenerator.h"
77
#import "RegexKitLite.h"
8+
#import "NSManagedObjectModel+momcom.h"
89

910
static NSString * const kTemplateVar = @"TemplateVar";
1011
NSString *gCustomBaseClass;
@@ -705,63 +706,74 @@ - (void)setModel:(NSString*)momOrXCDataModelFilePath {
705706
if ([[momOrXCDataModelFilePath pathExtension] isEqualToString:@"xcdatamodel"]) {
706707
// We've been handed a .xcdatamodel data model, transparently compile it into a .mom managed object model.
707708

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]);
733716
}
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");
746744
}
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+
}
765777
} else {
766778
momFilePath = momOrXCDataModelFilePath;
767779
}

mogenerator.xcodeproj/project.pbxproj

+50
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@
5959
79D2C05A0ACFBCB500F3F141 /* FoundationAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 79D2C0580ACFBCB500F3F141 /* FoundationAdditions.m */; };
6060
8DD76F9A0486AA7600D96B5E /* mogenerator.m in Sources */ = {isa = PBXBuildFile; fileRef = 08FB7796FE84155DC02AAC07 /* mogenerator.m */; settings = {ATTRIBUTES = (); }; };
6161
8DD76F9C0486AA7600D96B5E /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 08FB779EFE84155DC02AAC07 /* Foundation.framework */; };
62+
EE5ED87317332E020013CCD1 /* NSManagedObjectModel+momcom.m in Sources */ = {isa = PBXBuildFile; fileRef = EE5ED86617332E020013CCD1 /* NSManagedObjectModel+momcom.m */; };
63+
EE5ED87417332E020013CCD1 /* NSEntityDescription+momcom.m in Sources */ = {isa = PBXBuildFile; fileRef = EE5ED86817332E020013CCD1 /* NSEntityDescription+momcom.m */; };
64+
EE5ED87517332E020013CCD1 /* NSAttributeDescription+momcom.m in Sources */ = {isa = PBXBuildFile; fileRef = EE5ED86A17332E020013CCD1 /* NSAttributeDescription+momcom.m */; };
65+
EE5ED87617332E020013CCD1 /* NSPropertyDescription+momcom.m in Sources */ = {isa = PBXBuildFile; fileRef = EE5ED86C17332E020013CCD1 /* NSPropertyDescription+momcom.m */; };
66+
EE5ED87717332E020013CCD1 /* NSRelationshipDescription+momcom.m in Sources */ = {isa = PBXBuildFile; fileRef = EE5ED86E17332E020013CCD1 /* NSRelationshipDescription+momcom.m */; };
67+
EE5ED87817332E020013CCD1 /* NSFetchedPropertyDescription+momcom.m in Sources */ = {isa = PBXBuildFile; fileRef = EE5ED87017332E020013CCD1 /* NSFetchedPropertyDescription+momcom.m */; };
68+
EE5ED87917332E020013CCD1 /* NSFetchRequest+momcom.m in Sources */ = {isa = PBXBuildFile; fileRef = EE5ED87217332E020013CCD1 /* NSFetchRequest+momcom.m */; };
6269
/* End PBXBuildFile section */
6370

6471
/* Begin PBXFileReference section */
@@ -168,6 +175,20 @@
168175
79D2C0570ACFBCB500F3F141 /* FoundationAdditions.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = FoundationAdditions.h; sourceTree = "<group>"; };
169176
79D2C0580ACFBCB500F3F141 /* FoundationAdditions.m */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.objc; path = FoundationAdditions.m; sourceTree = "<group>"; };
170177
8DD76FA10486AA7600D96B5E /* mogenerator */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = mogenerator; sourceTree = BUILT_PRODUCTS_DIR; };
178+
EE5ED86517332E020013CCD1 /* NSManagedObjectModel+momcom.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSManagedObjectModel+momcom.h"; sourceTree = "<group>"; };
179+
EE5ED86617332E020013CCD1 /* NSManagedObjectModel+momcom.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSManagedObjectModel+momcom.m"; sourceTree = "<group>"; };
180+
EE5ED86717332E020013CCD1 /* NSEntityDescription+momcom.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSEntityDescription+momcom.h"; sourceTree = "<group>"; };
181+
EE5ED86817332E020013CCD1 /* NSEntityDescription+momcom.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSEntityDescription+momcom.m"; sourceTree = "<group>"; };
182+
EE5ED86917332E020013CCD1 /* NSAttributeDescription+momcom.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSAttributeDescription+momcom.h"; sourceTree = "<group>"; };
183+
EE5ED86A17332E020013CCD1 /* NSAttributeDescription+momcom.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSAttributeDescription+momcom.m"; sourceTree = "<group>"; };
184+
EE5ED86B17332E020013CCD1 /* NSPropertyDescription+momcom.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSPropertyDescription+momcom.h"; sourceTree = "<group>"; };
185+
EE5ED86C17332E020013CCD1 /* NSPropertyDescription+momcom.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSPropertyDescription+momcom.m"; sourceTree = "<group>"; };
186+
EE5ED86D17332E020013CCD1 /* NSRelationshipDescription+momcom.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSRelationshipDescription+momcom.h"; sourceTree = "<group>"; };
187+
EE5ED86E17332E020013CCD1 /* NSRelationshipDescription+momcom.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSRelationshipDescription+momcom.m"; sourceTree = "<group>"; };
188+
EE5ED86F17332E020013CCD1 /* NSFetchedPropertyDescription+momcom.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSFetchedPropertyDescription+momcom.h"; sourceTree = "<group>"; };
189+
EE5ED87017332E020013CCD1 /* NSFetchedPropertyDescription+momcom.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSFetchedPropertyDescription+momcom.m"; sourceTree = "<group>"; };
190+
EE5ED87117332E020013CCD1 /* NSFetchRequest+momcom.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSFetchRequest+momcom.h"; sourceTree = "<group>"; };
191+
EE5ED87217332E020013CCD1 /* NSFetchRequest+momcom.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSFetchRequest+momcom.m"; sourceTree = "<group>"; };
171192
/* End PBXFileReference section */
172193

173194
/* Begin PBXFrameworksBuildPhase section */
@@ -188,6 +209,7 @@
188209
isa = PBXGroup;
189210
children = (
190211
08FB7795FE84155DC02AAC07 /* Source */,
212+
EE5ED86417332DED0013CCD1 /* momcom */,
191213
79D2BF510ACFB51000F3F141 /* MiscMerge */,
192214
457C26C6139A1EF900BF00DD /* ponso */,
193215
55200E8E0C49FEEA00018A42 /* ddcli */,
@@ -345,6 +367,27 @@
345367
path = MiscMerge;
346368
sourceTree = "<group>";
347369
};
370+
EE5ED86417332DED0013CCD1 /* momcom */ = {
371+
isa = PBXGroup;
372+
children = (
373+
EE5ED86517332E020013CCD1 /* NSManagedObjectModel+momcom.h */,
374+
EE5ED86617332E020013CCD1 /* NSManagedObjectModel+momcom.m */,
375+
EE5ED86717332E020013CCD1 /* NSEntityDescription+momcom.h */,
376+
EE5ED86817332E020013CCD1 /* NSEntityDescription+momcom.m */,
377+
EE5ED86917332E020013CCD1 /* NSAttributeDescription+momcom.h */,
378+
EE5ED86A17332E020013CCD1 /* NSAttributeDescription+momcom.m */,
379+
EE5ED86B17332E020013CCD1 /* NSPropertyDescription+momcom.h */,
380+
EE5ED86C17332E020013CCD1 /* NSPropertyDescription+momcom.m */,
381+
EE5ED86D17332E020013CCD1 /* NSRelationshipDescription+momcom.h */,
382+
EE5ED86E17332E020013CCD1 /* NSRelationshipDescription+momcom.m */,
383+
EE5ED86F17332E020013CCD1 /* NSFetchedPropertyDescription+momcom.h */,
384+
EE5ED87017332E020013CCD1 /* NSFetchedPropertyDescription+momcom.m */,
385+
EE5ED87117332E020013CCD1 /* NSFetchRequest+momcom.h */,
386+
EE5ED87217332E020013CCD1 /* NSFetchRequest+momcom.m */,
387+
);
388+
path = momcom;
389+
sourceTree = "<group>";
390+
};
348391
/* End PBXGroup section */
349392

350393
/* Begin PBXNativeTarget section */
@@ -457,6 +500,13 @@
457500
457C26CD139A1EF900BF00DD /* MKCDAGNode.m in Sources */,
458501
457C26CE139A1EF900BF00DD /* MKCNSEntityDescriptionAdditions.m in Sources */,
459502
457C26CF139A1EF900BF00DD /* MKCNSManagedObjectModelAdditions.m in Sources */,
503+
EE5ED87317332E020013CCD1 /* NSManagedObjectModel+momcom.m in Sources */,
504+
EE5ED87417332E020013CCD1 /* NSEntityDescription+momcom.m in Sources */,
505+
EE5ED87517332E020013CCD1 /* NSAttributeDescription+momcom.m in Sources */,
506+
EE5ED87617332E020013CCD1 /* NSPropertyDescription+momcom.m in Sources */,
507+
EE5ED87717332E020013CCD1 /* NSRelationshipDescription+momcom.m in Sources */,
508+
EE5ED87817332E020013CCD1 /* NSFetchedPropertyDescription+momcom.m in Sources */,
509+
EE5ED87917332E020013CCD1 /* NSFetchRequest+momcom.m in Sources */,
460510
);
461511
runOnlyForDeploymentPostprocessing = 0;
462512
};
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// NSAttributeDescription+momcom.h
3+
// momc
4+
//
5+
// Created by Tom Harrington on 4/18/13.
6+
// Copyright (c) 2013 Tom Harrington. All rights reserved.
7+
//
8+
9+
#import <CoreData/CoreData.h>
10+
11+
@interface NSAttributeDescription (momcom)
12+
13+
+ (NSAttributeDescription *)baseEntityForXML:(NSXMLElement *)xmlNode;
14+
15+
@end

0 commit comments

Comments
 (0)