Skip to content

Commit

Permalink
ask xcode for the location of the current install
Browse files Browse the repository at this point in the history
  • Loading branch information
joshaber committed Mar 16, 2010
1 parent d6df0ee commit 663d53a
Showing 1 changed file with 60 additions and 34 deletions.
94 changes: 60 additions & 34 deletions mogenerator.m
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/*******************************************************************************
mogenerator.m - <http://github.com/rentzsch/mogenerator>
Copyright (c) 2006-2009 Jonathan 'Wolf' Rentzsch: <http://rentzsch.com>
Some rights reserved: <http://opensource.org/licenses/mit-license.php>
***************************************************************************/
mogenerator.m - <http://github.com/rentzsch/mogenerator>
Copyright (c) 2006-2009 Jonathan 'Wolf' Rentzsch: <http://rentzsch.com>
Some rights reserved: <http://opensource.org/licenses/mit-license.php>
***************************************************************************/

#import "mogenerator.h"

Expand Down Expand Up @@ -71,7 +71,7 @@ - (NSDictionary*)fetchRequestTemplates {

- (NSString *)_resolveKeyPathType:(NSString *)keyPath {
NSArray *components = [keyPath componentsSeparatedByString:@"."];

// Hope the set of keys in the key path consists of solely relationships. Abort otherwise

NSEntityDescription *entity = self;
Expand Down Expand Up @@ -186,7 +186,7 @@ - (BOOL)hasDefinedAttributeType {
}
- (NSString*)objectAttributeType {
#if MAC_OS_X_VERSION_MAX_ALLOWED < 1050
#define NSTransformableAttributeType 1800
#define NSTransformableAttributeType 1800
#endif
if ([self attributeType] == NSTransformableAttributeType) {
NSString *result = [[self userInfo] objectForKey:@"attributeValueClassName"];
Expand Down Expand Up @@ -261,23 +261,23 @@ - (void) application: (DDCliApplication *) app
[optionsParser setGetoptLongOnly: YES];
DDGetoptOption optionTable[] =
{
// Long Short Argument options
{@"model", 'm', DDGetoptRequiredArgument},
{@"base-class", 0, DDGetoptRequiredArgument},
// For compatibility:
{@"baseClass", 0, DDGetoptRequiredArgument},
{@"includem", 0, DDGetoptRequiredArgument},
{@"template-path", 0, DDGetoptRequiredArgument},
// For compatibility:
{@"templatePath", 0, DDGetoptRequiredArgument},
{@"output-dir", 'O', DDGetoptRequiredArgument},
{@"machine-dir", 'M', DDGetoptRequiredArgument},
{@"human-dir", 'H', DDGetoptRequiredArgument},
{@"template-group", 0, DDGetoptRequiredArgument},

{@"help", 'h', DDGetoptNoArgument},
{@"version", 0, DDGetoptNoArgument},
{nil, 0, 0},
// Long Short Argument options
{@"model", 'm', DDGetoptRequiredArgument},
{@"base-class", 0, DDGetoptRequiredArgument},
// For compatibility:
{@"baseClass", 0, DDGetoptRequiredArgument},
{@"includem", 0, DDGetoptRequiredArgument},
{@"template-path", 0, DDGetoptRequiredArgument},
// For compatibility:
{@"templatePath", 0, DDGetoptRequiredArgument},
{@"output-dir", 'O', DDGetoptRequiredArgument},
{@"machine-dir", 'M', DDGetoptRequiredArgument},
{@"human-dir", 'H', DDGetoptRequiredArgument},
{@"template-group", 0, DDGetoptRequiredArgument},
{@"help", 'h', DDGetoptNoArgument},
{@"version", 0, DDGetoptNoArgument},
{nil, 0, 0},
};
[optionsParser addOptionsFromTable: optionTable];
}
Expand All @@ -301,23 +301,49 @@ - (void) printUsage;
"Inspired by eogenerator.\n");
}

- (NSString *)currentXcodePath {
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:@"/usr/bin/xcode-select"];

NSArray *arguments = [NSArray arrayWithObjects:@"-print-path", @"/Developer", nil];
[task setArguments:arguments];

NSPipe *pipe = [NSPipe pipe];
[task setStandardOutput:pipe];

NSFileHandle *file = [pipe fileHandleForReading];
[task launch];

NSData *data = [file readDataToEndOfFile];

NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
[task release];

return [string autorelease];
}

- (void) setModel: (NSString *) path;
{
assert(!model); // Currently we only can load one model.

if( ![[NSFileManager defaultManager] fileExistsAtPath:path]){
NSString * reason = [NSString stringWithFormat: @"error loading file at %@: no such file exists", path];
DDCliParseException * e = [DDCliParseException parseExceptionWithReason: reason
exitCode: EX_NOINPUT];
@throw e;
}

if ([[path pathExtension] isEqualToString:@"xcdatamodel"]) {
// We've been handed a .xcdatamodel data model, transparently compile it into a .mom managed object model.

// Find where Xcode installed momc this week.
NSString *momc = nil;
if ([[NSFileManager defaultManager] fileExistsAtPath:@"/Developer/usr/bin/momc"]) { // Xcode 3.1 installs it here.

// Ask Xcode where it's installed.
NSString *defaultLocation = [NSString stringWithFormat:@"%@/usr/bin/momc", [self currentXcodePath]];
if([[NSFileManager defaultManager] fileExistsAtPath:defaultLocation]) {
momc = defaultLocation;
} else if ([[NSFileManager defaultManager] 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.
momc = @"/Library/Application Support/Apple/Developer Tools/Plug-ins/XDCoreDataModel.xdplugin/Contents/Resources/momc";
Expand Down Expand Up @@ -358,7 +384,7 @@ - (int) application: (DDCliApplication *) app
machineDir = outputDir;
if (humanDir == nil)
humanDir = outputDir;

NSFileManager *fm = [NSFileManager defaultManager];

int machineFilesGenerated = 0;
Expand Down Expand Up @@ -399,23 +425,23 @@ - (int) application: (DDCliApplication *) app
BOOL machineDirtied = NO;

NSString *machineHFileName = [machineDir stringByAppendingPathComponent:
[NSString stringWithFormat:@"_%@.h", entityClassName]];
[NSString stringWithFormat:@"_%@.h", entityClassName]];
if (![fm regularFileExistsAtPath:machineHFileName] || ![generatedMachineH isEqualToString:[NSString stringWithContentsOfFile:machineHFileName]]) {
// If the file doesn't exist or is different than what we just generated, write it out.
[generatedMachineH writeToFile:machineHFileName atomically:NO];
machineDirtied = YES;
machineFilesGenerated++;
}
NSString *machineMFileName = [machineDir stringByAppendingPathComponent:
[NSString stringWithFormat:@"_%@.m", entityClassName]];
[NSString stringWithFormat:@"_%@.m", entityClassName]];
if (![fm regularFileExistsAtPath:machineMFileName] || ![generatedMachineM isEqualToString:[NSString stringWithContentsOfFile:machineMFileName]]) {
// If the file doesn't exist or is different than what we just generated, write it out.
[generatedMachineM writeToFile:machineMFileName atomically:NO];
machineDirtied = YES;
machineFilesGenerated++;
}
NSString *humanHFileName = [humanDir stringByAppendingPathComponent:
[NSString stringWithFormat:@"%@.h", entityClassName]];
[NSString stringWithFormat:@"%@.h", entityClassName]];
if ([fm regularFileExistsAtPath:humanHFileName]) {
if (machineDirtied)
[fm touchPath:humanHFileName];
Expand All @@ -424,9 +450,9 @@ - (int) application: (DDCliApplication *) app
humanFilesGenerated++;
}
NSString *humanMFileName = [humanDir stringByAppendingPathComponent:
[NSString stringWithFormat:@"%@.m", entityClassName]];
[NSString stringWithFormat:@"%@.m", entityClassName]];
NSString *humanMMFileName = [humanDir stringByAppendingPathComponent:
[NSString stringWithFormat:@"%@.mm", entityClassName]];
[NSString stringWithFormat:@"%@.mm", entityClassName]];
if (![fm regularFileExistsAtPath:humanMFileName] && [fm regularFileExistsAtPath:humanMMFileName]) {
// Allow .mm human files as well as .m files.
humanMFileName = humanMMFileName;
Expand All @@ -441,7 +467,7 @@ - (int) application: (DDCliApplication *) app
}

[mfileContent appendFormat:@"#include \"%@\"\n#include \"%@\"\n",
[humanMFileName lastPathComponent], [machineMFileName lastPathComponent]];
[humanMFileName lastPathComponent], [machineMFileName lastPathComponent]];
}
}

Expand Down

0 comments on commit 663d53a

Please sign in to comment.