From 91389dd22bde8b9bbade8b191e19c817abc3158a Mon Sep 17 00:00:00 2001 From: Beniamin Sarkisyan Date: Wed, 20 Jul 2016 17:49:30 +0300 Subject: [PATCH] [RESOLVED] Added the possibility to add empty group to Xcode structure #118 --- lib/generamba/helpers/xcodeproj_helper.rb | 20 +++++++++--- lib/generamba/module_generator.rb | 39 +++++++++++++---------- 2 files changed, 38 insertions(+), 21 deletions(-) diff --git a/lib/generamba/helpers/xcodeproj_helper.rb b/lib/generamba/helpers/xcodeproj_helper.rb index 9c3d0c8..c7fc1d8 100644 --- a/lib/generamba/helpers/xcodeproj_helper.rb +++ b/lib/generamba/helpers/xcodeproj_helper.rb @@ -20,7 +20,7 @@ def self.obtain_project(project_name) # # @return [void] def self.add_file_to_project_and_targets(project, targets_name, group_path, file_path, file_type = nil) - module_group = self.retreive_group_or_create_if_needed(group_path, project, true) + module_group = self.retrieve_group_or_create_if_needed(group_path, project, true) xcode_file = module_group.new_file(File.absolute_path(file_path)) file_name = File.basename(file_path) @@ -40,6 +40,15 @@ def self.add_file_to_project_and_targets(project, targets_name, group_path, file end end end + + # Adds a provided directory to a specific Project + # @param project [Xcodeproj::Project] The target xcodeproj file + # @param group_path [Pathname] The Xcode group path for current directory + # + # @return [void] + def self.add_group_to_project(project, group_path) + self.retrieve_group_or_create_if_needed(group_path, project, true) + end # Adds xcode file to target based on it's type # @param target [Xcodeproj::AbstractTarget] xcode target to use @@ -79,7 +88,7 @@ def self.is_bundle_resource?(resource_name) # # @return [Void] def self.clear_group(project, targets_name, group_path) - module_group = self.retreive_group_or_create_if_needed(group_path, project, false) + module_group = self.retrieve_group_or_create_if_needed(group_path, project, false) return unless module_group files_path = self.files_path_from_group(module_group, project) @@ -98,7 +107,7 @@ def self.clear_group(project, targets_name, group_path) # # @return [TrueClass or FalseClass] def self.module_with_group_path_already_exists(project, group_path) - module_group = self.retreive_group_or_create_if_needed(group_path, project, false) + module_group = self.retrieve_group_or_create_if_needed(group_path, project, false) return module_group == nil ? false : true end @@ -110,18 +119,19 @@ def self.module_with_group_path_already_exists(project, group_path) # @param create_group_if_not_exists [TrueClass or FalseClass] If true notexistent group will be created # # @return [PBXGroup] - def self.retreive_group_or_create_if_needed(group_path, project, create_group_if_not_exists) + def self.retrieve_group_or_create_if_needed(group_path, project, create_group_if_not_exists) group_names = path_names_from_path(group_path) final_group = project group_names.each do |group_name| next_group = final_group[group_name] + unless next_group unless create_group_if_not_exists return nil end - + new_group_path = group_name next_group = final_group.new_group(group_name, new_group_path) end diff --git a/lib/generamba/module_generator.rb b/lib/generamba/module_generator.rb index 28e6b51..ceb5aeb 100755 --- a/lib/generamba/module_generator.rb +++ b/lib/generamba/module_generator.rb @@ -67,23 +67,30 @@ def process_files_if_needed(files, name, code_module, template, project, targets XcodeprojHelper.clear_group(project, targets, group_path) files.each do |file| - - file_group = File.dirname(file[TEMPLATE_NAME_KEY]) - - # Generating the content of the code file and it's name - file_name, file_content = ContentGenerator.create_file(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) - File.open(file_path, 'w+') do |f| - f.write(file_content) - end - - file_type = file[TEMPLATE_FILE_FILETYPE_KEY] + unless file[TEMPLATE_FILE_PATH_KEY] + directory_name = file[TEMPLATE_NAME_KEY].gsub(/^\/|\/$/, '') + file_group = dir_path.join(directory_name) + + FileUtils.mkdir_p file_group + XcodeprojHelper.add_group_to_project(project, file_group) + else + file_group = File.dirname(file[TEMPLATE_NAME_KEY]) + + # Generating the content of the code file and it's name + file_name, file_content = ContentGenerator.create_file(file, code_module, template) + file_path = dir_path.join(file_group).join(file_name) - # Creating the file in the Xcode project - XcodeprojHelper.add_file_to_project_and_targets(project, targets, group_path.join(file_group), file_path, file_type) + # Creating the file in the filesystem + FileUtils.mkdir_p File.dirname(file_path) + File.open(file_path, 'w+') do |f| + f.write(file_content) + end + + file_type = file[TEMPLATE_FILE_FILETYPE_KEY] + + # Creating the file in the Xcode project + XcodeprojHelper.add_file_to_project_and_targets(project, targets, group_path.join(file_group), file_path, file_type) + end end end end