|
| 1 | +// |
| 2 | +// MOMCompiler.m |
| 3 | +// XCDataModelPrinter |
| 4 | +// |
| 5 | +// Created by Chaitanya Gupta on 24/05/12. |
| 6 | +// Copyright (c) 2012 __MyCompanyName__. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import "MOMCompiler.h" |
| 10 | +#import "utils.h" |
| 11 | + |
| 12 | + |
| 13 | +#define MOMC_PATH_ENV_VAR @"MOMC_PATH" |
| 14 | +#define XCODE_4_3_2_MOMC_PATH @"/Applications/Xcode.app/Contents/Developer/usr/bin/momc" |
| 15 | +#define OLD_MOMC_PATH @"/usr/bin/momc" |
| 16 | + |
| 17 | + |
| 18 | +NSString *tempDirectoryPath() { |
| 19 | + NSURL *tmpURL = [NSURL fileURLWithPath:@"/tmp/"]; |
| 20 | + NSURL *url = [[NSFileManager defaultManager] URLForDirectory:NSItemReplacementDirectory |
| 21 | + inDomain:NSUserDomainMask |
| 22 | + appropriateForURL:tmpURL |
| 23 | + create:YES |
| 24 | + error:NULL]; |
| 25 | + return [url path]; |
| 26 | +} |
| 27 | + |
| 28 | +NSString *findFileInPaths(NSArray *testPaths) { |
| 29 | + for (NSString *testPath in testPaths) { |
| 30 | + if ([[NSFileManager defaultManager] fileExistsAtPath:testPath]) { |
| 31 | + return testPath; |
| 32 | + } |
| 33 | + } |
| 34 | + return nil; |
| 35 | +} |
| 36 | + |
| 37 | +@implementation MOMCompiler |
| 38 | + |
| 39 | +- (NSString *)compilePath:(NSString *)path { |
| 40 | + BOOL isDirectory; |
| 41 | + NSFileManager *dfm = [NSFileManager defaultManager]; |
| 42 | + if (![dfm fileExistsAtPath:path isDirectory:&isDirectory]) { |
| 43 | + NSPrintError(@"%@ does not exist", path); |
| 44 | + return nil; |
| 45 | + } |
| 46 | + NSString *tmpdir = tempDirectoryPath(); |
| 47 | + NSString *xcdatamodelDir; |
| 48 | + if (isDirectory) { |
| 49 | + xcdatamodelDir = path; |
| 50 | + } else { |
| 51 | + // assume .xcdatamodel/elements file is passed |
| 52 | + // create a .xcdatamodel directory inside tmpdir |
| 53 | + // and copy the elements file there |
| 54 | + xcdatamodelDir = [tmpdir stringByAppendingPathComponent:@"in.xcdatamodel"]; |
| 55 | + NSError *error; |
| 56 | + if (![dfm createDirectoryAtPath:xcdatamodelDir withIntermediateDirectories:YES attributes:nil error:&error]) { |
| 57 | + NSPrintError(@"Couldn't create temporary .xcdatamodel directory\n%@", error); |
| 58 | + return nil; |
| 59 | + } |
| 60 | + if (![dfm copyItemAtPath:path toPath:[xcdatamodelDir stringByAppendingPathComponent:@"elements"] error:&error]) { |
| 61 | + NSPrintError(@"Couldn't copy elements file\n%@", error); |
| 62 | + return nil; |
| 63 | + } |
| 64 | + } |
| 65 | + NSString *outfile = [tmpdir stringByAppendingPathComponent:@"out.mom"]; |
| 66 | + |
| 67 | + // Figure out the momc path |
| 68 | + NSMutableArray *trypaths = [NSMutableArray arrayWithObjects:XCODE_4_3_2_MOMC_PATH, OLD_MOMC_PATH, @"./momc", nil]; |
| 69 | + NSString *envMomcPath = [[[NSProcessInfo processInfo] environment] objectForKey:MOMC_PATH_ENV_VAR]; |
| 70 | + if (envMomcPath) { |
| 71 | + [trypaths insertObject:envMomcPath atIndex:0]; |
| 72 | + } |
| 73 | + NSString *momc = findFileInPaths(trypaths); |
| 74 | + if (momc == nil) { |
| 75 | + NSPrintError(@"Couldn't find momc"); |
| 76 | + return nil; |
| 77 | + } |
| 78 | + |
| 79 | + // Run momc to compile .xcdatamodel to .mom |
| 80 | + NSTask *task = [NSTask launchedTaskWithLaunchPath:momc arguments:[NSArray arrayWithObjects:xcdatamodelDir, outfile, nil]]; |
| 81 | + [task waitUntilExit]; |
| 82 | + int status = [task terminationStatus]; |
| 83 | + if (status != 0) { |
| 84 | + NSPrintError(@"momc exited with status: %d", status); |
| 85 | + return nil; |
| 86 | + } |
| 87 | + return outfile; |
| 88 | +} |
| 89 | + |
| 90 | +@end |
0 commit comments