From 991a25e0b431756df994144f060756b2993fc533 Mon Sep 17 00:00:00 2001 From: Adam Cox Date: Wed, 22 Jun 2011 10:14:36 +0100 Subject: [PATCH 1/7] Added the ability to pass key/value type arguments to DDGetoptLongParser * 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) --- ddcli/DDGetoptLongParser.h | 2 ++ ddcli/DDGetoptLongParser.m | 12 ++++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/ddcli/DDGetoptLongParser.h b/ddcli/DDGetoptLongParser.h index d61e4074..a0c09a6e 100644 --- a/ddcli/DDGetoptLongParser.h +++ b/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; /** diff --git a/ddcli/DDGetoptLongParser.m b/ddcli/DDGetoptLongParser.m index a383def1..fadafa65 100644 --- a/ddcli/DDGetoptLongParser.m +++ b/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,14 @@ - (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]]; + [mTarget setValue:[pair objectAtIndex:1] forKeyPath:keypath]; + } else [mTarget setValue: nsoptarg forKey: key]; } From 790a43624e4efd34c131c51f03336525eaba98af Mon Sep 17 00:00:00 2001 From: Adam Cox Date: Wed, 22 Jun 2011 10:21:01 +0100 Subject: [PATCH 2/7] Added a templateVar dictionary as a key value argument type --- mogenerator.h | 1 + mogenerator.m | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/mogenerator.h b/mogenerator.h index 0cf4da60..06046a3a 100644 --- a/mogenerator.h +++ b/mogenerator.h @@ -56,6 +56,7 @@ BOOL _version; BOOL _listSourceFiles; BOOL _orphaned; + NSMutableDictionary *templateVar; } - (NSString*)appSupportFileNamed:(NSString*)fileName_; diff --git a/mogenerator.m b/mogenerator.m index 2dff83a8..21a4ec3b 100644 --- a/mogenerator.m +++ b/mogenerator.m @@ -284,6 +284,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 +356,7 @@ - (void) application: (DDCliApplication *) app {@"help", 'h', DDGetoptNoArgument}, {@"version", 0, DDGetoptNoArgument}, + {@"template-var", 0, DDGetoptKeyValueArgument}, {nil, 0, 0}, }; [optionsParser addOptionsFromTable: optionTable]; From 130970662d959e468d68d0a6cee771e94d102b35 Mon Sep 17 00:00:00 2001 From: Adam Cox Date: Wed, 22 Jun 2011 11:02:46 +0100 Subject: [PATCH 3/7] Added switches in the template to check for use of ARC on the Entity Description. --- templates/machine.h.motemplate | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/templates/machine.h.motemplate b/templates/machine.h.motemplate index b3d55db2..ed956c7d 100644 --- a/templates/machine.h.motemplate +++ b/templates/machine.h.motemplate @@ -19,7 +19,11 @@ <$foreach Attribute noninheritedAttributes do$> <$if Attribute.hasDefinedAttributeType$> +<$if useARCQualifiers$> +@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 useARCQualifiers$> +@property (nonatomic, strong) NSSet* <$Relationship.name$>; +<$else$> @property (nonatomic, retain) NSSet* <$Relationship.name$>; +<$endif$> - (NSMutableSet*)<$Relationship.name$>Set; <$else$> +<$if useARCQualifiers$> +@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$> From a2bb6dbd15d5df2abc65f9ebdd22ecf915e38c81 Mon Sep 17 00:00:00 2001 From: Adam Cox Date: Wed, 22 Jun 2011 11:05:35 +0100 Subject: [PATCH 4/7] Inserted the template var dictionary into the merge engines as TemplateVar --- mogenerator.m | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/mogenerator.m b/mogenerator.m index 21a4ec3b..13819ccb 100644 --- a/mogenerator.m +++ b/mogenerator.m @@ -8,6 +8,7 @@ #import "mogenerator.h" #import "RegexKitLite.h" +static NSString *kTemplateVar = @"TemplateVar"; NSString *gCustomBaseClass; @interface NSEntityDescription (fetchedPropertiesAdditions) @@ -596,6 +597,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], From 7078754670f445f62a660dbd28a85497c80483a0 Mon Sep 17 00:00:00 2001 From: Adam Cox Date: Wed, 22 Jun 2011 11:06:44 +0100 Subject: [PATCH 5/7] Added the attempt to parse out the value type in a key/value argument pair. * Supports true/false * Supports numbers * Otherwise just a string --- ddcli/DDGetoptLongParser.m | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/ddcli/DDGetoptLongParser.m b/ddcli/DDGetoptLongParser.m index fadafa65..de3962bc 100644 --- a/ddcli/DDGetoptLongParser.m +++ b/ddcli/DDGetoptLongParser.m @@ -226,7 +226,29 @@ - (NSArray *) parseOptionsWithArguments: (NSArray *) arguments NSArray *pair = [nsoptarg componentsSeparatedByString:@"="]; // Build a keypath from the argument and the new key NSString *keypath = [NSString stringWithFormat:@"%@.%@", key, [pair objectAtIndex:0]]; - [mTarget setValue:[pair objectAtIndex:1] forKeyPath:keypath]; + + // 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]; From ebb98cf87291c67fc211e80c986326ca344d3f88 Mon Sep 17 00:00:00 2001 From: Adam Cox Date: Wed, 22 Jun 2011 11:07:20 +0100 Subject: [PATCH 6/7] Modified the template to use the new TemplateVar.arc qualifier --- templates/machine.h.motemplate | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/templates/machine.h.motemplate b/templates/machine.h.motemplate index ed956c7d..779ad084 100644 --- a/templates/machine.h.motemplate +++ b/templates/machine.h.motemplate @@ -19,7 +19,7 @@ <$foreach Attribute noninheritedAttributes do$> <$if Attribute.hasDefinedAttributeType$> -<$if useARCQualifiers$> +<$if TemplateVar.arc$> @property (nonatomic, strong) <$Attribute.objectAttributeType$> *<$Attribute.name$>; <$else$> @property (nonatomic, retain) <$Attribute.objectAttributeType$> *<$Attribute.name$>; @@ -34,14 +34,14 @@ <$endforeach do$> <$foreach Relationship noninheritedRelationships do$> <$if Relationship.isToMany$> -<$if useARCQualifiers$> +<$if TemplateVar.arc$> @property (nonatomic, strong) NSSet* <$Relationship.name$>; <$else$> @property (nonatomic, retain) NSSet* <$Relationship.name$>; <$endif$> - (NSMutableSet*)<$Relationship.name$>Set; <$else$> -<$if useARCQualifiers$> +<$if TemplateVar.arc$> @property (nonatomic, strong) <$Relationship.destinationEntity.managedObjectClassName$>* <$Relationship.name$>; <$else$> @property (nonatomic, retain) <$Relationship.destinationEntity.managedObjectClassName$>* <$Relationship.name$>; From 6728355b084fbba75e724af721d2f2578b1233cc Mon Sep 17 00:00:00 2001 From: Adam Cox Date: Wed, 22 Jun 2011 11:16:12 +0100 Subject: [PATCH 7/7] Added --template-var to the help message --- mogenerator.m | 1 + 1 file changed, 1 insertion(+) diff --git a/mogenerator.m b/mogenerator.m index 13819ccb..379e41fc 100644 --- a/mogenerator.m +++ b/mogenerator.m @@ -373,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"