Skip to content

Commit

Permalink
Merge pull request #62 from nzhuk/master
Browse files Browse the repository at this point in the history
[NEW] --includeh option generate aggregate header file for all Model classes. Useful if you have a complex, fine-grained data model with lot of entities
  • Loading branch information
rentzsch committed Jun 5, 2011
2 parents 2d0e0d9 + 7fa143d commit cf6b185
Show file tree
Hide file tree
Showing 14 changed files with 68 additions and 17 deletions.
1 change: 0 additions & 1 deletion contributed templates/Nikita Zhuk/ponso/README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ Some missing features include:
- Automatic setting of inverse one-to-one relationships in setters
- Support for many-to-many relationships
- Implementations of to-many relationships as ordered sets instead of arrays
- Cycle detection of strong relationships
- Detection of retain cycles caused by both relationship directions being non-transient

Feel free to fork & contribute.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
4584D40E139A9D360035BCA5 /* ModelDepartmentEmployee.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = ModelDepartmentEmployee.m; path = Sources/DataModel/ModelDepartmentEmployee.m; sourceTree = SOURCE_ROOT; };
4584D40F139A9D360035BCA5 /* ModelEmployee.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ModelEmployee.h; path = Sources/DataModel/ModelEmployee.h; sourceTree = SOURCE_ROOT; };
4584D410139A9D360035BCA5 /* ModelEmployee.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = ModelEmployee.m; path = Sources/DataModel/ModelEmployee.m; sourceTree = SOURCE_ROOT; };
45F78872139BAA1900010BD3 /* Model.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Model.h; path = Sources/DataModel/Model.h; sourceTree = SOURCE_ROOT; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -131,6 +132,7 @@
457C26AF139A188D00BF00DD /* Data Model */ = {
isa = PBXGroup;
children = (
45F78872139BAA1900010BD3 /* Model.h */,
4584D3F9139A9D360035BCA5 /* _ModelAssistant.h */,
4584D3FA139A9D360035BCA5 /* _ModelAssistant.m */,
4584D3FB139A9D360035BCA5 /* _ModelCompany.h */,
Expand Down Expand Up @@ -217,7 +219,7 @@
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "### Settings\n\nDATA_MODEL_FILE=\"$PROJECT_DIR/MyModel.xcdatamodeld/MyModel.xcdatamodel\"\nDATA_MODEL_SOURCE_DIR=\"$PROJECT_DIR/Sources/DataModel\"\n\nBASE_CLASS=\"ModelObject\"\n\nMOGENERATOR_DIR=\"$PROJECT_DIR/../../../../../\"\nMOGENERATOR_BIN=\"$MOGENERATOR_DIR/mogenerator\" ## Adjust this so that it points to a compiled mogenerator binary.\nMOGENERATOR_TEMPLATES=\"$MOGENERATOR_DIR/contributed templates/Nikita Zhuk/ponso/templates\"\n\n### Logic\n\nsource \"$MOGENERATOR_DIR/ponso/generate.sh\"\n";
shellScript = "### Settings\n\nDATA_MODEL_FILE=\"$PROJECT_DIR/MyModel.xcdatamodeld/MyModel.xcdatamodel\"\nDATA_MODEL_SOURCE_DIR=\"$PROJECT_DIR/Sources/DataModel\"\n\nBASE_CLASS=\"ModelObject\"\nAGGREGATE_HEADER=\"$DATA_MODEL_SOURCE_DIR/Model.h\"\n\nMOGENERATOR_DIR=\"$PROJECT_DIR/../../../../../\"\nMOGENERATOR_BIN=\"$MOGENERATOR_DIR/mogenerator\" ## Adjust this so that it points to a compiled mogenerator binary.\nMOGENERATOR_TEMPLATES=\"$MOGENERATOR_DIR/contributed templates/Nikita Zhuk/ponso/templates\"\n\n### Logic\n\nsource \"$MOGENERATOR_DIR/ponso/generate.sh\"\n";
};
/* End PBXShellScriptBuildPhase section */

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@

#ifdef __OBJC__
#import <Foundation/Foundation.h>
#import "Model.h"
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@
*/

#import <Foundation/Foundation.h>
#import "ModelCompany.h"
#import "ModelDepartment.h"
#import "ModelDepartmentAssistant.h"
#import "ModelDepartmentEmployee.h"
#import "ModelEmployee.h"
#import "ModelAssistant.h"
#import "Model.h"

int main (int argc, const char * argv[])
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#import "ModelAssistant.h"
#import "ModelCompany.h"
#import "ModelDepartment.h"
#import "ModelDepartmentAssistant.h"
#import "ModelDepartmentEmployee.h"
#import "ModelEmployee.h"

Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
//

#import "ModelDepartmentAssistant.h"
#import "ModelDepartment.h"
#import "ModelCompany.h"

@implementation ModelDepartmentAssistant

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
//

#import "ModelDepartmentEmployee.h"
#import "ModelDepartment.h"
#import "ModelCompany.h"

@implementation ModelDepartmentEmployee

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
//

#import "ModelEmployee.h"
#import "ModelCompany.h"

@implementation ModelEmployee

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

#import <Foundation/Foundation.h>
#import "ModelObject.h"

<$checkNonTransientRelationshipCycles $>
<$foreach Relationship noninheritedRelationships do$>@class <$Relationship.destinationEntity.managedObjectClassName$>;
<$endforeach do$>

Expand Down
1 change: 1 addition & 0 deletions mogenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
NSManagedObjectModel *model;
NSString *baseClass;
NSString *includem;
NSString *includeh;
NSString *templatePath;
NSString *outputDir;
NSString *machineDir;
Expand Down
21 changes: 19 additions & 2 deletions mogenerator.m
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ - (void) application: (DDCliApplication *) app
// For compatibility:
{@"baseClass", 0, DDGetoptRequiredArgument},
{@"includem", 0, DDGetoptRequiredArgument},
{@"includeh", 0, DDGetoptRequiredArgument},
{@"template-path", 0, DDGetoptRequiredArgument},
// For compatibility:
{@"templatePath", 0, DDGetoptRequiredArgument},
Expand All @@ -353,7 +354,8 @@ - (void) printUsage;
printf("\n"
" -m, --model MODEL Path to model\n"
" --base-class CLASS Custom base class\n"
" --includem FILE Generate aggregate include file\n"
" --includem FILE Generate aggregate include file for .m files for both human and machine generated source files\n"
" --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"
" -O, --output-dir DIR Output directory\n"
Expand Down Expand Up @@ -496,7 +498,10 @@ - (int) application: (DDCliApplication *) app

gCustomBaseClass = [baseClass retain];
NSString * mfilePath = includem;
NSString * hfilePath = includeh;

NSMutableString * mfileContent = [NSMutableString stringWithString:@""];
NSMutableString * hfileContent = [NSMutableString stringWithString:@""];

[self validateOutputPath:outputDir forType:@"Output"];
[self validateOutputPath:machineDir forType:@"Machine Output"];
Expand Down Expand Up @@ -657,6 +662,8 @@ - (int) application: (DDCliApplication *) app

[mfileContent appendFormat:@"#import \"%@\"\n#import \"%@\"\n",
[humanMFileName lastPathComponent], [machineMFileName lastPathComponent]];

[hfileContent appendFormat:@"#import \"%@\"\n", [humanHFileName lastPathComponent]];
}

if (_listSourceFiles) {
Expand All @@ -677,10 +684,20 @@ - (int) application: (DDCliApplication *) app
[mfileContent writeToFile:mfilePath atomically:NO encoding:NSUTF8StringEncoding error:nil];
mfileGenerated = YES;
}


bool hfileGenerated = NO;
if (hfilePath && ![hfileContent isEqualToString:@""]) {
[hfileContent writeToFile:hfilePath atomically:NO encoding:NSUTF8StringEncoding error:nil];
hfileGenerated = YES;
}

if (!_listSourceFiles) {
printf("%d machine files%s %d human files%s generated.\n", machineFilesGenerated,
(mfileGenerated ? "," : " and"), humanFilesGenerated, (mfileGenerated ? " and one include.m file" : ""));

if(hfileGenerated) {
printf("Aggregate header file was also generated to %s.\n", [hfilePath fileSystemRepresentation]);
}
}

return EXIT_SUCCESS;
Expand Down
4 changes: 4 additions & 0 deletions ponso/MKCNSEntityDescriptionAdditions.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@
/** @TypeInfo NSAttributeDescription */
@property(nonatomic, readonly) NSArray *noninheritedRelationshipsInIDKeyPathTopologicalOrder;

// Checks if the managed object model of the receiver contains cycles in non-transient relationships.
// If cycles are found, an exception is raised.
- (void)checkNonTransientRelationshipCycles;

@end
29 changes: 29 additions & 0 deletions ponso/MKCNSEntityDescriptionAdditions.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ - (BOOL)includeRelationship:(NSRelationshipDescription *)relationship

@end

@interface MKCNSRelationshipDescriptionNonTransientDependencyFilter : NSObject <MKCNSRelationshipDescriptionDependencyFilter> @end
@implementation MKCNSRelationshipDescriptionNonTransientDependencyFilter

- (BOOL)includeRelationship:(NSRelationshipDescription *)relationship
{
if([[relationship entity] isEqual:[relationship destinationEntity]])
return NO;

if([relationship isTransient])
return NO;

return YES;
}

@end

@implementation NSEntityDescription(MKCNSEntityDescriptionAdditions)

/** @TypeInfo NSAttributeDescription */
Expand Down Expand Up @@ -80,4 +96,17 @@ - (NSArray*)noninheritedRelationshipsInIDKeyPathTopologicalOrder
return sortedRelationships;
}

- (void)checkNonTransientRelationshipCycles
{
id dependencyFilter = [[[MKCNSRelationshipDescriptionNonTransientDependencyFilter alloc] init] autorelease];
NSArray *entities = [[self managedObjectModel] entitiesInTopologicalOrderUsingDependencyFilter:dependencyFilter];

if(entities == nil)
{
NSString *desc = @"Cycles were found in non-transient relationships.";

[[NSException exceptionWithName:@"StrongRelationshipCyclesFoundException" reason:desc userInfo:nil] raise];
}
}

@end
3 changes: 2 additions & 1 deletion ponso/generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ MOMC_SUPPRESS_INVERSE_TRANSIENT_ERROR=YES \
"$MOGENERATOR_BIN" \
-m "$DATA_MODEL_FILE" \
--base-class "$BASE_CLASS" \
--includeh "$AGGREGATE_HEADER" \
--template-path "$MOGENERATOR_TEMPLATES" \
-O "$DATA_MODEL_SOURCE_DIR"
-O "$DATA_MODEL_SOURCE_DIR" || exit 1

echo "Generated Data Model Classes to $DATA_MODEL_SOURCE_DIR"

0 comments on commit cf6b185

Please sign in to comment.