From d32c935dad19be1b95588ea359dd2d8b5fd1bf8a Mon Sep 17 00:00:00 2001 From: Beniamin Sarkisyan Date: Wed, 6 Jan 2016 22:33:47 +0300 Subject: [PATCH 1/2] Fixed #64: Errors if default path is empty --- .../generamba/helpers/rambafile_validator.rb | 84 ++++++++++++++----- generamba/lib/generamba/module_generator.rb | 64 +++++++------- 2 files changed, 96 insertions(+), 52 deletions(-) diff --git a/generamba/lib/generamba/helpers/rambafile_validator.rb b/generamba/lib/generamba/helpers/rambafile_validator.rb index f82115d..bcce677 100644 --- a/generamba/lib/generamba/helpers/rambafile_validator.rb +++ b/generamba/lib/generamba/helpers/rambafile_validator.rb @@ -11,37 +11,79 @@ def validate(path) mandatory_fields = [COMPANY_KEY, PROJECT_NAME_KEY, - XCODEPROJ_PATH_KEY, - PROJECT_FILE_PATH_KEY, - PROJECT_GROUP_PATH_KEY, - TEMPLATES_KEY] - - interchangable_fields = [[PROJECT_TARGETS_KEY, PROJECT_TARGET_KEY]] + XCODEPROJ_PATH_KEY] mandatory_fields.each do |field| - if preferences.has_key?(field) == false - error_description = "Rambafile is broken! Cannot find #{field} field, which is mandatory. Either add it manually, or run *generamba setup*.".red - raise StandardError.new(error_description) + unless preferences[field] + puts "Rambafile is broken! *#{field}* field cannot be empty, because it is mandatory. Either add it manually, or run *generamba setup*.".red + exit end end - interchangable_fields.each do |fields_array| - has_value = false - fields_array.each do |field| - has_value = preferences.has_key?(field) || has_value - end + project_failure_fields = all_project_failure_fields(preferences) + test_failure_fields = all_test_failure_fields(preferences) + failure_fields = project_failure_fields + test_failure_fields - if has_value == false - error_description = "Rambafile is broken! Cannot find any of #{fields_array} fields, one of them is mandatory. Either add it manually, or run *generamba setup*.".red - raise StandardError.new(error_description) - end + if failure_fields.count > 0 + puts "Rambafile is broken! Cannot find any of #{failure_fields} fields, one of them is mandatory. Either add it manually, or run *generamba setup*.".red + exit end - if preferences[TEMPLATES_KEY] == nil - error_description = "You can't run *generamba gen* without any templates installed. Add their declarations to a Rambafile and run *generamba template install*.".red - raise StandardError.new(error_description) + unless preferences[TEMPLATES_KEY] + puts "You can't run *generamba gen* without any templates installed. Add their declarations to a Rambafile and run *generamba template install*.".red + exit + end + end + + private + + def all_project_failure_fields(preferences) + return all_nil_mandatory_fields_for_target_type("project", preferences) + end + + def all_test_failure_fields(preferences) + target = preferences[TEST_TARGET_KEY] + targets = preferences[TEST_TARGETS_KEY] + file_path = preferences[TEST_FILE_PATH_KEY] + group_path = preferences[TEST_GROUP_PATH_KEY] + + has_test_fields = target || targets || file_path || group_path + + unless has_test_fields + return [] + end + + return all_nil_mandatory_fields_for_target_type("test", preferences) + end + + def all_nil_mandatory_fields_for_target_type(target_type, preferences) + target_type = target_type.upcase + + target_const_value = Generamba.const_get(target_type + "_TARGET_KEY") + targets_const_value = Generamba.const_get(target_type + "_TARGETS_KEY") + + target = preferences[target_const_value] + targets = preferences[targets_const_value] + + fields = [] + + if !target && (!targets || (targets && targets.count == 0)) + fields.push(target_const_value) + end + + file_path_const_value = Generamba.const_get(target_type + "_FILE_PATH_KEY") + + unless preferences[file_path_const_value] + fields.push(file_path_const_value) + end + + group_path_const_value = Generamba.const_get(target_type + "_GROUP_PATH_KEY") + + unless preferences[group_path_const_value] + fields.push(group_path_const_value) end + return fields end end diff --git a/generamba/lib/generamba/module_generator.rb b/generamba/lib/generamba/module_generator.rb index 68b5013..4d512b2 100644 --- a/generamba/lib/generamba/module_generator.rb +++ b/generamba/lib/generamba/module_generator.rb @@ -21,33 +21,41 @@ def generate_module(name, code_module, template) # Creating code files puts('Creating code files...') process_files_if_needed(template.code_files, - name, - code_module, - template, - project, - code_module.project_targets, - code_module.module_group_path, - code_module.module_file_path) + name, + code_module, + template, + project, + code_module.project_targets, + code_module.module_group_path, + code_module.module_file_path) # Creating test files - puts('Creating test files...') - process_files_if_needed(template.test_files, - name, - code_module, - template, - project, - code_module.test_targets, - code_module.test_group_path, - code_module.test_file_path) + included_tests = code_module.test_targets && code_module.test_group_path && code_module.test_file_path + + if included_tests + puts('Creating test files...') + process_files_if_needed(template.test_files, + name, + code_module, + template, + project, + code_module.test_targets, + code_module.test_group_path, + code_module.test_file_path) + end # Saving the current changes in the Xcode project project.save + + test_file_path_created_message = !code_module.test_file_path ? "" : "Test file path: #{code_module.test_file_path}".green + "\n" + test_group_path_created_message = !code_module.test_group_path ? "" : "Test group path: #{code_module.test_group_path}".green + puts("Module successfully created!\n" + - "Name: #{name}".green + "\n" + - "Module file path: #{code_module.module_file_path}".green + "\n" + - "Module group path: #{code_module.module_group_path}".green + "\n" + - "Test file path: #{code_module.test_file_path}".green + "\n" + - "Test group path: #{code_module.test_group_path}".green) + "Name: #{name}".green + "\n" + + "Module file path: #{code_module.module_file_path}".green + "\n" + + "Module group path: #{code_module.module_group_path}".green + "\n" + + test_file_path_created_message + + test_group_path_created_message) end def process_files_if_needed(files, name, code_module, template, project, targets, group_path, dir_path) @@ -68,12 +76,9 @@ def process_files_if_needed(files, name, code_module, template, project, targets file_group = File.dirname(file[TEMPLATE_NAME_KEY]) # Generating the content of the code file - file_content = ContentGenerator.create_file_content(file, - code_module, - template) - file_path = dir_path - .join(file_group) - .join(file_name) + file_content = ContentGenerator.create_file_content(file, code_module, template) + file_path = dir_path.join(file_group) + .join(file_name) # Creating the file in the filesystem FileUtils.mkdir_p File.dirname(file_path) @@ -82,10 +87,7 @@ def process_files_if_needed(files, name, code_module, template, project, targets end # Creating the file in the Xcode project - XcodeprojHelper.add_file_to_project_and_targets(project, - targets, - group_path.join(file_group), - file_path) + XcodeprojHelper.add_file_to_project_and_targets(project, targets, group_path.join(file_group), file_path) end end end From 16115f688b8f6bd8bc0e6344f0ca21ecfd3b0916 Mon Sep 17 00:00:00 2001 From: Beniamin Sarkisyan Date: Wed, 6 Jan 2016 22:52:01 +0300 Subject: [PATCH 2/2] Commented new methods --- .../lib/generamba/helpers/rambafile_validator.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/generamba/lib/generamba/helpers/rambafile_validator.rb b/generamba/lib/generamba/helpers/rambafile_validator.rb index bcce677..b1ea69b 100644 --- a/generamba/lib/generamba/helpers/rambafile_validator.rb +++ b/generamba/lib/generamba/helpers/rambafile_validator.rb @@ -5,6 +5,8 @@ class RambafileValidator # Method validates Rambafile contents # @param path [String] The path to a Rambafile + # + # @return [Void] def validate(path) file_contents = open(path).read preferences = file_contents.empty? ? {} : YAML.load(file_contents).to_hash @@ -37,10 +39,18 @@ def validate(path) private + # Method which return all project failure fields + # @param preferences [Hash] Converted Rambafile + # + # @return [Array] def all_project_failure_fields(preferences) return all_nil_mandatory_fields_for_target_type("project", preferences) end + # Method which return all test failure fields + # @param preferences [Hash] Converted Rambafile + # + # @return [Array] def all_test_failure_fields(preferences) target = preferences[TEST_TARGET_KEY] targets = preferences[TEST_TARGETS_KEY] @@ -56,6 +66,11 @@ def all_test_failure_fields(preferences) return all_nil_mandatory_fields_for_target_type("test", preferences) end + # Method which return all failure fields for target_type + # @param target_type [String] "project" or "test" + # @param preferences [Hash] Converted Rambafile + # + # @return [Array] def all_nil_mandatory_fields_for_target_type(target_type, preferences) target_type = target_type.upcase