Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any interest in built-in templates? #79

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions gentemplatesrc.py
Original file line number Diff line number Diff line change
@@ -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 changes: 41 additions & 15 deletions mogenerator.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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;
{
Expand Down Expand Up @@ -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
Expand Down
39 changes: 39 additions & 0 deletions mogenerator.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -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>"; };
Expand Down Expand Up @@ -187,6 +209,7 @@
08FB7794FE84155DC02AAC07 /* mogenerator */ = {
isa = PBXGroup;
children = (
190CB0AF149A70B000537A17 /* templates */,
08FB7795FE84155DC02AAC07 /* Source */,
79D2BF510ACFB51000F3F141 /* MiscMerge */,
457C26C6139A1EF900BF00DD /* ponso */,
Expand Down Expand Up @@ -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 = (
Expand Down Expand Up @@ -356,6 +390,7 @@
8DD76F9B0486AA7600D96B5E /* Frameworks */,
);
buildRules = (
190CB0B9149A714D00537A17 /* PBXBuildRule */,
);
dependencies = (
);
Expand Down Expand Up @@ -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 */,
Expand Down