Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Any interest in built-in templates? #79

Closed
wants to merge 1 commit into from

2 participants

Ingvar Nedrebo Jonathan 'Wolf' Rentzsch
Ingvar Nedrebo

You may or may not be interested in this update I just commited:

Compile and build the default templates into the mogenerator
executable, to make it standalone for people (like me) who are happy
with the default templates. For custom installed templates everything
of course works as before.

I do this with a custom build rule script that generates a .m file for each template,
containing an NSString with the template text. This string then becomes the
final fallback for the template search.

Ingvar Nedrebo iriz Build default templates into executable
Compile and build the default templates into the mogenerator
executable, to make it standalone for people (like me) who are happy
with the default templates. For custom installed templates everything
works as before.
023cb82
Jonathan 'Wolf' Rentzsch
Owner

Oops, I overlooked this one.

I like the idea of building default templates into the tool itself, but I think a better way would be to house them in mogenerator's binary as a Mach-O section, see http://stackoverflow.com/questions/7780789/reading-data-embedded-plist-linked-to-executable-through-sectcreate-text for how Apple embeds Info.plist's into standalone binaries.

I think we can do the same with mogenerator.

Ingvar Nedrebo

Interesting approach. I have implemented your suggestion, using the PlistBuddy tool to add the templates to Info.plist and then link that into the executable. I'll tidy up and commit in the next couple of days, so you can have a look and see what you think.

Jonathan 'Wolf' Rentzsch
Owner

Cool, thanks.

Ingvar Nedrebo

I have committed the Info.plist approach. There is an issue there, which is that I cannot find a way in Xcode to make the executable depend on the Info.plist file so that it's relinked when the file changes. Which means that if you make changes to any of the templates, and hit Build, the Info.plist file will be updated but not the tool.

For that reason, rather than pushing to master I created a branch: "infoplist". master still has my first approach, which does not have this dependency issue.

Jonathan 'Wolf' Rentzsch
Owner

Sorry I'm dragging my feet on this one -- I've been rather busy,

FYI I'll take a look at your fork and hopefully it can make v1.25.

Ingvar Nedrebo

No worries.

This was referenced
Jonathan 'Wolf' Rentzsch
Owner

Included in 1.26.

Jonathan 'Wolf' Rentzsch rentzsch closed this
Ingvar Nedrebo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 15, 2011
  1. Ingvar Nedrebo

    Build default templates into executable

    iriz authored
    Compile and build the default templates into the mogenerator
    executable, to make it standalone for people (like me) who are happy
    with the default templates. For custom installed templates everything
    works as before.
This page is out of date. Refresh to see the latest.
32 gentemplatesrc.py
View
@@ -0,0 +1,32 @@
+#!/usr/bin/python
+
+import argparse
+import os
+import re
+
+def produce_source_files(outpath, inpaths):
+
+ of = open(outpath, 'w')
+ of.write('#include <Foundation/Foundation.h>\n')
+
+ tmpl_names = map(os.path.basename, inpaths)
+ var_names = [s.replace('.', '_') for s in tmpl_names]
+
+ for path, name in zip(inpaths, var_names):
+ of.write('NSString * const MOBuiltin_%s = ' % name)
+
+ # escape quotes and backlashes
+ p = re.compile(r'("|\\)')
+ for line in open(path):
+ line = p.sub(r'\\\1', line.strip('\n'))
+ of.write('@"%s\\n"\n' % line)
+ of.write(';\n')
+
+if __name__ == "__main__":
+
+ parser = argparse.ArgumentParser()
+ parser.add_argument('-o', '--output')
+ parser.add_argument('input', nargs='+')
+ args = parser.parse_args()
+
+ produce_source_files(args.output, args.input)
56 mogenerator.m
View
@@ -300,12 +300,23 @@ - (NSString*)camelCaseString {
}
@end
-static MiscMergeEngine* engineWithTemplatePath(NSString *templatePath_) {
- MiscMergeTemplate *template = [[[MiscMergeTemplate alloc] init] autorelease];
- [template setStartDelimiter:@"<$" endDelimiter:@"$>"];
- [template parseContentsOfFile:templatePath_];
-
- return [[[MiscMergeEngine alloc] initWithTemplate:template] autorelease];
+// These are generated by a custom build rule.
+extern NSString * const MOBuiltin_machine_h_motemplate;
+extern NSString * const MOBuiltin_machine_m_motemplate;
+extern NSString * const MOBuiltin_human_h_motemplate;
+extern NSString * const MOBuiltin_human_m_motemplate;
+
+static NSString * builtinTemplate(NSString * templateName_) {
+ static NSDictionary * builtinTemplates = nil;
+ if (!builtinTemplates) {
+ builtinTemplates = [NSDictionary dictionaryWithObjectsAndKeys:
+ MOBuiltin_machine_h_motemplate, @"machine.h.motemplate",
+ MOBuiltin_machine_m_motemplate, @"machine.m.motemplate",
+ MOBuiltin_human_h_motemplate, @"human.h.motemplate",
+ MOBuiltin_human_m_motemplate, @"human.m.motemplate",
+ nil];
+ }
+ return [builtinTemplates objectForKey:templateName_];
}
@implementation MOGeneratorApp
@@ -330,7 +341,10 @@ - (NSString*)appSupportFileNamed:(NSString*)fileName_ {
if (templatePath) {
if ([fileManager fileExistsAtPath:templatePath isDirectory:&isDirectory] && isDirectory) {
- return [templatePath stringByAppendingPathComponent:fileName_];
+ NSString *templateFile = [templatePath stringByAppendingPathComponent:fileName_];
+ if ([fileManager fileExistsAtPath:templateFile isDirectory:&isDirectory] && !isDirectory) {
+ return templateFile;
+ }
}
} else {
NSArray *appSupportDirectories = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask+NSLocalDomainMask, YES);
@@ -350,13 +364,25 @@ - (NSString*)appSupportFileNamed:(NSString*)fileName_ {
}
}
}
- }
-
- ddprintf(@"appSupportFileNamed:@\"%@\": file not found", fileName_);
- exit(EXIT_FAILURE);
+ }
+ //ddprintf(@"appSupportFileNamed:@\"%@\": file not found", fileName_);
+ //exit(EXIT_FAILURE);
return nil;
}
+- (MiscMergeEngine*) engineWithTemplateName: (NSString *)templateName_ {
+ MiscMergeTemplate *template = [[[MiscMergeTemplate alloc] init] autorelease];
+ [template setStartDelimiter:@"<$" endDelimiter:@"$>"];
+
+ NSString *path = [self appSupportFileNamed:templateName_];
+ if (path)
+ [template parseContentsOfFile:path];
+ else
+ [template parseString:builtinTemplate(templateName_)];
+
+ return [[[MiscMergeEngine alloc] initWithTemplate:template] autorelease];
+}
+
- (void) application: (DDCliApplication *) app
willParseOptions: (DDGetoptLongParser *) optionsParser;
{
@@ -622,13 +648,13 @@ - (int) application: (DDCliApplication *) app
int humanFilesGenerated = 0;
if (model) {
- MiscMergeEngine *machineH = engineWithTemplatePath([self appSupportFileNamed:@"machine.h.motemplate"]);
+ MiscMergeEngine *machineH = [self engineWithTemplateName:@"machine.h.motemplate"];
assert(machineH);
- MiscMergeEngine *machineM = engineWithTemplatePath([self appSupportFileNamed:@"machine.m.motemplate"]);
+ MiscMergeEngine *machineM = [self engineWithTemplateName:@"machine.m.motemplate"];
assert(machineM);
- MiscMergeEngine *humanH = engineWithTemplatePath([self appSupportFileNamed:@"human.h.motemplate"]);
+ MiscMergeEngine *humanH = [self engineWithTemplateName:@"human.h.motemplate"];
assert(humanH);
- MiscMergeEngine *humanM = engineWithTemplatePath([self appSupportFileNamed:@"human.m.motemplate"]);
+ MiscMergeEngine *humanM = [self engineWithTemplateName:@"human.m.motemplate"];
assert(humanM);
// Add the template var dictionary to each of the merge engines
39 mogenerator.xcodeproj/project.pbxproj
View
@@ -7,6 +7,10 @@
objects = {
/* Begin PBXBuildFile section */
+ 190CB0BA149A719200537A17 /* human.h.motemplate in Sources */ = {isa = PBXBuildFile; fileRef = 190CB0B0149A70B000537A17 /* human.h.motemplate */; };
+ 190CB0BB149A719200537A17 /* human.m.motemplate in Sources */ = {isa = PBXBuildFile; fileRef = 190CB0B1149A70B000537A17 /* human.m.motemplate */; };
+ 190CB0BC149A719200537A17 /* machine.h.motemplate in Sources */ = {isa = PBXBuildFile; fileRef = 190CB0B2149A70B000537A17 /* machine.h.motemplate */; };
+ 190CB0BD149A719200537A17 /* machine.m.motemplate in Sources */ = {isa = PBXBuildFile; fileRef = 190CB0B3149A70B000537A17 /* machine.m.motemplate */; };
457C26CD139A1EF900BF00DD /* MKCDAGNode.m in Sources */ = {isa = PBXBuildFile; fileRef = 457C26C8139A1EF900BF00DD /* MKCDAGNode.m */; };
457C26CE139A1EF900BF00DD /* MKCNSEntityDescriptionAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 457C26CA139A1EF900BF00DD /* MKCNSEntityDescriptionAdditions.m */; };
457C26CF139A1EF900BF00DD /* MKCNSManagedObjectModelAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 457C26CC139A1EF900BF00DD /* MKCNSManagedObjectModelAdditions.m */; };
@@ -61,9 +65,27 @@
8DD76F9C0486AA7600D96B5E /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 08FB779EFE84155DC02AAC07 /* Foundation.framework */; };
/* End PBXBuildFile section */
+/* Begin PBXBuildRule section */
+ 190CB0B9149A714D00537A17 /* PBXBuildRule */ = {
+ isa = PBXBuildRule;
+ compilerSpec = com.apple.compilers.proxy.script;
+ filePatterns = "*.motemplate";
+ fileType = pattern.proxy;
+ isEditable = 1;
+ outputFiles = (
+ "$(DERIVED_FILES_DIR)/$(INPUT_FILE_BASE).m",
+ );
+ script = "./gentemplatesrc.py -o ${DERIVED_FILES_DIR}/${INPUT_FILE_BASE}.m ${INPUT_FILE_PATH}";
+ };
+/* End PBXBuildRule section */
+
/* Begin PBXFileReference section */
08FB7796FE84155DC02AAC07 /* mogenerator.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = mogenerator.m; sourceTree = "<group>"; usesTabs = 1; };
08FB779EFE84155DC02AAC07 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = /System/Library/Frameworks/Foundation.framework; sourceTree = "<absolute>"; };
+ 190CB0B0149A70B000537A17 /* human.h.motemplate */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = human.h.motemplate; sourceTree = "<group>"; };
+ 190CB0B1149A70B000537A17 /* human.m.motemplate */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = human.m.motemplate; sourceTree = "<group>"; };
+ 190CB0B2149A70B000537A17 /* machine.h.motemplate */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = machine.h.motemplate; sourceTree = "<group>"; };
+ 190CB0B3149A70B000537A17 /* machine.m.motemplate */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = machine.m.motemplate; sourceTree = "<group>"; };
32A70AAB03705E1F00C91783 /* mogenerator_Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = mogenerator_Prefix.pch; sourceTree = "<group>"; };
457C26C7139A1EF900BF00DD /* MKCDAGNode.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MKCDAGNode.h; sourceTree = "<group>"; };
457C26C8139A1EF900BF00DD /* MKCDAGNode.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MKCDAGNode.m; sourceTree = "<group>"; };
@@ -187,6 +209,7 @@
08FB7794FE84155DC02AAC07 /* mogenerator */ = {
isa = PBXGroup;
children = (
+ 190CB0AF149A70B000537A17 /* templates */,
08FB7795FE84155DC02AAC07 /* Source */,
79D2BF510ACFB51000F3F141 /* MiscMerge */,
457C26C6139A1EF900BF00DD /* ponso */,
@@ -222,6 +245,17 @@
name = "External Frameworks and Libraries";
sourceTree = "<group>";
};
+ 190CB0AF149A70B000537A17 /* templates */ = {
+ isa = PBXGroup;
+ children = (
+ 190CB0B0149A70B000537A17 /* human.h.motemplate */,
+ 190CB0B1149A70B000537A17 /* human.m.motemplate */,
+ 190CB0B2149A70B000537A17 /* machine.h.motemplate */,
+ 190CB0B3149A70B000537A17 /* machine.m.motemplate */,
+ );
+ path = templates;
+ sourceTree = "<group>";
+ };
1AB674ADFE9D54B511CA2CBB /* Products */ = {
isa = PBXGroup;
children = (
@@ -356,6 +390,7 @@
8DD76F9B0486AA7600D96B5E /* Frameworks */,
);
buildRules = (
+ 190CB0B9149A714D00537A17 /* PBXBuildRule */,
);
dependencies = (
);
@@ -391,6 +426,10 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
+ 190CB0BA149A719200537A17 /* human.h.motemplate in Sources */,
+ 190CB0BB149A719200537A17 /* human.m.motemplate in Sources */,
+ 190CB0BC149A719200537A17 /* machine.h.motemplate in Sources */,
+ 190CB0BD149A719200537A17 /* machine.m.motemplate in Sources */,
8DD76F9A0486AA7600D96B5E /* mogenerator.m in Sources */,
79D2BFA20ACFB51A00F3F141 /* _MiscMergeBreakCommand.m in Sources */,
79D2BFA40ACFB51A00F3F141 /* _MiscMergeCallCommand.m in Sources */,
Something went wrong with that request. Please try again.