Skip to content

Commit 6570ce4

Browse files
author
Chaitanya Gupta
committed
Add MOMCompiler.
1 parent 89cf6ca commit 6570ce4

File tree

5 files changed

+131
-1
lines changed

5 files changed

+131
-1
lines changed

XCDataModelPrinter.xcodeproj/project.pbxproj

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
/* Begin PBXBuildFile section */
1010
549A79D1156E2F560030310A /* utils.m in Sources */ = {isa = PBXBuildFile; fileRef = 549A79D0156E2F560030310A /* utils.m */; };
11+
549A79D4156E2FE80030310A /* MOMCompiler.m in Sources */ = {isa = PBXBuildFile; fileRef = 549A79D3156E2FE80030310A /* MOMCompiler.m */; };
1112
54CAE68E155E54D500C956CD /* MOMPrinter.m in Sources */ = {isa = PBXBuildFile; fileRef = 54CAE68D155E54D400C956CD /* MOMPrinter.m */; };
1213
54F505ED1550FC8F00D4A631 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 54F505EC1550FC8F00D4A631 /* Foundation.framework */; };
1314
54F505F01550FC8F00D4A631 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 54F505EF1550FC8F00D4A631 /* main.m */; };
@@ -30,6 +31,8 @@
3031
/* Begin PBXFileReference section */
3132
549A79CF156E2F560030310A /* utils.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = utils.h; sourceTree = "<group>"; };
3233
549A79D0156E2F560030310A /* utils.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = utils.m; sourceTree = "<group>"; };
34+
549A79D2156E2FE80030310A /* MOMCompiler.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MOMCompiler.h; sourceTree = "<group>"; };
35+
549A79D3156E2FE80030310A /* MOMCompiler.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MOMCompiler.m; sourceTree = "<group>"; };
3336
54CAE68C155E54D400C956CD /* MOMPrinter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MOMPrinter.h; sourceTree = "<group>"; };
3437
54CAE68D155E54D400C956CD /* MOMPrinter.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MOMPrinter.m; sourceTree = "<group>"; };
3538
54F505E81550FC8F00D4A631 /* XCDataModelPrinter */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = XCDataModelPrinter; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -86,11 +89,13 @@
8689
children = (
8790
54F5060C1551234400D4A631 /* NSData_Base64 */,
8891
54F505EF1550FC8F00D4A631 /* main.m */,
92+
549A79D2156E2FE80030310A /* MOMCompiler.h */,
93+
549A79D3156E2FE80030310A /* MOMCompiler.m */,
8994
54CAE68C155E54D400C956CD /* MOMPrinter.h */,
9095
54CAE68D155E54D400C956CD /* MOMPrinter.m */,
91-
54F505F11550FC8F00D4A631 /* Supporting Files */,
9296
549A79CF156E2F560030310A /* utils.h */,
9397
549A79D0156E2F560030310A /* utils.m */,
98+
54F505F11550FC8F00D4A631 /* Supporting Files */,
9499
);
95100
path = src;
96101
sourceTree = "<group>";
@@ -166,6 +171,7 @@
166171
54F5060F1551234400D4A631 /* NSData+Base64.m in Sources */,
167172
54CAE68E155E54D500C956CD /* MOMPrinter.m in Sources */,
168173
549A79D1156E2F560030310A /* utils.m in Sources */,
174+
549A79D4156E2FE80030310A /* MOMCompiler.m in Sources */,
169175
);
170176
runOnlyForDeploymentPostprocessing = 0;
171177
};

src/MOMCompiler.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// MOMCompiler.h
3+
// XCDataModelPrinter
4+
//
5+
// Created by Chaitanya Gupta on 24/05/12.
6+
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
@interface MOMCompiler : NSObject
12+
13+
- (NSString *)compilePath:(NSString *)path;
14+
15+
@end

src/MOMCompiler.m

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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

src/utils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@
1010

1111

1212
void NSPrintf(NSString *fmt, ...);
13+
void NSFPrintf(FILE *stream, NSString *fmt, ...);
14+
void NSPrintError(NSString *fmt, ...);

src/utils.m

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,20 @@ void NSPrintf(NSString *fmt, ...) {
1616
printf("%s", [outStr UTF8String]);
1717
va_end(args);
1818
}
19+
20+
void NSFPrintf(FILE *stream, NSString *fmt, ...) {
21+
va_list args;
22+
va_start(args, fmt);
23+
NSString *outStr = [[[NSString alloc] initWithFormat:fmt arguments:args] autorelease];
24+
fprintf(stream, "%s", [outStr UTF8String]);
25+
va_end(args);
26+
}
27+
28+
void NSPrintError(NSString *fmt, ...) {
29+
va_list args;
30+
va_start(args, fmt);
31+
NSString *processName = [[NSProcessInfo processInfo] processName];
32+
NSString *outStr = [[[NSString alloc] initWithFormat:fmt arguments:args] autorelease];
33+
fprintf(stderr, "%s: %s\n", [processName UTF8String], [outStr UTF8String]);
34+
va_end(args);
35+
}

0 commit comments

Comments
 (0)