Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Custom Template Vars / ARC @property qualifiers #64

Merged
merged 8 commits into from

2 participants

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

@rentzsch rentzsch merged commit 88ab27b into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 22, 2011
  1. @extremeboredom

    Added the ability to pass key/value type arguments to DDGetoptLongParser

    extremeboredom authored
    	* Added a new argument option (DDGetoptKeyValueArgument)
    	* Parse into a keypath based on the argument and the key provided
    	* Set on the keypath (requires a dictionary property)
  2. @extremeboredom
  3. @extremeboredom
  4. @extremeboredom
  5. @extremeboredom

    Added the attempt to parse out the value type in a key/value argument…

    extremeboredom authored
    … pair.
    
    	* Supports true/false
    	* Supports numbers
    	* Otherwise just a string
  6. @extremeboredom
  7. @extremeboredom
  8. @extremeboredom
This page is out of date. Refresh to see the latest.
View
2  ddcli/DDGetoptLongParser.h
@@ -42,6 +42,8 @@ typedef enum DDGetoptArgumentOptions
DDGetoptOptionalArgument = optional_argument,
/** Option takes a mandatory argument */
DDGetoptRequiredArgument = required_argument,
+ /** Option takes a key-value pair argument */
+ DDGetoptKeyValueArgument,
} DDGetoptArgumentOptions;
/**
View
34 ddcli/DDGetoptLongParser.m
@@ -120,7 +120,7 @@ - (void) addLongOption: (NSString *) longOption
struct option * option = [self currentOption];
option->name = utf8String;
- option->has_arg = argumentOptions;
+ option->has_arg = argumentOptions == DDGetoptKeyValueArgument ? DDGetoptRequiredArgument : argumentOptions;
option->flag = NULL;
int shortOptionValue;
@@ -128,7 +128,7 @@ - (void) addLongOption: (NSString *) longOption
{
shortOptionValue = shortOption;
option->val = shortOption;
- if (argumentOptions == DDGetoptRequiredArgument)
+ if (argumentOptions == DDGetoptRequiredArgument || argumentOptions == DDGetoptKeyValueArgument)
[mOptionString appendFormat: @"%c:", shortOption];
else if (argumentOptions == DDGetoptOptionalArgument)
[mOptionString appendFormat: @"%c::", shortOption];
@@ -220,6 +220,36 @@ - (NSArray *) parseOptionsWithArguments: (NSArray *) arguments
int argumentOptions = [[optionInfo objectAtIndex: 1] intValue];
if (argumentOptions == DDGetoptNoArgument)
[mTarget setValue: [NSNumber numberWithBool: YES] forKey: key];
+ else if (argumentOptions == DDGetoptKeyValueArgument)
+ {
+ // Split the arguement on the '=' sign
+ NSArray *pair = [nsoptarg componentsSeparatedByString:@"="];
+ // Build a keypath from the argument and the new key
+ NSString *keypath = [NSString stringWithFormat:@"%@.%@", key, [pair objectAtIndex:0]];
+
+ // If it is a number or a boolean, we'll parse that out
+ NSString *value = [pair objectAtIndex:1];
+ id parsedValue = value;
+ // Looks like a boolean?
+ if ([value isCaseInsensitiveLike:@"true"] || [value isCaseInsensitiveLike:@"false"])
+ {
+ parsedValue = [NSNumber numberWithBool:[value boolValue]];
+ }
+ else
+ {
+ // Looks like a number?
+ NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
+ [formatter setAllowsFloats:YES];
+ NSNumber *numberValue = [formatter numberFromString:value];
+ if (numberValue)
+ {
+ parsedValue = numberValue;
+ }
+ [formatter release];
+ }
+
+ [mTarget setValue:parsedValue forKeyPath:keypath];
+ }
else
[mTarget setValue: nsoptarg forKey: key];
}
View
1  mogenerator.h
@@ -56,6 +56,7 @@
BOOL _version;
BOOL _listSourceFiles;
BOOL _orphaned;
+ NSMutableDictionary *templateVar;
}
- (NSString*)appSupportFileNamed:(NSString*)fileName_;
View
22 mogenerator.m
@@ -8,6 +8,7 @@
#import "mogenerator.h"
#import "RegexKitLite.h"
+static NSString *kTemplateVar = @"TemplateVar";
NSString *gCustomBaseClass;
@interface NSEntityDescription (fetchedPropertiesAdditions)
@@ -284,6 +285,19 @@ - (NSString*)camelCaseString {
@implementation MOGeneratorApp
+- (id)init {
+ self = [super init];
+ if (self) {
+ templateVar = [[NSMutableDictionary alloc] init];
+ }
+ return self;
+}
+
+- (void)dealloc {
+ [templateVar release];
+ [super dealloc];
+}
+
NSString *ApplicationSupportSubdirectoryName = @"mogenerator";
- (NSString*)appSupportFileNamed:(NSString*)fileName_ {
NSFileManager *fileManager = [NSFileManager defaultManager];
@@ -343,6 +357,7 @@ - (void) application: (DDCliApplication *) app
{@"help", 'h', DDGetoptNoArgument},
{@"version", 0, DDGetoptNoArgument},
+ {@"template-var", 0, DDGetoptKeyValueArgument},
{nil, 0, 0},
};
[optionsParser addOptionsFromTable: optionTable];
@@ -358,6 +373,7 @@ - (void) printUsage;
" --includeh FILE Generate aggregate include file for .h files for human generated source files only\n"
" --template-path PATH Path to templates (absolute or relative to model path)\n"
" --template-group NAME Name of template group\n"
+ " --template-var KEY=VALUE A key-value pair to pass to the template file. There can be many of these.\n"
" -O, --output-dir DIR Output directory\n"
" -M, --machine-dir DIR Output directory for machine files\n"
" -H, --human-dir DIR Output directory for human files\n"
@@ -582,6 +598,12 @@ - (int) application: (DDCliApplication *) app
MiscMergeEngine *humanM = engineWithTemplatePath([self appSupportFileNamed:@"human.m.motemplate"]);
assert(humanM);
+ // Add the template var dictionary to each of the merge engines
+ [machineH setEngineValue:templateVar forKey:kTemplateVar];
+ [machineM setEngineValue:templateVar forKey:kTemplateVar];
+ [humanH setEngineValue:templateVar forKey:kTemplateVar];
+ [humanM setEngineValue:templateVar forKey:kTemplateVar];
+
NSMutableArray *humanMFiles = [NSMutableArray array],
*humanHFiles = [NSMutableArray array],
*machineMFiles = [NSMutableArray array],
View
12 templates/machine.h.motemplate
@@ -19,7 +19,11 @@
<$foreach Attribute noninheritedAttributes do$>
<$if Attribute.hasDefinedAttributeType$>
+<$if TemplateVar.arc$>
+@property (nonatomic, strong) <$Attribute.objectAttributeType$> *<$Attribute.name$>;
+<$else$>
@property (nonatomic, retain) <$Attribute.objectAttributeType$> *<$Attribute.name$>;
+<$endif$>
<$if Attribute.hasScalarAttributeType$>
@property <$Attribute.scalarAttributeType$> <$Attribute.name$>Value;
- (<$Attribute.scalarAttributeType$>)<$Attribute.name$>Value;
@@ -30,10 +34,18 @@
<$endforeach do$>
<$foreach Relationship noninheritedRelationships do$>
<$if Relationship.isToMany$>
+<$if TemplateVar.arc$>
+@property (nonatomic, strong) NSSet* <$Relationship.name$>;
+<$else$>
@property (nonatomic, retain) NSSet* <$Relationship.name$>;
+<$endif$>
- (NSMutableSet*)<$Relationship.name$>Set;
<$else$>
+<$if TemplateVar.arc$>
+@property (nonatomic, strong) <$Relationship.destinationEntity.managedObjectClassName$>* <$Relationship.name$>;
+<$else$>
@property (nonatomic, retain) <$Relationship.destinationEntity.managedObjectClassName$>* <$Relationship.name$>;
+<$endif$>
//- (BOOL)validate<$Relationship.name.initialCapitalString$>:(id*)value_ error:(NSError**)error_;
<$endif$>
<$endforeach do$>
Something went wrong with that request. Please try again.