Skip to content

Commit

Permalink
Merge pull request #225 from atomicbird/master
Browse files Browse the repository at this point in the history
Use built-in model compiler instead of relying on Xcode
  • Loading branch information
Justin Williams committed Apr 26, 2015
2 parents 5983bf0 + b38ee75 commit 73fccca
Show file tree
Hide file tree
Showing 17 changed files with 1,024 additions and 55 deletions.
4 changes: 4 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ Here's mogenerator's elevator pitch:
Want more detail? John Blanco has authored a [detailed writeup about mogenerator](http://raptureinvenice.com/getting-started-with-mogenerator/).

# About this fork

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).

## Using mogenerator

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).
Expand Down
122 changes: 67 additions & 55 deletions mogenerator.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#import "mogenerator.h"
#import "RegexKitLite.h"
#import "NSManagedObjectModel+momcom.h"

static NSString * const kTemplateVar = @"TemplateVar";
NSString *gCustomBaseClass;
Expand Down Expand Up @@ -817,63 +818,74 @@ - (void)setModel:(NSString*)momOrXCDataModelFilePath {
if ([[momOrXCDataModelFilePath pathExtension] isEqualToString:@"xcdatamodel"]) {
// We've been handed a .xcdatamodel data model, transparently compile it into a .mom managed object model.

NSString *momcTool = nil;
{{
if (NO && [fm fileExistsAtPath:@"/usr/bin/xcrun"]) {
// Cool, we can just use Xcode 3.2.6/4.x's xcrun command to find and execute momc for us.
momcTool = @"/usr/bin/xcrun momc";
} else {
// Rats, don't have xcrun. Hunt around for momc in various places where various versions of Xcode stashed it.
NSString *xcodeSelectMomcPath = [NSString stringWithFormat:@"%@/usr/bin/momc", [self xcodeSelectPrintPath]];

if ([fm fileExistsAtPath:xcodeSelectMomcPath]) {
momcTool = [NSString stringWithFormat:@"\"%@\"", xcodeSelectMomcPath]; // Quote for safety.
} else if ([fm fileExistsAtPath:@"/Applications/Xcode.app/Contents/Developer/usr/bin/momc"]) {
// Xcode 4.3 - Command Line Tools for Xcode
momcTool = @"/Applications/Xcode.app/Contents/Developer/usr/bin/momc";
} else if ([fm fileExistsAtPath:@"/Developer/usr/bin/momc"]) {
// Xcode 3.1.
momcTool = @"/Developer/usr/bin/momc";
} else if ([fm fileExistsAtPath:@"/Library/Application Support/Apple/Developer Tools/Plug-ins/XDCoreDataModel.xdplugin/Contents/Resources/momc"]) {
// Xcode 3.0.
momcTool = @"\"/Library/Application Support/Apple/Developer Tools/Plug-ins/XDCoreDataModel.xdplugin/Contents/Resources/momc\"";
} else if ([fm fileExistsAtPath:@"/Developer/Library/Xcode/Plug-ins/XDCoreDataModel.xdplugin/Contents/Resources/momc"]) {
// Xcode 2.4.
momcTool = @"/Developer/Library/Xcode/Plug-ins/XDCoreDataModel.xdplugin/Contents/Resources/momc";
}
assert(momcTool && "momc not found");
NSString *contentsPath = [momOrXCDataModelFilePath stringByAppendingPathComponent:@"contents"];
if ([[NSFileManager defaultManager] fileExistsAtPath:contentsPath]) {
// Cool, the model is in the Xcode 4.0+ format, we can compile it ourselves.
NSError *compileError = nil;
momFilePath = [NSManagedObjectModel compileModelAtPath:momOrXCDataModelFilePath inDirectory:NSTemporaryDirectory() error:&compileError];
if (compileError) {
NSLog(@"Error: %@", [compileError localizedDescription]);
}
}}

NSMutableString *momcOptions = [NSMutableString string];
{{
NSArray *supportedMomcOptions = [NSArray arrayWithObjects:
@"MOMC_NO_WARNINGS",
@"MOMC_NO_INVERSE_RELATIONSHIP_WARNINGS",
@"MOMC_SUPPRESS_INVERSE_TRANSIENT_ERROR",
nil];
for (NSString *momcOption in supportedMomcOptions) {
if ([[[NSProcessInfo processInfo] environment] objectForKey:momcOption]) {
[momcOptions appendFormat:@" -%@ ", momcOption];
assert(momFilePath);
} else {
NSString *momcTool = nil;
{{
if (NO && [fm fileExistsAtPath:@"/usr/bin/xcrun"]) {
// Cool, we can just use Xcode 3.2.6/4.x's xcrun command to find and execute momc for us.
momcTool = @"/usr/bin/xcrun momc";
} else {
// Rats, don't have xcrun. Hunt around for momc in various places where various versions of Xcode stashed it.
NSString *xcodeSelectMomcPath = [NSString stringWithFormat:@"%@/usr/bin/momc", [self xcodeSelectPrintPath]];

if ([fm fileExistsAtPath:xcodeSelectMomcPath]) {
momcTool = [NSString stringWithFormat:@"\"%@\"", xcodeSelectMomcPath]; // Quote for safety.
} else if ([fm fileExistsAtPath:@"/Applications/Xcode.app/Contents/Developer/usr/bin/momc"]) {
// Xcode 4.3 - Command Line Tools for Xcode
momcTool = @"/Applications/Xcode.app/Contents/Developer/usr/bin/momc";
} else if ([fm fileExistsAtPath:@"/Developer/usr/bin/momc"]) {
// Xcode 3.1.
momcTool = @"/Developer/usr/bin/momc";
} else if ([fm fileExistsAtPath:@"/Library/Application Support/Apple/Developer Tools/Plug-ins/XDCoreDataModel.xdplugin/Contents/Resources/momc"]) {
// Xcode 3.0.
momcTool = @"\"/Library/Application Support/Apple/Developer Tools/Plug-ins/XDCoreDataModel.xdplugin/Contents/Resources/momc\"";
} else if ([fm fileExistsAtPath:@"/Developer/Library/Xcode/Plug-ins/XDCoreDataModel.xdplugin/Contents/Resources/momc"]) {
// Xcode 2.4.
momcTool = @"/Developer/Library/Xcode/Plug-ins/XDCoreDataModel.xdplugin/Contents/Resources/momc";
}
assert(momcTool && "momc not found");
}
}
}}

NSString *momcIncantation = nil;
{{
NSString *tempGeneratedMomFileName = [[[NSProcessInfo processInfo] globallyUniqueString] stringByAppendingPathExtension:@"mom"];
tempGeneratedMomFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:tempGeneratedMomFileName];
momcIncantation = [NSString stringWithFormat:@"%@ %@ \"%@\" \"%@\"",
momcTool,
momcOptions,
momOrXCDataModelFilePath,
tempGeneratedMomFilePath];
}}

{{
system([momcIncantation UTF8String]); // Ignore system() result since momc sadly doesn't return any relevent error codes.
momFilePath = tempGeneratedMomFilePath;
}}
}}

NSMutableString *momcOptions = [NSMutableString string];
{{
NSArray *supportedMomcOptions = [NSArray arrayWithObjects:
@"MOMC_NO_WARNINGS",
@"MOMC_NO_INVERSE_RELATIONSHIP_WARNINGS",
@"MOMC_SUPPRESS_INVERSE_TRANSIENT_ERROR",
nil];
for (NSString *momcOption in supportedMomcOptions) {
if ([[[NSProcessInfo processInfo] environment] objectForKey:momcOption]) {
[momcOptions appendFormat:@" -%@ ", momcOption];
}
}
}}

NSString *momcIncantation = nil;
{{
NSString *tempGeneratedMomFileName = [[[NSProcessInfo processInfo] globallyUniqueString] stringByAppendingPathExtension:@"mom"];
tempGeneratedMomFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:tempGeneratedMomFileName];
momcIncantation = [NSString stringWithFormat:@"%@ %@ \"%@\" \"%@\"",
momcTool,
momcOptions,
momOrXCDataModelFilePath,
tempGeneratedMomFilePath];
}}

{{
system([momcIncantation UTF8String]); // Ignore system() result since momc sadly doesn't return any relevent error codes.
momFilePath = tempGeneratedMomFilePath;
}}
}
} else {
momFilePath = momOrXCDataModelFilePath;
}
Expand Down
50 changes: 50 additions & 0 deletions mogenerator.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@
79D2C05A0ACFBCB500F3F141 /* FoundationAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 79D2C0580ACFBCB500F3F141 /* FoundationAdditions.m */; };
8DD76F9A0486AA7600D96B5E /* mogenerator.m in Sources */ = {isa = PBXBuildFile; fileRef = 08FB7796FE84155DC02AAC07 /* mogenerator.m */; settings = {ATTRIBUTES = (); }; };
8DD76F9C0486AA7600D96B5E /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 08FB779EFE84155DC02AAC07 /* Foundation.framework */; };
EE5ED87317332E020013CCD1 /* NSManagedObjectModel+momcom.m in Sources */ = {isa = PBXBuildFile; fileRef = EE5ED86617332E020013CCD1 /* NSManagedObjectModel+momcom.m */; };
EE5ED87417332E020013CCD1 /* NSEntityDescription+momcom.m in Sources */ = {isa = PBXBuildFile; fileRef = EE5ED86817332E020013CCD1 /* NSEntityDescription+momcom.m */; };
EE5ED87517332E020013CCD1 /* NSAttributeDescription+momcom.m in Sources */ = {isa = PBXBuildFile; fileRef = EE5ED86A17332E020013CCD1 /* NSAttributeDescription+momcom.m */; };
EE5ED87617332E020013CCD1 /* NSPropertyDescription+momcom.m in Sources */ = {isa = PBXBuildFile; fileRef = EE5ED86C17332E020013CCD1 /* NSPropertyDescription+momcom.m */; };
EE5ED87717332E020013CCD1 /* NSRelationshipDescription+momcom.m in Sources */ = {isa = PBXBuildFile; fileRef = EE5ED86E17332E020013CCD1 /* NSRelationshipDescription+momcom.m */; };
EE5ED87817332E020013CCD1 /* NSFetchedPropertyDescription+momcom.m in Sources */ = {isa = PBXBuildFile; fileRef = EE5ED87017332E020013CCD1 /* NSFetchedPropertyDescription+momcom.m */; };
EE5ED87917332E020013CCD1 /* NSFetchRequest+momcom.m in Sources */ = {isa = PBXBuildFile; fileRef = EE5ED87217332E020013CCD1 /* NSFetchRequest+momcom.m */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand Down Expand Up @@ -168,6 +175,20 @@
79D2C0570ACFBCB500F3F141 /* FoundationAdditions.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = FoundationAdditions.h; sourceTree = "<group>"; };
79D2C0580ACFBCB500F3F141 /* FoundationAdditions.m */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.objc; path = FoundationAdditions.m; sourceTree = "<group>"; };
8DD76FA10486AA7600D96B5E /* mogenerator */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = mogenerator; sourceTree = BUILT_PRODUCTS_DIR; };
EE5ED86517332E020013CCD1 /* NSManagedObjectModel+momcom.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSManagedObjectModel+momcom.h"; sourceTree = "<group>"; };
EE5ED86617332E020013CCD1 /* NSManagedObjectModel+momcom.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSManagedObjectModel+momcom.m"; sourceTree = "<group>"; };
EE5ED86717332E020013CCD1 /* NSEntityDescription+momcom.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSEntityDescription+momcom.h"; sourceTree = "<group>"; };
EE5ED86817332E020013CCD1 /* NSEntityDescription+momcom.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSEntityDescription+momcom.m"; sourceTree = "<group>"; };
EE5ED86917332E020013CCD1 /* NSAttributeDescription+momcom.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSAttributeDescription+momcom.h"; sourceTree = "<group>"; };
EE5ED86A17332E020013CCD1 /* NSAttributeDescription+momcom.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSAttributeDescription+momcom.m"; sourceTree = "<group>"; };
EE5ED86B17332E020013CCD1 /* NSPropertyDescription+momcom.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSPropertyDescription+momcom.h"; sourceTree = "<group>"; };
EE5ED86C17332E020013CCD1 /* NSPropertyDescription+momcom.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSPropertyDescription+momcom.m"; sourceTree = "<group>"; };
EE5ED86D17332E020013CCD1 /* NSRelationshipDescription+momcom.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSRelationshipDescription+momcom.h"; sourceTree = "<group>"; };
EE5ED86E17332E020013CCD1 /* NSRelationshipDescription+momcom.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSRelationshipDescription+momcom.m"; sourceTree = "<group>"; };
EE5ED86F17332E020013CCD1 /* NSFetchedPropertyDescription+momcom.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSFetchedPropertyDescription+momcom.h"; sourceTree = "<group>"; };
EE5ED87017332E020013CCD1 /* NSFetchedPropertyDescription+momcom.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSFetchedPropertyDescription+momcom.m"; sourceTree = "<group>"; };
EE5ED87117332E020013CCD1 /* NSFetchRequest+momcom.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSFetchRequest+momcom.h"; sourceTree = "<group>"; };
EE5ED87217332E020013CCD1 /* NSFetchRequest+momcom.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSFetchRequest+momcom.m"; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand All @@ -188,6 +209,7 @@
isa = PBXGroup;
children = (
08FB7795FE84155DC02AAC07 /* Source */,
EE5ED86417332DED0013CCD1 /* momcom */,
79D2BF510ACFB51000F3F141 /* MiscMerge */,
457C26C6139A1EF900BF00DD /* ponso */,
55200E8E0C49FEEA00018A42 /* ddcli */,
Expand Down Expand Up @@ -345,6 +367,27 @@
path = MiscMerge;
sourceTree = "<group>";
};
EE5ED86417332DED0013CCD1 /* momcom */ = {
isa = PBXGroup;
children = (
EE5ED86517332E020013CCD1 /* NSManagedObjectModel+momcom.h */,
EE5ED86617332E020013CCD1 /* NSManagedObjectModel+momcom.m */,
EE5ED86717332E020013CCD1 /* NSEntityDescription+momcom.h */,
EE5ED86817332E020013CCD1 /* NSEntityDescription+momcom.m */,
EE5ED86917332E020013CCD1 /* NSAttributeDescription+momcom.h */,
EE5ED86A17332E020013CCD1 /* NSAttributeDescription+momcom.m */,
EE5ED86B17332E020013CCD1 /* NSPropertyDescription+momcom.h */,
EE5ED86C17332E020013CCD1 /* NSPropertyDescription+momcom.m */,
EE5ED86D17332E020013CCD1 /* NSRelationshipDescription+momcom.h */,
EE5ED86E17332E020013CCD1 /* NSRelationshipDescription+momcom.m */,
EE5ED86F17332E020013CCD1 /* NSFetchedPropertyDescription+momcom.h */,
EE5ED87017332E020013CCD1 /* NSFetchedPropertyDescription+momcom.m */,
EE5ED87117332E020013CCD1 /* NSFetchRequest+momcom.h */,
EE5ED87217332E020013CCD1 /* NSFetchRequest+momcom.m */,
);
path = momcom;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -459,6 +502,13 @@
457C26CD139A1EF900BF00DD /* MKCDAGNode.m in Sources */,
457C26CE139A1EF900BF00DD /* MKCNSEntityDescriptionAdditions.m in Sources */,
457C26CF139A1EF900BF00DD /* MKCNSManagedObjectModelAdditions.m in Sources */,
EE5ED87317332E020013CCD1 /* NSManagedObjectModel+momcom.m in Sources */,
EE5ED87417332E020013CCD1 /* NSEntityDescription+momcom.m in Sources */,
EE5ED87517332E020013CCD1 /* NSAttributeDescription+momcom.m in Sources */,
EE5ED87617332E020013CCD1 /* NSPropertyDescription+momcom.m in Sources */,
EE5ED87717332E020013CCD1 /* NSRelationshipDescription+momcom.m in Sources */,
EE5ED87817332E020013CCD1 /* NSFetchedPropertyDescription+momcom.m in Sources */,
EE5ED87917332E020013CCD1 /* NSFetchRequest+momcom.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
15 changes: 15 additions & 0 deletions momcom/NSAttributeDescription+momcom.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// NSAttributeDescription+momcom.h
// momc
//
// Created by Tom Harrington on 4/18/13.
// Copyright (c) 2013 Tom Harrington. All rights reserved.
//

#import <CoreData/CoreData.h>

@interface NSAttributeDescription (momcom)

+ (NSAttributeDescription *)baseEntityForXML:(NSXMLElement *)xmlNode;

@end
Loading

0 comments on commit 73fccca

Please sign in to comment.