Skip to content

Commit

Permalink
Merge branch 'template_arguments'
Browse files Browse the repository at this point in the history
  • Loading branch information
extremeboredom committed Jun 22, 2011
2 parents cf6b185 + 6728355 commit 2d9c726
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 2 deletions.
2 changes: 2 additions & 0 deletions ddcli/DDGetoptLongParser.h
Expand Up @@ -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;

/**
Expand Down
34 changes: 32 additions & 2 deletions ddcli/DDGetoptLongParser.m
Expand Up @@ -120,15 +120,15 @@ - (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;
if (shortOption != 0)
{
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];
Expand Down Expand Up @@ -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];
}
Expand Down
1 change: 1 addition & 0 deletions mogenerator.h
Expand Up @@ -56,6 +56,7 @@
BOOL _version;
BOOL _listSourceFiles;
BOOL _orphaned;
NSMutableDictionary *templateVar;
}

- (NSString*)appSupportFileNamed:(NSString*)fileName_;
Expand Down
22 changes: 22 additions & 0 deletions mogenerator.m
Expand Up @@ -8,6 +8,7 @@
#import "mogenerator.h"
#import "RegexKitLite.h"

static NSString *kTemplateVar = @"TemplateVar";
NSString *gCustomBaseClass;

@interface NSEntityDescription (fetchedPropertiesAdditions)
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -343,6 +357,7 @@ - (void) application: (DDCliApplication *) app

{@"help", 'h', DDGetoptNoArgument},
{@"version", 0, DDGetoptNoArgument},
{@"template-var", 0, DDGetoptKeyValueArgument},
{nil, 0, 0},
};
[optionsParser addOptionsFromTable: optionTable];
Expand All @@ -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"
Expand Down Expand Up @@ -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],
Expand Down
12 changes: 12 additions & 0 deletions templates/machine.h.motemplate
Expand Up @@ -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;
Expand All @@ -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$>
Expand Down

0 comments on commit 2d9c726

Please sign in to comment.