Permalink
Browse files

[NEW] Build in default templates into the mogenerator binary itself.

  • Loading branch information...
1 parent 7ab15a6 commit 1493a4a6b928254ea86e49b63231f15e6cd96430 @rentzsch committed Apr 12, 2012
Showing with 112 additions and 16 deletions.
  1. +8 −0 build-mogenerator-Info-plist.sh
  2. +0 −2 mogenerator.h
  3. +75 −14 mogenerator.m
  4. +29 −0 mogenerator.xcodeproj/project.pbxproj
View
8 build-mogenerator-Info-plist.sh
@@ -0,0 +1,8 @@
+rm -f /tmp/mogenerator-Info.plist
+/usr/libexec/PlistBuddy \
+ -c "Clear" \
+ -c "Import :human.h.motemplate templates/human.h.motemplate" \
+ -c "Import :human.m.motemplate templates/human.m.motemplate" \
+ -c "Import :machine.h.motemplate templates/machine.h.motemplate" \
+ -c "Import :machine.m.motemplate templates/machine.m.motemplate" \
+ /tmp/mogenerator-Info.plist
View
2 mogenerator.h
@@ -70,6 +70,4 @@
BOOL _orphaned;
NSMutableDictionary *templateVar;
}
-
-- (NSString*)appSupportFileNamed:(NSString*)fileName_;
@end
View
89 mogenerator.m
@@ -363,10 +363,29 @@ - (NSString*)camelCaseString {
}
@end
-static MiscMergeEngine* engineWithTemplatePath(NSString *templatePath_) {
+@interface MogeneratorTemplateDesc : NSObject {
+ NSString *templateName;
+ NSString *templatePath;
+}
+- (id)initWithName:(NSString*)name_ path:(NSString*)path_;
+- (NSString*)templateName;
+- (void)setTemplateName:(NSString*)name_;
+- (NSString*)templatePath;
+- (void)setTemplatePath:(NSString*)path_;
+@end
+
+static MiscMergeEngine* engineWithTemplateDesc(MogeneratorTemplateDesc *templateDesc_) {
MiscMergeTemplate *template = [[[MiscMergeTemplate alloc] init] autorelease];
[template setStartDelimiter:@"<$" endDelimiter:@"$>"];
- [template parseContentsOfFile:templatePath_];
+ if ([templateDesc_ templatePath]) {
+ [template parseContentsOfFile:[templateDesc_ templatePath]];
+ } else {
+ NSData *templateData = [[NSBundle mainBundle] objectForInfoDictionaryKey:[templateDesc_ templateName]];
+ assert(templateData);
+ NSString *templateString = [[[NSString alloc] initWithData:templateData encoding:NSASCIIStringEncoding] autorelease];
+ [template setFilename:[@"x-__info_plist://" stringByAppendingString:[templateDesc_ templateName]]];
+ [template parseString:templateString];
+ }
return [[[MiscMergeEngine alloc] initWithTemplate:template] autorelease];
}
@@ -387,35 +406,36 @@ - (void)dealloc {
}
NSString *ApplicationSupportSubdirectoryName = @"mogenerator";
-- (NSString*)appSupportFileNamed:(NSString*)fileName_ {
+- (MogeneratorTemplateDesc*)templateDescNamed:(NSString*)fileName_ {
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL isDirectory;
if (templatePath) {
if ([fileManager fileExistsAtPath:templatePath isDirectory:&isDirectory] && isDirectory) {
- return [templatePath stringByAppendingPathComponent:fileName_];
+ return [[[MogeneratorTemplateDesc alloc] initWithName:fileName_
+ path:[templatePath stringByAppendingPathComponent:fileName_]] autorelease];
}
- } else {
+ } else if (templateGroup) {
NSArray *appSupportDirectories = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask+NSLocalDomainMask, YES);
assert(appSupportDirectories);
nsenumerate (appSupportDirectories, NSString*, appSupportDirectory) {
if ([fileManager fileExistsAtPath:appSupportDirectory isDirectory:&isDirectory]) {
NSString *appSupportSubdirectory = [appSupportDirectory stringByAppendingPathComponent:ApplicationSupportSubdirectoryName];
- if (templateGroup) {
- appSupportSubdirectory = [appSupportSubdirectory stringByAppendingPathComponent:templateGroup];
- }
+ appSupportSubdirectory = [appSupportSubdirectory stringByAppendingPathComponent:templateGroup];
if ([fileManager fileExistsAtPath:appSupportSubdirectory isDirectory:&isDirectory] && isDirectory) {
NSString *appSupportFile = [appSupportSubdirectory stringByAppendingPathComponent:fileName_];
if ([fileManager fileExistsAtPath:appSupportFile isDirectory:&isDirectory] && !isDirectory) {
- return appSupportFile;
+ return [[[MogeneratorTemplateDesc alloc] initWithName:fileName_ path:appSupportFile] autorelease];
}
}
}
}
+ } else {
+ return [[[MogeneratorTemplateDesc alloc] initWithName:fileName_ path:nil] autorelease];
}
- ddprintf(@"appSupportFileNamed:@\"%@\": file not found", fileName_);
+ ddprintf(@"templateDescNamed:@\"%@\": file not found", fileName_);
exit(EXIT_FAILURE);
return nil;
}
@@ -688,13 +708,13 @@ - (int) application: (DDCliApplication *) app
int humanFilesGenerated = 0;
if (model) {
- MiscMergeEngine *machineH = engineWithTemplatePath([self appSupportFileNamed:@"machine.h.motemplate"]);
+ MiscMergeEngine *machineH = engineWithTemplateDesc([self templateDescNamed:@"machine.h.motemplate"]);
assert(machineH);
- MiscMergeEngine *machineM = engineWithTemplatePath([self appSupportFileNamed:@"machine.m.motemplate"]);
+ MiscMergeEngine *machineM = engineWithTemplateDesc([self templateDescNamed:@"machine.m.motemplate"]);
assert(machineM);
- MiscMergeEngine *humanH = engineWithTemplatePath([self appSupportFileNamed:@"human.h.motemplate"]);
+ MiscMergeEngine *humanH = engineWithTemplateDesc([self templateDescNamed:@"human.h.motemplate"]);
assert(humanH);
- MiscMergeEngine *humanM = engineWithTemplatePath([self appSupportFileNamed:@"human.m.motemplate"]);
+ MiscMergeEngine *humanM = engineWithTemplateDesc([self templateDescNamed:@"human.m.motemplate"]);
assert(humanM);
// Add the template var dictionary to each of the merge engines
@@ -826,6 +846,47 @@ - (int) application: (DDCliApplication *) app
@end
+@implementation MogeneratorTemplateDesc
+
+- (id)initWithName:(NSString*)name_ path:(NSString*)path_ {
+ self = [super init];
+ if (self) {
+ templateName = [name_ retain];
+ templatePath = [path_ retain];
+ }
+ return self;
+}
+
+- (void)dealloc {
+ [templateName release];
+ [templatePath release];
+ [super dealloc];
+}
+
+- (NSString*)templateName {
+ return templateName;
+}
+
+- (void)setTemplateName:(NSString*)name_ {
+ if (templateName != name_) {
+ [templateName release];
+ templateName = [name_ retain];
+ }
+}
+
+- (NSString*)templatePath {
+ return templatePath;
+}
+
+- (void)setTemplatePath:(NSString*)path_ {
+ if (templatePath != path_) {
+ [templatePath release];
+ templatePath = [path_ retain];
+ }
+}
+
+@end
+
int main (int argc, char * const * argv)
{
return DDCliAppRunWithClass([MOGeneratorApp class]);
View
29 mogenerator.xcodeproj/project.pbxproj
@@ -352,6 +352,7 @@
isa = PBXNativeTarget;
buildConfigurationList = 1DEB927408733DD40010E9CD /* Build configuration list for PBXNativeTarget "mogenerator" */;
buildPhases = (
+ 7956A85B152E4A500081B5CA /* ShellScript */,
8DD76F990486AA7600D96B5E /* Sources */,
8DD76F9B0486AA7600D96B5E /* Frameworks */,
);
@@ -386,6 +387,22 @@
};
/* End PBXProject section */
+/* Begin PBXShellScriptBuildPhase section */
+ 7956A85B152E4A500081B5CA /* ShellScript */ = {
+ isa = PBXShellScriptBuildPhase;
+ buildActionMask = 2147483647;
+ files = (
+ );
+ inputPaths = (
+ );
+ outputPaths = (
+ );
+ runOnlyForDeploymentPostprocessing = 0;
+ shellPath = /bin/sh;
+ shellScript = "sh build-mogenerator-Info-plist.sh\n";
+ };
+/* End PBXShellScriptBuildPhase section */
+
/* Begin PBXSourcesBuildPhase section */
8DD76F990486AA7600D96B5E /* Sources */ = {
isa = PBXSourcesBuildPhase;
@@ -460,6 +477,12 @@
GCC_VERSION = com.apple.compilers.llvm.clang.1_0;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
INSTALL_PATH = "$(HOME)/bin";
+ OTHER_LDFLAGS = (
+ "-sectcreate",
+ __TEXT,
+ __info_plist,
+ "/tmp/mogenerator-Info.plist",
+ );
PRODUCT_NAME = mogenerator;
WARNING_CFLAGS = "-Wall";
};
@@ -477,6 +500,12 @@
GCC_VERSION = com.apple.compilers.llvm.clang.1_0;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
INSTALL_PATH = "$(HOME)/bin";
+ OTHER_LDFLAGS = (
+ "-sectcreate",
+ __TEXT,
+ __info_plist,
+ "/tmp/mogenerator-Info.plist",
+ );
PRODUCT_NAME = mogenerator;
WARNING_CFLAGS = "-Wall";
};

0 comments on commit 1493a4a

Please sign in to comment.