From 98f28ccf9cbe39e2fee05b39d12a0843d350a69a Mon Sep 17 00:00:00 2001 From: Beniamin Sarkisyan Date: Thu, 25 Aug 2016 00:06:52 +0300 Subject: [PATCH] 1. Replaced gen_command option `module_targets` to `project_targets`. 2. Replaced rambaspec parameter `file_name` to `custom_name`. 3. Replaced rambaspec parameter `file_type` to `is_resource`. --- lib/generamba/cli/gen_command.rb | 2 +- lib/generamba/code_generation/code_module.rb | 3 +- .../code_generation/content_generator.rb | 2 +- .../constants/rambaspec_constants.rb | 4 +- lib/generamba/helpers/xcodeproj_helper.rb | 34 +++------------ lib/generamba/module_generator.rb | 43 +++++++++---------- 6 files changed, 33 insertions(+), 55 deletions(-) diff --git a/lib/generamba/cli/gen_command.rb b/lib/generamba/cli/gen_command.rb index 8251102..882409a 100644 --- a/lib/generamba/cli/gen_command.rb +++ b/lib/generamba/cli/gen_command.rb @@ -13,7 +13,7 @@ class Application < Thor desc 'gen [MODULE_NAME] [TEMPLATE_NAME]', 'Creates a new VIPER module with a given name from a specific template' method_option :description, :aliases => '-d', :desc => 'Provides a full description to the module' method_option :author, :desc => 'Specifies the author name for generated module' - method_option :module_targets, :desc => 'Specifies project targets for adding new module files' + method_option :project_targets, :desc => 'Specifies project targets for adding new module files' method_option :module_file_path, :desc => 'Specifies a location in the filesystem for new files' method_option :module_group_path, :desc => 'Specifies a location in Xcode groups for new files' method_option :module_path, :desc => 'Specifies a location (both in the filesystem and Xcode) for new files' diff --git a/lib/generamba/code_generation/code_module.rb b/lib/generamba/code_generation/code_module.rb index c343fb1..1c1ce90 100644 --- a/lib/generamba/code_generation/code_module.rb +++ b/lib/generamba/code_generation/code_module.rb @@ -57,7 +57,7 @@ def initialize(name, description, rambafile, options) # Options adaptation @author = options[:author] if options[:author] - @project_targets = options[:module_targets].split(',') if options[:module_targets] + @project_targets = options[:project_targets].split(',') if options[:project_targets] @test_targets = options[:test_targets].split(',') if options[:test_targets] if options[:module_file_path] @@ -96,5 +96,6 @@ def initialize(name, description, rambafile, options) @podfile_path = rambafile[PODFILE_PATH_KEY] if rambafile[PODFILE_PATH_KEY] != nil @cartfile_path = rambafile[CARTFILE_PATH_KEY] if rambafile[CARTFILE_PATH_KEY] != nil end + end end \ No newline at end of file diff --git a/lib/generamba/code_generation/content_generator.rb b/lib/generamba/code_generation/content_generator.rb index e74aa14..d829b2b 100755 --- a/lib/generamba/code_generation/content_generator.rb +++ b/lib/generamba/code_generation/content_generator.rb @@ -56,7 +56,7 @@ def self.create_file(file, code_module, template) def self.file_name_template(file) template_default_text = "{{ prefix }}{{ module_info.name }}{{ module_info.file_basename }}" - template_text = file[TEMPLATE_FILE_FILENAME_KEY] || template_default_text + template_text = file[TEMPLATE_FILE_CUSTOM_NAME_KEY] || template_default_text return Liquid::Template.parse(template_text) end end diff --git a/lib/generamba/constants/rambaspec_constants.rb b/lib/generamba/constants/rambaspec_constants.rb index f007f14..08c435e 100755 --- a/lib/generamba/constants/rambaspec_constants.rb +++ b/lib/generamba/constants/rambaspec_constants.rb @@ -10,9 +10,9 @@ module Generamba TEMPLATE_CODE_FILES_KEY = 'code_files' TEMPLATE_TEST_FILES_KEY = 'test_files' TEMPLATE_FILE_NAME_KEY = 'name' + TEMPLATE_FILE_CUSTOM_NAME_KEY = 'custom_name' TEMPLATE_FILE_PATH_KEY = 'path' - TEMPLATE_FILE_FILENAME_KEY = 'file_name' - TEMPLATE_FILE_FILETYPE_KEY = 'file_type' + TEMPLATE_FILE_IS_RESOURCE_KEY = 'is_resource' TEMPLATE_DEPENDENCIES_KEY = 'dependencies' end \ No newline at end of file diff --git a/lib/generamba/helpers/xcodeproj_helper.rb b/lib/generamba/helpers/xcodeproj_helper.rb index c7fc1d8..e294d3f 100644 --- a/lib/generamba/helpers/xcodeproj_helper.rb +++ b/lib/generamba/helpers/xcodeproj_helper.rb @@ -16,10 +16,10 @@ def self.obtain_project(project_name) # @param targets_name [String] Array of targets name # @param group_path [Pathname] The Xcode group path for current file # @param file_path [Pathname] The file path for current file - # @param file_type is either 'source' or 'resource' - it affects on where file will be added. Put nil for autodetect + # @param file_is_resource [TrueClass or FalseClass] If true then file is resource # # @return [void] - def self.add_file_to_project_and_targets(project, targets_name, group_path, file_path, file_type = nil) + def self.add_file_to_project_and_targets(project, targets_name, group_path, file_path, file_is_resource = false) module_group = self.retrieve_group_or_create_if_needed(group_path, project, true) xcode_file = module_group.new_file(File.absolute_path(file_path)) @@ -27,16 +27,10 @@ def self.add_file_to_project_and_targets(project, targets_name, group_path, file targets_name.each do |target| xcode_target = self.obtain_target(target, project) - unless file_type - if self.is_compile_source?(file_name) - file_type = 'source' - elsif self.is_bundle_resource?(file_name) - file_type = 'resource' - end - end - - if file_type != nil - self.add_file_to_target(xcode_target, xcode_file, file_type) + if file_is_resource || self.is_bundle_resource?(file_name) + xcode_target.add_resources([xcode_file]) + elsif self.is_compile_source?(file_name) + xcode_target.add_file_references([xcode_file]) end end end @@ -49,22 +43,6 @@ def self.add_file_to_project_and_targets(project, targets_name, group_path, file 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 - # @param file [Xcodeproj::PBXFileReference] file reference to add - # @param type [String] is either 'source' or 'resource' - # - def self.add_file_to_target(target, file, type) - case type - when 'source' - target.add_file_references([file]) - when 'resource' - target.add_resources([file]) - else - puts "Can't add file with type #{type}. Only 'source' and 'resource' are acceptable" - end - end # File is a compiled source # @param file_name [String] String of file name diff --git a/lib/generamba/module_generator.rb b/lib/generamba/module_generator.rb index ceb5aeb..ea6cbeb 100755 --- a/lib/generamba/module_generator.rb +++ b/lib/generamba/module_generator.rb @@ -47,15 +47,12 @@ def generate_module(name, code_module, template) # 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_created_message + - test_group_path_created_message) + puts 'Module successfully created!' + puts "Name: #{name}".green + puts "Module file path: #{code_module.module_file_path}".green + puts "Module group path: #{code_module.module_group_path}".green + puts !code_module.test_file_path ? '' : "Test file path: #{code_module.test_file_path}".green + puts !code_module.test_group_path ? '' : "Test group path: #{code_module.test_group_path}".green end def process_files_if_needed(files, name, code_module, template, project, targets, group_path, dir_path) @@ -73,24 +70,26 @@ def process_files_if_needed(files, name, code_module, template, project, targets 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) + next + end - # 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_group = File.dirname(file[TEMPLATE_NAME_KEY]) - file_type = file[TEMPLATE_FILE_FILETYPE_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_is_resource = file[TEMPLATE_FILE_IS_RESOURCE_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_is_resource) end end end