Changeset 317
- Timestamp:
- 08/05/08 10:02:20 (4 months ago)
- Files:
-
- trunk/cocoa/mogenerator/mogenerator.m (modified) (5 diffs)
- trunk/cocoa/mogenerator/mogenerator.xcodeproj/project.pbxproj (modified) (2 diffs)
- trunk/cocoa/mogenerator/mogeneratorTestMule/MOs/_ChildMO.h (modified) (1 diff)
- trunk/cocoa/mogenerator/mogeneratorTestMule/MOs/_ChildMO.m (modified) (1 diff)
- trunk/cocoa/mogenerator/mogeneratorTestMule/MOs/_HumanMO.h (modified) (1 diff)
- trunk/cocoa/mogenerator/mogeneratorTestMule/MOs/_HumanMO.m (modified) (1 diff)
- trunk/cocoa/mogenerator/mogeneratorTestMule/MOs/_ParentMO.h (modified) (1 diff)
- trunk/cocoa/mogenerator/mogeneratorTestMule/MOs/_ParentMO.m (modified) (1 diff)
- trunk/cocoa/mogenerator/mogeneratorTestMule/mogeneratorTestMule.xcodeproj/project.pbxproj (modified) (1 diff)
- trunk/cocoa/mogenerator/mogeneratorTestMule/mogeneratorTestMule_DataModel.xcdatamodel/elements (modified) (previous)
- trunk/cocoa/mogenerator/mogeneratorTestMule/mogeneratorTestMule_DataModel.xcdatamodel/layout (modified) (previous)
- trunk/cocoa/mogenerator/templates/machine.h.motemplate (modified) (1 diff)
- trunk/cocoa/mogenerator/templates/machine.m.motemplate (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/cocoa/mogenerator/mogenerator.m
r314 r317 6 6 ***************************************************************************/ 7 7 8 #import <Foundation/Foundation.h> 9 #import <CoreData/CoreData.h> 10 11 #import "MiscMergeTemplate.h" 12 #import "MiscMergeCommandBlock.h" 13 #import "MiscMergeEngine.h" 14 #import "FoundationAdditions.h" 15 #import "nsenumerate.h" 16 #import "NSString+MiscAdditions.h" 17 #import "DDCommandLineInterface.h" 8 #import "mogenerator.h" 18 9 19 10 NSString *gCustomBaseClass; 20 11 21 @interface NSEntityDescription (customBaseClass)22 - (BOOL)hasCustomSuperentity;23 - (NSString*)customSuperentity;24 @end25 12 @implementation NSEntityDescription (customBaseClass) 26 13 - (BOOL)hasCustomSuperentity { … … 62 49 } 63 50 } 51 52 #pragma mark Fetch Request support 53 54 - (NSDictionary*)fetchRequestTemplates { 55 // -[NSManagedObjectModel _fetchRequestTemplatesByName] is a private method, but it's the only way to get 56 // model fetch request templates without knowing their name ahead of time. rdar://problem/4901396 asks for 57 // a public method (-[NSManagedObjectModel fetchRequestTemplatesByName]) that does the same thing. 58 // If that request is fulfilled, this code won't need to be modified thanks to KVC lookup order magic. 59 // UPDATE: 10.5 now has a public -fetchRequestTemplatesByName method. 60 NSDictionary *fetchRequests = [[self managedObjectModel] valueForKey:@"fetchRequestTemplatesByName"]; 61 62 NSMutableDictionary *result = [NSMutableDictionary dictionaryWithCapacity:[fetchRequests count]]; 63 nsenumerate ([fetchRequests allKeys], NSString, fetchRequestName) { 64 NSFetchRequest *fetchRequest = [fetchRequests objectForKey:fetchRequestName]; 65 if ([fetchRequest entity] == self) { 66 [result setObject:fetchRequest forKey:fetchRequestName]; 67 } 68 } 69 return result; 70 } 71 - (void)_processPredicate:(NSPredicate*)predicate_ bindings:(NSMutableArray*)bindings_ { 72 if ([predicate_ isKindOfClass:[NSCompoundPredicate class]]) { 73 nsenumerate([(NSCompoundPredicate*)predicate_ subpredicates], NSPredicate, subpredicate) { 74 [self _processPredicate:subpredicate bindings:bindings_]; 75 } 76 } else { 77 assert([[(NSComparisonPredicate*)predicate_ leftExpression] expressionType] == NSKeyPathExpressionType); 78 NSExpression *lhs = [(NSComparisonPredicate*)predicate_ leftExpression]; 79 NSExpression *rhs = [(NSComparisonPredicate*)predicate_ rightExpression]; 80 switch([rhs expressionType]) { 81 case NSConstantValueExpressionType: 82 case NSEvaluatedObjectExpressionType: 83 case NSKeyPathExpressionType: 84 case NSFunctionExpressionType: 85 // Don't do anything with these. 86 break; 87 case NSVariableExpressionType: { 88 // TODO SHOULD Handle LHS keypaths. 89 90 NSString *type = nil; 91 92 NSAttributeDescription *attribute = [[self attributesByName] objectForKey:[lhs keyPath]]; 93 if (attribute) { 94 type = [attribute objectAttributeType]; 95 type = [type stringByAppendingString:@"*"]; 96 } else { 97 // Probably a relationship 98 assert(0&&"TODO: add relationship support"); 99 } 100 101 [bindings_ addObject:[NSDictionary dictionaryWithObjectsAndKeys: 102 [rhs variable], @"name", 103 type, @"type", 104 nil]]; 105 } break; 106 default: 107 assert(0 && "unknown NSExpression type"); 108 } 109 } 110 } 111 - (NSArray*)prettyFetchRequests { 112 NSDictionary *fetchRequests = [self fetchRequestTemplates]; 113 NSMutableArray *result = [NSMutableArray arrayWithCapacity:[fetchRequests count]]; 114 nsenumerate ([fetchRequests allKeys], NSString, fetchRequestName) { 115 NSFetchRequest *fetchRequest = [fetchRequests objectForKey:fetchRequestName]; 116 NSMutableArray *bindings = [NSMutableArray array]; 117 [self _processPredicate:[fetchRequest predicate] bindings:bindings]; 118 [result addObject:[NSDictionary dictionaryWithObjectsAndKeys: 119 fetchRequestName, @"name", 120 bindings, @"bindings", 121 nil]]; 122 } 123 return result; 124 } 64 125 @end 65 @interface NSAttributeDescription (scalarAttributeType) 66 - (BOOL)hasScalarAttributeType; 67 - (NSString*)scalarAttributeType; 68 - (BOOL)hasDefinedAttributeType; 69 - (NSString*)objectAttributeType; 70 @end 126 71 127 @implementation NSAttributeDescription (scalarAttributeType) 72 128 - (BOOL)hasScalarAttributeType { … … 123 179 } 124 180 @end 125 @interface NSString (camelCaseString) 126 - (NSString*)camelCaseString; 127 @end 181 128 182 @implementation NSString (camelCaseString) 129 183 - (NSString*)camelCaseString { … … 147 201 return [[[MiscMergeEngine alloc] initWithTemplate:template] autorelease]; 148 202 } 149 150 @interface MOGeneratorApp : NSObject <DDCliApplicationDelegate> {151 NSString *tempMOMPath;152 NSManagedObjectModel *model;153 NSString *baseClass;154 NSString *includem;155 NSString *templatePath;156 NSString *outputDir;157 NSString *machineDir;158 NSString *humanDir;159 NSString *templateGroup;160 BOOL _help;161 BOOL _version;162 }163 164 - (NSString*)appSupportFileNamed:(NSString*)fileName_;165 @end166 203 167 204 @implementation MOGeneratorApp … … 291 328 if (_version) 292 329 { 293 printf("mogenerator 1.1 0.1. By Jonathan 'Wolf' Rentzsch + friends.\n");330 printf("mogenerator 1.11. By Jonathan 'Wolf' Rentzsch + friends.\n"); 294 331 return EXIT_SUCCESS; 295 332 } trunk/cocoa/mogenerator/mogenerator.xcodeproj/project.pbxproj
r283 r317 70 70 55200F9B0C4A2CA800018A42 /* DDCliParseException.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = DDCliParseException.h; sourceTree = "<group>"; }; 71 71 55200F9C0C4A2CA800018A42 /* DDCliParseException.m */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.objc; path = DDCliParseException.m; sourceTree = "<group>"; }; 72 79A586510E48E8B400474C35 /* mogenerator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = mogenerator.h; sourceTree = "<group>"; }; 72 73 79D2BF3E0ACFB25B00F3F141 /* CoreData.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreData.framework; path = /System/Library/Frameworks/CoreData.framework; sourceTree = "<absolute>"; }; 73 74 79D2BF520ACFB51A00F3F141 /* _MiscMergeBreakCommand.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = _MiscMergeBreakCommand.h; path = ../MiscMerge/_MiscMergeBreakCommand.h; sourceTree = "<group>"; }; … … 185 186 children = ( 186 187 08FB7796FE84155DC02AAC07 /* mogenerator.m */, 188 79A586510E48E8B400474C35 /* mogenerator.h */, 187 189 79D2C00E0ACFB81200F3F141 /* nsenumerate.h */, 188 190 79D2C0570ACFBCB500F3F141 /* FoundationAdditions.h */, trunk/cocoa/mogenerator/mogeneratorTestMule/MOs/_ChildMO.h
r313 r317 35 35 36 36 37 37 38 @end trunk/cocoa/mogenerator/mogeneratorTestMule/MOs/_ChildMO.m
r313 r317 55 55 56 56 57 58 57 59 @end trunk/cocoa/mogenerator/mogeneratorTestMule/MOs/_HumanMO.h
r313 r317 47 47 48 48 49 50 + (NSArray*)fetchByHumanName:(NSManagedObjectContext*)moc_ humanName:(NSString*)humanName_; 51 + (NSArray*)fetchByHumanName:(NSManagedObjectContext*)moc_ humanName:(NSString*)humanName_ error:(NSError**)error_; 52 49 53 @end trunk/cocoa/mogenerator/mogeneratorTestMule/MOs/_HumanMO.m
r313 r317 75 75 76 76 77 78 79 + (NSArray*)fetchByHumanName:(NSManagedObjectContext*)moc_ humanName:(NSString*)humanName_ { 80 NSError *error = nil; 81 id result = [self fetchByHumanName:moc_ humanName:humanName_ error:&error]; 82 if (error) { 83 [NSApp presentError:error]; 84 } 85 return result; 86 } 87 + (NSArray*)fetchByHumanName:(NSManagedObjectContext*)moc_ humanName:(NSString*)humanName_ error:(NSError**)error_ { 88 NSError *error = nil; 89 90 NSManagedObjectModel *model = [[moc_ persistentStoreCoordinator] managedObjectModel]; 91 NSFetchRequest *fetchRequest = [model fetchRequestFromTemplateWithName:@"byHumanName" 92 substitutionVariables:[NSDictionary dictionaryWithObjectsAndKeys: 93 94 humanName_, @"humanName", 95 96 nil] 97 ]; 98 NSAssert(fetchRequest, @"Can't find fetch request named \"byHumanName\"."); 99 100 NSArray *result = [moc_ executeFetchRequest:fetchRequest error:&error]; 101 if (error_) *error_ = error; 102 return result; 103 } 104 77 105 @end trunk/cocoa/mogenerator/mogeneratorTestMule/MOs/_ParentMO.h
r313 r317 169 169 170 170 171 171 172 @end trunk/cocoa/mogenerator/mogeneratorTestMule/MOs/_ParentMO.m
r313 r317 364 364 365 365 366 367 366 368 @end trunk/cocoa/mogenerator/mogeneratorTestMule/mogeneratorTestMule.xcodeproj/project.pbxproj
r228 r317 168 168 isa = PBXProject; 169 169 buildConfigurationList = 26FC0A880875C7B200E6366F /* Build configuration list for PBXProject "mogeneratorTestMule" */; 170 compatibilityVersion = "Xcode 2.4"; 170 171 hasScannedForEncodings = 1; 171 172 mainGroup = 29B97314FDCFA39411CA2CEA /* mogeneratorTestMule */; trunk/cocoa/mogenerator/templates/machine.h.motemplate
r300 r317 48 48 <$endif$> 49 49 <$endforeach do$> 50 <$foreach FetchRequest prettyFetchRequests do$> 51 + (NSArray*)fetch<$FetchRequest.name.initialCapitalString$>:(NSManagedObjectContext*)moc_ <$foreach Binding FetchRequest.bindings do2$><$Binding.name$>:(<$Binding.type$>)<$Binding.name$>_<$endforeach do2$>; 52 + (NSArray*)fetch<$FetchRequest.name.initialCapitalString$>:(NSManagedObjectContext*)moc_ <$foreach Binding FetchRequest.bindings do2$><$Binding.name$>:(<$Binding.type$>)<$Binding.name$>_<$endforeach do2$> error:(NSError**)error_; 53 <$endforeach do$> 50 54 @end trunk/cocoa/mogenerator/templates/machine.m.motemplate
r314 r317 115 115 <$endif$> 116 116 <$endforeach do$> 117 118 <$foreach FetchRequest prettyFetchRequests do$> 119 + (NSArray*)fetch<$FetchRequest.name.initialCapitalString$>:(NSManagedObjectContext*)moc_ <$foreach Binding FetchRequest.bindings do2$><$Binding.name$>:(<$Binding.type$>)<$Binding.name$>_<$endforeach do2$> { 120 NSError *error = nil; 121 id result = [self fetch<$FetchRequest.name.initialCapitalString$>:moc_ <$foreach Binding FetchRequest.bindings do2$><$Binding.name$>:<$Binding.name$>_<$endforeach do2$> error:&error]; 122 if (error) { 123 [NSApp presentError:error]; 124 } 125 return result; 126 } 127 + (NSArray*)fetch<$FetchRequest.name.initialCapitalString$>:(NSManagedObjectContext*)moc_ <$foreach Binding FetchRequest.bindings do2$><$Binding.name$>:(<$Binding.type$>)<$Binding.name$>_<$endforeach do2$> error:(NSError**)error_ { 128 NSError *error = nil; 129 130 NSManagedObjectModel *model = [[moc_ persistentStoreCoordinator] managedObjectModel]; 131 NSFetchRequest *fetchRequest = [model fetchRequestFromTemplateWithName:@"<$FetchRequest.name$>" 132 substitutionVariables:[NSDictionary dictionaryWithObjectsAndKeys: 133 <$foreach Binding FetchRequest.bindings do2$> 134 <$Binding.name$>_, @"<$Binding.name$>", 135 <$endforeach do2$> 136 nil] 137 ]; 138 NSAssert(fetchRequest, @"Can't find fetch request named \"<$FetchRequest.name$>\"."); 139 140 NSArray *result = [moc_ executeFetchRequest:fetchRequest error:&error]; 141 if (error_) *error_ = error; 142 return result; 143 } 144 <$endforeach do$> 117 145 @end
