Skip to content
This repository

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 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.

Jonathan 'Wolf' Rentzsch rentzsch referenced this pull request February 16, 2012
Closed

Default templates? #91

Jonathan 'Wolf' Rentzsch
Owner

Included in 1.26.

Jonathan 'Wolf' Rentzsch rentzsch closed this April 12, 2012
Ingvar Nedrebo
iriz commented April 13, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Dec 15, 2011
Ingvar Nedrebo 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
This page is out of date. Refresh to see the latest.
32  gentemplatesrc.py
... ...
@@ -0,0 +1,32 @@
  1
+#!/usr/bin/python
  2
+
  3
+import argparse
  4
+import os
  5
+import re
  6
+
  7
+def produce_source_files(outpath, inpaths):
  8
+
  9
+    of = open(outpath, 'w')
  10
+    of.write('#include <Foundation/Foundation.h>\n')
  11
+
  12
+    tmpl_names = map(os.path.basename, inpaths)
  13
+    var_names = [s.replace('.', '_') for s in tmpl_names]
  14
+
  15
+    for path, name in zip(inpaths, var_names):    
  16
+        of.write('NSString * const MOBuiltin_%s = ' % name)
  17
+
  18
+        # escape quotes and backlashes
  19
+        p = re.compile(r'("|\\)')
  20
+        for line in open(path):
  21
+            line = p.sub(r'\\\1', line.strip('\n'))
  22
+            of.write('@"%s\\n"\n' % line)
  23
+        of.write(';\n')
  24
+
  25
+if __name__ == "__main__":
  26
+    
  27
+    parser = argparse.ArgumentParser()
  28
+    parser.add_argument('-o', '--output')
  29
+    parser.add_argument('input', nargs='+')
  30
+    args = parser.parse_args()
  31
+
  32
+    produce_source_files(args.output, args.input)
56  mogenerator.m
@@ -300,12 +300,23 @@ - (NSString*)camelCaseString {
300 300
 }
301 301
 @end
302 302
 
303  
-static MiscMergeEngine* engineWithTemplatePath(NSString *templatePath_) {
304  
-	MiscMergeTemplate *template = [[[MiscMergeTemplate alloc] init] autorelease];
305  
-	[template setStartDelimiter:@"<$" endDelimiter:@"$>"];
306  
-	[template parseContentsOfFile:templatePath_];
307  
-	
308  
-	return [[[MiscMergeEngine alloc] initWithTemplate:template] autorelease];
  303
+// These are generated by a custom build rule.
  304
+extern NSString * const MOBuiltin_machine_h_motemplate;
  305
+extern NSString * const MOBuiltin_machine_m_motemplate;
  306
+extern NSString * const MOBuiltin_human_h_motemplate;
  307
+extern NSString * const MOBuiltin_human_m_motemplate;
  308
+
  309
+static NSString * builtinTemplate(NSString * templateName_) {
  310
+    static NSDictionary * builtinTemplates = nil;
  311
+    if (!builtinTemplates) {
  312
+        builtinTemplates = [NSDictionary dictionaryWithObjectsAndKeys:
  313
+                            MOBuiltin_machine_h_motemplate, @"machine.h.motemplate",
  314
+                            MOBuiltin_machine_m_motemplate, @"machine.m.motemplate",
  315
+                            MOBuiltin_human_h_motemplate, @"human.h.motemplate",
  316
+                            MOBuiltin_human_m_motemplate, @"human.m.motemplate",
  317
+							nil];
  318
+    }
  319
+    return [builtinTemplates objectForKey:templateName_];
309 320
 }
310 321
 
311 322
 @implementation MOGeneratorApp
@@ -330,7 +341,10 @@ - (NSString*)appSupportFileNamed:(NSString*)fileName_ {
330 341
 	
331 342
 	if (templatePath) {
332 343
 		if ([fileManager fileExistsAtPath:templatePath isDirectory:&isDirectory] && isDirectory) {
333  
-			return [templatePath stringByAppendingPathComponent:fileName_];
  344
+			NSString *templateFile = [templatePath stringByAppendingPathComponent:fileName_];
  345
+			if ([fileManager fileExistsAtPath:templateFile isDirectory:&isDirectory] && !isDirectory) {
  346
+				return templateFile;
  347
+			}
334 348
 		}
335 349
 	} else {
336 350
 		NSArray *appSupportDirectories = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask+NSLocalDomainMask, YES);
@@ -350,13 +364,25 @@ - (NSString*)appSupportFileNamed:(NSString*)fileName_ {
350 364
 				}
351 365
 			}
352 366
 		}
353  
-	}
354  
-	
355  
-	ddprintf(@"appSupportFileNamed:@\"%@\": file not found", fileName_);
356  
-	exit(EXIT_FAILURE);
  367
+	}	
  368
+	//ddprintf(@"appSupportFileNamed:@\"%@\": file not found", fileName_);
  369
+	//exit(EXIT_FAILURE);
357 370
 	return nil;
358 371
 }
359 372
 
  373
+- (MiscMergeEngine*) engineWithTemplateName: (NSString *)templateName_ {
  374
+	MiscMergeTemplate *template = [[[MiscMergeTemplate alloc] init] autorelease];
  375
+	[template setStartDelimiter:@"<$" endDelimiter:@"$>"];
  376
+
  377
+	NSString *path = [self appSupportFileNamed:templateName_];
  378
+	if (path)
  379
+		[template parseContentsOfFile:path];
  380
+	else
  381
+		[template parseString:builtinTemplate(templateName_)];
  382
+	
  383
+	return [[[MiscMergeEngine alloc] initWithTemplate:template] autorelease];
  384
+}
  385
+
360 386
 - (void) application: (DDCliApplication *) app
361 387
     willParseOptions: (DDGetoptLongParser *) optionsParser;
362 388
 {
@@ -622,13 +648,13 @@ - (int) application: (DDCliApplication *) app
622 648
 	int humanFilesGenerated = 0;
623 649
 	
624 650
 	if (model) {
625  
-		MiscMergeEngine *machineH = engineWithTemplatePath([self appSupportFileNamed:@"machine.h.motemplate"]);
  651
+		MiscMergeEngine *machineH = [self engineWithTemplateName:@"machine.h.motemplate"];
626 652
 		assert(machineH);
627  
-		MiscMergeEngine *machineM = engineWithTemplatePath([self appSupportFileNamed:@"machine.m.motemplate"]);
  653
+		MiscMergeEngine *machineM = [self engineWithTemplateName:@"machine.m.motemplate"];
628 654
 		assert(machineM);
629  
-		MiscMergeEngine *humanH = engineWithTemplatePath([self appSupportFileNamed:@"human.h.motemplate"]);
  655
+		MiscMergeEngine *humanH = [self engineWithTemplateName:@"human.h.motemplate"];
630 656
 		assert(humanH);
631  
-		MiscMergeEngine *humanM = engineWithTemplatePath([self appSupportFileNamed:@"human.m.motemplate"]);
  657
+		MiscMergeEngine *humanM = [self engineWithTemplateName:@"human.m.motemplate"];
632 658
 		assert(humanM);
633 659
 		
634 660
 		// Add the template var dictionary to each of the merge engines
39  mogenerator.xcodeproj/project.pbxproj
@@ -7,6 +7,10 @@
7 7
 	objects = {
8 8
 
9 9
 /* Begin PBXBuildFile section */
  10
+		190CB0BA149A719200537A17 /* human.h.motemplate in Sources */ = {isa = PBXBuildFile; fileRef = 190CB0B0149A70B000537A17 /* human.h.motemplate */; };
  11
+		190CB0BB149A719200537A17 /* human.m.motemplate in Sources */ = {isa = PBXBuildFile; fileRef = 190CB0B1149A70B000537A17 /* human.m.motemplate */; };
  12
+		190CB0BC149A719200537A17 /* machine.h.motemplate in Sources */ = {isa = PBXBuildFile; fileRef = 190CB0B2149A70B000537A17 /* machine.h.motemplate */; };
  13
+		190CB0BD149A719200537A17 /* machine.m.motemplate in Sources */ = {isa = PBXBuildFile; fileRef = 190CB0B3149A70B000537A17 /* machine.m.motemplate */; };
10 14
 		457C26CD139A1EF900BF00DD /* MKCDAGNode.m in Sources */ = {isa = PBXBuildFile; fileRef = 457C26C8139A1EF900BF00DD /* MKCDAGNode.m */; };
11 15
 		457C26CE139A1EF900BF00DD /* MKCNSEntityDescriptionAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 457C26CA139A1EF900BF00DD /* MKCNSEntityDescriptionAdditions.m */; };
12 16
 		457C26CF139A1EF900BF00DD /* MKCNSManagedObjectModelAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 457C26CC139A1EF900BF00DD /* MKCNSManagedObjectModelAdditions.m */; };
@@ -61,9 +65,27 @@
61 65
 		8DD76F9C0486AA7600D96B5E /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 08FB779EFE84155DC02AAC07 /* Foundation.framework */; };
62 66
 /* End PBXBuildFile section */
63 67
 
  68
+/* Begin PBXBuildRule section */
  69
+		190CB0B9149A714D00537A17 /* PBXBuildRule */ = {
  70
+			isa = PBXBuildRule;
  71
+			compilerSpec = com.apple.compilers.proxy.script;
  72
+			filePatterns = "*.motemplate";
  73
+			fileType = pattern.proxy;
  74
+			isEditable = 1;
  75
+			outputFiles = (
  76
+				"$(DERIVED_FILES_DIR)/$(INPUT_FILE_BASE).m",
  77
+			);
  78
+			script = "./gentemplatesrc.py -o ${DERIVED_FILES_DIR}/${INPUT_FILE_BASE}.m ${INPUT_FILE_PATH}";
  79
+		};
  80
+/* End PBXBuildRule section */
  81
+
64 82
 /* Begin PBXFileReference section */
65 83
 		08FB7796FE84155DC02AAC07 /* mogenerator.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = mogenerator.m; sourceTree = "<group>"; usesTabs = 1; };
66 84
 		08FB779EFE84155DC02AAC07 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = /System/Library/Frameworks/Foundation.framework; sourceTree = "<absolute>"; };
  85
+		190CB0B0149A70B000537A17 /* human.h.motemplate */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = human.h.motemplate; sourceTree = "<group>"; };
  86
+		190CB0B1149A70B000537A17 /* human.m.motemplate */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = human.m.motemplate; sourceTree = "<group>"; };
  87
+		190CB0B2149A70B000537A17 /* machine.h.motemplate */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = machine.h.motemplate; sourceTree = "<group>"; };
  88
+		190CB0B3149A70B000537A17 /* machine.m.motemplate */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = machine.m.motemplate; sourceTree = "<group>"; };
67 89
 		32A70AAB03705E1F00C91783 /* mogenerator_Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = mogenerator_Prefix.pch; sourceTree = "<group>"; };
68 90
 		457C26C7139A1EF900BF00DD /* MKCDAGNode.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MKCDAGNode.h; sourceTree = "<group>"; };
69 91
 		457C26C8139A1EF900BF00DD /* MKCDAGNode.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MKCDAGNode.m; sourceTree = "<group>"; };
@@ -187,6 +209,7 @@
187 209
 		08FB7794FE84155DC02AAC07 /* mogenerator */ = {
188 210
 			isa = PBXGroup;
189 211
 			children = (
  212
+				190CB0AF149A70B000537A17 /* templates */,
190 213
 				08FB7795FE84155DC02AAC07 /* Source */,
191 214
 				79D2BF510ACFB51000F3F141 /* MiscMerge */,
192 215
 				457C26C6139A1EF900BF00DD /* ponso */,
@@ -222,6 +245,17 @@
222 245
 			name = "External Frameworks and Libraries";
223 246
 			sourceTree = "<group>";
224 247
 		};
  248
+		190CB0AF149A70B000537A17 /* templates */ = {
  249
+			isa = PBXGroup;
  250
+			children = (
  251
+				190CB0B0149A70B000537A17 /* human.h.motemplate */,
  252
+				190CB0B1149A70B000537A17 /* human.m.motemplate */,
  253
+				190CB0B2149A70B000537A17 /* machine.h.motemplate */,
  254
+				190CB0B3149A70B000537A17 /* machine.m.motemplate */,
  255
+			);
  256
+			path = templates;
  257
+			sourceTree = "<group>";
  258
+		};
225 259
 		1AB674ADFE9D54B511CA2CBB /* Products */ = {
226 260
 			isa = PBXGroup;
227 261
 			children = (
@@ -356,6 +390,7 @@
356 390
 				8DD76F9B0486AA7600D96B5E /* Frameworks */,
357 391
 			);
358 392
 			buildRules = (
  393
+				190CB0B9149A714D00537A17 /* PBXBuildRule */,
359 394
 			);
360 395
 			dependencies = (
361 396
 			);
@@ -391,6 +426,10 @@
391 426
 			isa = PBXSourcesBuildPhase;
392 427
 			buildActionMask = 2147483647;
393 428
 			files = (
  429
+				190CB0BA149A719200537A17 /* human.h.motemplate in Sources */,
  430
+				190CB0BB149A719200537A17 /* human.m.motemplate in Sources */,
  431
+				190CB0BC149A719200537A17 /* machine.h.motemplate in Sources */,
  432
+				190CB0BD149A719200537A17 /* machine.m.motemplate in Sources */,
394 433
 				8DD76F9A0486AA7600D96B5E /* mogenerator.m in Sources */,
395 434
 				79D2BFA20ACFB51A00F3F141 /* _MiscMergeBreakCommand.m in Sources */,
396 435
 				79D2BFA40ACFB51A00F3F141 /* _MiscMergeCallCommand.m in Sources */,
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.