Skip to content
This repository

Custom Template Vars / ARC @property qualifiers #64

Merged
merged 8 commits into from almost 3 years ago

2 participants

extremeboredom Jonathan 'Wolf' Rentzsch
extremeboredom

Added --template-var KEY=VALUE arguments on the command line to pass directly to the templates.

Default templates now check for the --template-var arc=true to switch to ARC-style property qualifiers.

As discussed in Issue 63.

Jonathan 'Wolf' Rentzsch rentzsch merged commit 88ab27b into from June 22, 2011
Jonathan 'Wolf' Rentzsch rentzsch closed this June 22, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
2  ddcli/DDGetoptLongParser.h
@@ -42,6 +42,8 @@ typedef enum DDGetoptArgumentOptions
42 42
     DDGetoptOptionalArgument = optional_argument,
43 43
     /** Option takes a mandatory argument */
44 44
     DDGetoptRequiredArgument = required_argument,
  45
+    /** Option takes a key-value pair argument */
  46
+    DDGetoptKeyValueArgument,
45 47
 } DDGetoptArgumentOptions;
46 48
 
47 49
 /**
34  ddcli/DDGetoptLongParser.m
@@ -120,7 +120,7 @@ - (void) addLongOption: (NSString *) longOption
120 120
     
121 121
     struct option * option = [self currentOption];
122 122
     option->name = utf8String;
123  
-    option->has_arg = argumentOptions;
  123
+    option->has_arg = argumentOptions == DDGetoptKeyValueArgument ? DDGetoptRequiredArgument : argumentOptions;
124 124
     option->flag = NULL;
125 125
 
126 126
     int shortOptionValue;
@@ -128,7 +128,7 @@ - (void) addLongOption: (NSString *) longOption
128 128
     {
129 129
         shortOptionValue = shortOption;
130 130
         option->val = shortOption;
131  
-        if (argumentOptions == DDGetoptRequiredArgument)
  131
+        if (argumentOptions == DDGetoptRequiredArgument || argumentOptions == DDGetoptKeyValueArgument)
132 132
             [mOptionString appendFormat: @"%c:", shortOption];
133 133
         else if (argumentOptions == DDGetoptOptionalArgument)
134 134
             [mOptionString appendFormat: @"%c::", shortOption];
@@ -220,6 +220,36 @@ - (NSArray *) parseOptionsWithArguments: (NSArray *) arguments
220 220
             int argumentOptions = [[optionInfo objectAtIndex: 1] intValue];
221 221
             if (argumentOptions == DDGetoptNoArgument)
222 222
                 [mTarget setValue: [NSNumber numberWithBool: YES] forKey: key];
  223
+            else if (argumentOptions == DDGetoptKeyValueArgument)
  224
+            {
  225
+                // Split the arguement on the '=' sign
  226
+                NSArray *pair = [nsoptarg componentsSeparatedByString:@"="];
  227
+                // Build a keypath from the argument and the new key
  228
+                NSString *keypath = [NSString stringWithFormat:@"%@.%@", key, [pair objectAtIndex:0]];
  229
+                
  230
+                // If it is a number or a boolean, we'll parse that out
  231
+                NSString *value = [pair objectAtIndex:1];
  232
+                id parsedValue = value;
  233
+                // Looks like a boolean?
  234
+                if ([value isCaseInsensitiveLike:@"true"] || [value isCaseInsensitiveLike:@"false"])
  235
+                {
  236
+                    parsedValue = [NSNumber numberWithBool:[value boolValue]];
  237
+                }
  238
+                else
  239
+                {
  240
+                    // Looks like a number?
  241
+                    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
  242
+                    [formatter setAllowsFloats:YES];
  243
+                    NSNumber *numberValue = [formatter numberFromString:value];
  244
+                    if (numberValue)
  245
+                    {
  246
+                        parsedValue = numberValue;
  247
+                    }
  248
+                    [formatter release];
  249
+                }
  250
+                
  251
+                [mTarget setValue:parsedValue forKeyPath:keypath];
  252
+            }
223 253
             else
224 254
                 [mTarget setValue: nsoptarg forKey: key];
225 255
         }
1  mogenerator.h
@@ -56,6 +56,7 @@
56 56
 	BOOL					_version;
57 57
 	BOOL					_listSourceFiles;
58 58
     BOOL					_orphaned;
  59
+    NSMutableDictionary     *templateVar;
59 60
 }
60 61
 
61 62
 - (NSString*)appSupportFileNamed:(NSString*)fileName_;
22  mogenerator.m
@@ -8,6 +8,7 @@
8 8
 #import "mogenerator.h"
9 9
 #import "RegexKitLite.h"
10 10
 
  11
+static NSString *kTemplateVar = @"TemplateVar";
11 12
 NSString	*gCustomBaseClass;
12 13
 
13 14
 @interface NSEntityDescription (fetchedPropertiesAdditions)
@@ -284,6 +285,19 @@ - (NSString*)camelCaseString {
284 285
 
285 286
 @implementation MOGeneratorApp
286 287
 
  288
+- (id)init {
  289
+    self = [super init];
  290
+    if (self) {
  291
+        templateVar = [[NSMutableDictionary alloc] init];
  292
+    }
  293
+    return self;
  294
+}
  295
+
  296
+- (void)dealloc {
  297
+    [templateVar release];
  298
+    [super dealloc];
  299
+}
  300
+
287 301
 NSString *ApplicationSupportSubdirectoryName = @"mogenerator";
288 302
 - (NSString*)appSupportFileNamed:(NSString*)fileName_ {
289 303
 	NSFileManager *fileManager = [NSFileManager defaultManager];
@@ -343,6 +357,7 @@ - (void) application: (DDCliApplication *) app
343 357
 
344 358
     {@"help",				'h',    DDGetoptNoArgument},
345 359
     {@"version",			0,      DDGetoptNoArgument},
  360
+	{@"template-var",		0,      DDGetoptKeyValueArgument},
346 361
     {nil,					0,      0},
347 362
     };
348 363
     [optionsParser addOptionsFromTable: optionTable];
@@ -358,6 +373,7 @@ - (void) printUsage;
358 373
            "      --includeh FILE           Generate aggregate include file for .h files for human generated source files only\n"
359 374
            "      --template-path PATH      Path to templates (absolute or relative to model path)\n"
360 375
            "      --template-group NAME     Name of template group\n"
  376
+		   "      --template-var KEY=VALUE  A key-value pair to pass to the template file. There can be many of these.\n"
361 377
            "  -O, --output-dir DIR          Output directory\n"
362 378
            "  -M, --machine-dir DIR         Output directory for machine files\n"
363 379
            "  -H, --human-dir DIR           Output directory for human files\n"
@@ -582,6 +598,12 @@ - (int) application: (DDCliApplication *) app
582 598
 		MiscMergeEngine *humanM = engineWithTemplatePath([self appSupportFileNamed:@"human.m.motemplate"]);
583 599
 		assert(humanM);
584 600
 		
  601
+		// Add the template var dictionary to each of the merge engines
  602
+		[machineH setEngineValue:templateVar forKey:kTemplateVar];
  603
+		[machineM setEngineValue:templateVar forKey:kTemplateVar];
  604
+		[humanH setEngineValue:templateVar forKey:kTemplateVar];
  605
+		[humanM setEngineValue:templateVar forKey:kTemplateVar];
  606
+		
585 607
 		NSMutableArray	*humanMFiles = [NSMutableArray array],
586 608
 						*humanHFiles = [NSMutableArray array],
587 609
 						*machineMFiles = [NSMutableArray array],
12  templates/machine.h.motemplate
@@ -19,7 +19,11 @@
19 19
 
20 20
 <$foreach Attribute noninheritedAttributes do$>
21 21
 <$if Attribute.hasDefinedAttributeType$>
  22
+<$if TemplateVar.arc$>
  23
+@property (nonatomic, strong) <$Attribute.objectAttributeType$> *<$Attribute.name$>;
  24
+<$else$>
22 25
 @property (nonatomic, retain) <$Attribute.objectAttributeType$> *<$Attribute.name$>;
  26
+<$endif$>
23 27
 <$if Attribute.hasScalarAttributeType$>
24 28
 @property <$Attribute.scalarAttributeType$> <$Attribute.name$>Value;
25 29
 - (<$Attribute.scalarAttributeType$>)<$Attribute.name$>Value;
@@ -30,10 +34,18 @@
30 34
 <$endforeach do$>
31 35
 <$foreach Relationship noninheritedRelationships do$>
32 36
 <$if Relationship.isToMany$>
  37
+<$if TemplateVar.arc$>
  38
+@property (nonatomic, strong) NSSet* <$Relationship.name$>;
  39
+<$else$>
33 40
 @property (nonatomic, retain) NSSet* <$Relationship.name$>;
  41
+<$endif$>
34 42
 - (NSMutableSet*)<$Relationship.name$>Set;
35 43
 <$else$>
  44
+<$if TemplateVar.arc$>
  45
+@property (nonatomic, strong) <$Relationship.destinationEntity.managedObjectClassName$>* <$Relationship.name$>;
  46
+<$else$>
36 47
 @property (nonatomic, retain) <$Relationship.destinationEntity.managedObjectClassName$>* <$Relationship.name$>;
  48
+<$endif$>
37 49
 //- (BOOL)validate<$Relationship.name.initialCapitalString$>:(id*)value_ error:(NSError**)error_;
38 50
 <$endif$>
39 51
 <$endforeach do$>
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.