From b10ff4a7e3f60a9a14207c27694c609ed3874934 Mon Sep 17 00:00:00 2001 From: Martin Kim Dung-Pham Date: Mon, 24 Sep 2018 12:11:58 +0200 Subject: [PATCH 1/6] Add argument for ignoring entities during class generation --- mogenerator.h | 1 + mogenerator.m | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/mogenerator.h b/mogenerator.h index 1d5761d8..5aed9c97 100644 --- a/mogenerator.h +++ b/mogenerator.h @@ -67,6 +67,7 @@ NSString *baseClass; NSString *baseClassImport; NSString *baseClassForce; + NSString *ignoredEntities; NSString *includem; NSString *includeh; NSString *templatePath; diff --git a/mogenerator.m b/mogenerator.m index 241621cf..0139e1d4 100644 --- a/mogenerator.m +++ b/mogenerator.m @@ -796,6 +796,7 @@ - (void)application:(DDCliApplication*)app {@"base-class", 0, DDGetoptRequiredArgument}, {@"base-class-import", 0, DDGetoptRequiredArgument}, {@"base-class-force", 0, DDGetoptRequiredArgument}, + {@"ignored-entities", 0, DDGetoptRequiredArgument}, // For compatibility: {@"baseClass", 0, DDGetoptRequiredArgument}, {@"includem", 0, DDGetoptRequiredArgument}, @@ -850,6 +851,7 @@ - (void)printUsage { "--base-class-force CLASS Same as --base-class except will force all entities to\n" " have the specified base class. Even if a super entity\n" " exists\n" + "--ignored-entities LIST Comma separated list of of entity names. Entities will be ignored when generating classes\n" "--includem FILE Generate aggregate include file for .m files for both\n" " human and machine generated source files\n" "--includeh FILE Generate aggregate include file for .h files for human\n" @@ -1046,6 +1048,11 @@ - (void)validateOutputPath:(NSString*)path forType:(NSString*)type } } +- (BOOL)isIgnoredEntity:(NSString *)entityName { + NSArray *entities = [ignoredEntities componentsSeparatedByString:@","]; + return [entities containsObject:entityName] || [entities containsObject:[NSString stringWithFormat:@"_%@", entityName]]; +} + - (int)application:(DDCliApplication*)app runWithArguments:(NSArray*)arguments { if (_help) { [self printUsage]; @@ -1192,6 +1199,9 @@ - (int)application:(DDCliApplication*)app runWithArguments:(NSArray*)arguments { NSArray *entitiesWithCustomSubclass = [model entitiesWithACustomSubclassInConfiguration:configuration verbose:YES]; for (NSEntityDescription *entity in entitiesWithCustomSubclass) { + if ([self isIgnoredEntity:entity.name]) { + continue; + } NSString *generatedMachineH = [machineH executeWithObject:entity sender:nil]; NSString *generatedMachineM = [machineM executeWithObject:entity sender:nil]; NSString *generatedHumanH = [humanH executeWithObject:entity sender:nil]; From 00aa081984ab477939100c4f975d3f6b1cb66067 Mon Sep 17 00:00:00 2001 From: Martin Kim Dung-Pham Date: Tue, 25 Sep 2018 08:07:11 +0200 Subject: [PATCH 2/6] Add `Aunt/Uncle` test for `--ignored-entities` parameter --- test/Rakefile | 14 +++++++++++++- test/test.xcdatamodel/contents | 4 ++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/test/Rakefile b/test/Rakefile index 6f04d33f..60625cff 100644 --- a/test/Rakefile +++ b/test/Rakefile @@ -26,7 +26,9 @@ run_or_die 'xcodebuild -project ../mogenerator.xcodeproj -scheme mogenerator cle run_or_die 'xcodebuild -project ../mogenerator.xcodeproj -scheme mogenerator' def gen_and_compile_objc(mogenPath, extra_mogen_args, extra_llvm_args) - puts "*** Testing Objective-C" + status = '*** Testing Objective-C' + status << ' with parameters' if extra_mogen_args.length > 0 + puts status ENV['MOMC_NO_INVERSE_RELATIONSHIP_WARNINGS'] = '1' run_or_die "#{mogenPath.gsub(/ /, '\\ ')} --model test.xcdatamodel --output MOs --baseClass MyBaseClass #{extra_mogen_args}" run_or_die 'cp ./objc/Gender.* ./objc/MyBaseClass.* MOs' @@ -60,6 +62,16 @@ task :swift do Rake::Task[:clean].execute end +desc 'Generate, Compile and Run with parameters' +task :objc do + Rake::Task[:clean].execute + gen_and_compile_objc(MOGENERATOR_PATH, '--ignored-entities Uncle,Aunt', '') + includes_uncle = Dir.entries('./MOs').any? do |file| + file.include?('Uncle') || file.include?('Aunt') + end + raise 'Failed: Ignored entities should not be generated' if includes_uncle + Rake::Task[:clean].execute +end desc 'Clean output' task :clean do diff --git a/test/test.xcdatamodel/contents b/test/test.xcdatamodel/contents index 54105d88..a62d35c2 100644 --- a/test/test.xcdatamodel/contents +++ b/test/test.xcdatamodel/contents @@ -55,6 +55,9 @@ + + + @@ -69,5 +72,6 @@ + \ No newline at end of file From a71b0835e5db93dd6be8b857541980f2f826a4ae Mon Sep 17 00:00:00 2001 From: Martin Kim Dung-Pham Date: Wed, 26 Sep 2018 08:47:31 +0200 Subject: [PATCH 3/6] Move ignored flag into entities as `mogenerator.ignore` user info key --- mogenerator.h | 2 +- mogenerator.m | 18 ++++++++++-------- test/Rakefile | 12 ++++-------- test/test.xcdatamodel/contents | 3 +++ 4 files changed, 18 insertions(+), 17 deletions(-) diff --git a/mogenerator.h b/mogenerator.h index 5aed9c97..003ebb4a 100644 --- a/mogenerator.h +++ b/mogenerator.h @@ -27,6 +27,7 @@ - (NSString*)additionalHeaderFileName; - (void)_processPredicate:(NSPredicate*)predicate_ bindings:(NSMutableArray*)bindings_; - (NSArray*)prettyFetchRequests; +- (BOOL)isIgnored; @end @interface NSAttributeDescription (typing) @@ -67,7 +68,6 @@ NSString *baseClass; NSString *baseClassImport; NSString *baseClassForce; - NSString *ignoredEntities; NSString *includem; NSString *includeh; NSString *templatePath; diff --git a/mogenerator.m b/mogenerator.m index 0139e1d4..61ad5d08 100644 --- a/mogenerator.m +++ b/mogenerator.m @@ -19,6 +19,7 @@ static const NSString *const kAdditionalImportsKey = @"additionalImports"; static const NSString *const kCustomBaseClass = @"mogenerator.customBaseClass"; static const NSString *const kReadOnly = @"mogenerator.readonly"; +static const NSString *const kIgnored = @"mogenerator.ignore"; @interface NSEntityDescription (fetchedPropertiesAdditions) - (NSDictionary*)fetchedPropertiesByName; @@ -418,6 +419,14 @@ - (NSArray*)prettyFetchRequests { } return result; } + +- (BOOL)isIgnored { + NSString *readonlyUserinfoValue = [[self userInfo] objectForKey:kIgnored]; + if (readonlyUserinfoValue != nil) { + return YES; + } + return NO; +} @end @implementation NSAttributeDescription (typing) @@ -796,7 +805,6 @@ - (void)application:(DDCliApplication*)app {@"base-class", 0, DDGetoptRequiredArgument}, {@"base-class-import", 0, DDGetoptRequiredArgument}, {@"base-class-force", 0, DDGetoptRequiredArgument}, - {@"ignored-entities", 0, DDGetoptRequiredArgument}, // For compatibility: {@"baseClass", 0, DDGetoptRequiredArgument}, {@"includem", 0, DDGetoptRequiredArgument}, @@ -851,7 +859,6 @@ - (void)printUsage { "--base-class-force CLASS Same as --base-class except will force all entities to\n" " have the specified base class. Even if a super entity\n" " exists\n" - "--ignored-entities LIST Comma separated list of of entity names. Entities will be ignored when generating classes\n" "--includem FILE Generate aggregate include file for .m files for both\n" " human and machine generated source files\n" "--includeh FILE Generate aggregate include file for .h files for human\n" @@ -1048,11 +1055,6 @@ - (void)validateOutputPath:(NSString*)path forType:(NSString*)type } } -- (BOOL)isIgnoredEntity:(NSString *)entityName { - NSArray *entities = [ignoredEntities componentsSeparatedByString:@","]; - return [entities containsObject:entityName] || [entities containsObject:[NSString stringWithFormat:@"_%@", entityName]]; -} - - (int)application:(DDCliApplication*)app runWithArguments:(NSArray*)arguments { if (_help) { [self printUsage]; @@ -1199,7 +1201,7 @@ - (int)application:(DDCliApplication*)app runWithArguments:(NSArray*)arguments { NSArray *entitiesWithCustomSubclass = [model entitiesWithACustomSubclassInConfiguration:configuration verbose:YES]; for (NSEntityDescription *entity in entitiesWithCustomSubclass) { - if ([self isIgnoredEntity:entity.name]) { + if ([entity isIgnored]) { continue; } NSString *generatedMachineH = [machineH executeWithObject:entity sender:nil]; diff --git a/test/Rakefile b/test/Rakefile index 60625cff..6c1b1717 100644 --- a/test/Rakefile +++ b/test/Rakefile @@ -26,9 +26,7 @@ run_or_die 'xcodebuild -project ../mogenerator.xcodeproj -scheme mogenerator cle run_or_die 'xcodebuild -project ../mogenerator.xcodeproj -scheme mogenerator' def gen_and_compile_objc(mogenPath, extra_mogen_args, extra_llvm_args) - status = '*** Testing Objective-C' - status << ' with parameters' if extra_mogen_args.length > 0 - puts status + puts = '*** Testing Objective-C' ENV['MOMC_NO_INVERSE_RELATIONSHIP_WARNINGS'] = '1' run_or_die "#{mogenPath.gsub(/ /, '\\ ')} --model test.xcdatamodel --output MOs --baseClass MyBaseClass #{extra_mogen_args}" run_or_die 'cp ./objc/Gender.* ./objc/MyBaseClass.* MOs' @@ -62,13 +60,11 @@ task :swift do Rake::Task[:clean].execute end -desc 'Generate, Compile and Run with parameters' +desc 'Generate, Compile and test ignored entities' task :objc do Rake::Task[:clean].execute - gen_and_compile_objc(MOGENERATOR_PATH, '--ignored-entities Uncle,Aunt', '') - includes_uncle = Dir.entries('./MOs').any? do |file| - file.include?('Uncle') || file.include?('Aunt') - end + gen_and_compile_objc(MOGENERATOR_PATH, '', '') + includes_uncle = Dir.entries('./MOs').any? { |file| file.include?('Uncle') } raise 'Failed: Ignored entities should not be generated' if includes_uncle Rake::Task[:clean].execute end diff --git a/test/test.xcdatamodel/contents b/test/test.xcdatamodel/contents index a62d35c2..73ba0625 100644 --- a/test/test.xcdatamodel/contents +++ b/test/test.xcdatamodel/contents @@ -57,6 +57,9 @@ + + + From 7784c2e0f82f27e942c80840bde11fe6a6bd2f40 Mon Sep 17 00:00:00 2001 From: Martin Kim Dung-Pham Date: Wed, 26 Sep 2018 09:23:31 +0200 Subject: [PATCH 4/6] Remove unnecessary change --- test/Rakefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Rakefile b/test/Rakefile index 6c1b1717..e9d21922 100644 --- a/test/Rakefile +++ b/test/Rakefile @@ -26,7 +26,7 @@ run_or_die 'xcodebuild -project ../mogenerator.xcodeproj -scheme mogenerator cle run_or_die 'xcodebuild -project ../mogenerator.xcodeproj -scheme mogenerator' def gen_and_compile_objc(mogenPath, extra_mogen_args, extra_llvm_args) - puts = '*** Testing Objective-C' + puts "*** Testing Objective-C" ENV['MOMC_NO_INVERSE_RELATIONSHIP_WARNINGS'] = '1' run_or_die "#{mogenPath.gsub(/ /, '\\ ')} --model test.xcdatamodel --output MOs --baseClass MyBaseClass #{extra_mogen_args}" run_or_die 'cp ./objc/Gender.* ./objc/MyBaseClass.* MOs' From 38e62d1bf902c5a860e2fb22c906fe04dd0587f9 Mon Sep 17 00:00:00 2001 From: Martin Kim Dung-Pham Date: Wed, 26 Sep 2018 09:42:07 +0200 Subject: [PATCH 5/6] Move `isIgnored` into its own category --- mogenerator.h | 6 +++++- mogenerator.m | 16 +++++++++------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/mogenerator.h b/mogenerator.h index 003ebb4a..20940152 100644 --- a/mogenerator.h +++ b/mogenerator.h @@ -17,6 +17,11 @@ - (NSArray*)entitiesWithACustomSubclassInConfiguration:(NSString*)configuration_ verbose:(BOOL)verbose_; @end +@interface NSEntityDescription (userInfo) +/// @return Whether or not the entity should be ignored during code generation +- (BOOL)isIgnored; +@end + @interface NSEntityDescription (customBaseClass) - (BOOL)hasCustomClass; - (BOOL)hasSuperentity; @@ -27,7 +32,6 @@ - (NSString*)additionalHeaderFileName; - (void)_processPredicate:(NSPredicate*)predicate_ bindings:(NSMutableArray*)bindings_; - (NSArray*)prettyFetchRequests; -- (BOOL)isIgnored; @end @interface NSAttributeDescription (typing) diff --git a/mogenerator.m b/mogenerator.m index 61ad5d08..64af375a 100644 --- a/mogenerator.m +++ b/mogenerator.m @@ -136,6 +136,15 @@ - (NSArray*)entitiesWithACustomSubclassInConfiguration:(NSString*)configuration_ } @end +@implementation NSEntityDescription (userInfo) +- (BOOL)isIgnored { + NSString *readonlyUserinfoValue = [[self userInfo] objectForKey:kIgnored]; + if (readonlyUserinfoValue != nil) { + return YES; + } + return NO; +} +@end @implementation NSEntityDescription (customBaseClass) - (BOOL)hasCustomBaseCaseImport { @@ -420,13 +429,6 @@ - (NSArray*)prettyFetchRequests { return result; } -- (BOOL)isIgnored { - NSString *readonlyUserinfoValue = [[self userInfo] objectForKey:kIgnored]; - if (readonlyUserinfoValue != nil) { - return YES; - } - return NO; -} @end @implementation NSAttributeDescription (typing) From c816e7d4c4f13145fff178be6286aff2d8b28846 Mon Sep 17 00:00:00 2001 From: Martin Kim Dung-Pham Date: Fri, 28 Sep 2018 08:59:06 +0200 Subject: [PATCH 6/6] Update tests and cleanup pull request --- mogenerator.m | 1 - test/Rakefile | 15 +++++++-------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/mogenerator.m b/mogenerator.m index 64af375a..a83b3de1 100644 --- a/mogenerator.m +++ b/mogenerator.m @@ -428,7 +428,6 @@ - (NSArray*)prettyFetchRequests { } return result; } - @end @implementation NSAttributeDescription (typing) diff --git a/test/Rakefile b/test/Rakefile index e9d21922..5898b5f5 100644 --- a/test/Rakefile +++ b/test/Rakefile @@ -45,11 +45,17 @@ def gen_and_compile_swift(mogenPath, extra_mogen_args) puts run_or_die './testbin' end +def assert_entity_user_info_respected + # mogenerator.ignore + includes_uncle = Dir.entries('./MOs').any? { |file| file.include?('Uncle') } + raise 'Failed: Ignored entities should not be generated' if includes_uncle +end desc 'Generate, Compile and Run Objective-C' task :objc do Rake::Task[:clean].execute gen_and_compile_objc(MOGENERATOR_PATH, '', '') + assert_entity_user_info_respected Rake::Task[:clean].execute end @@ -57,17 +63,10 @@ desc 'Generate, Compile and Run Swift' task :swift do Rake::Task[:clean].execute gen_and_compile_swift(MOGENERATOR_PATH, '') + assert_entity_user_info_respected Rake::Task[:clean].execute end -desc 'Generate, Compile and test ignored entities' -task :objc do - Rake::Task[:clean].execute - gen_and_compile_objc(MOGENERATOR_PATH, '', '') - includes_uncle = Dir.entries('./MOs').any? { |file| file.include?('Uncle') } - raise 'Failed: Ignored entities should not be generated' if includes_uncle - Rake::Task[:clean].execute -end desc 'Clean output' task :clean do