Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A little refactoring #137

Merged
merged 2 commits into from
Aug 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/generamba/cli/gen_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
3 changes: 2 additions & 1 deletion lib/generamba/code_generation/code_module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,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]
Expand Down Expand Up @@ -106,5 +106,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
2 changes: 1 addition & 1 deletion lib/generamba/code_generation/content_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/generamba/constants/rambaspec_constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
36 changes: 7 additions & 29 deletions lib/generamba/helpers/xcodeproj_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,21 @@ 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)
module_group = retrieve_group_or_create_if_needed(group_path, project, true)
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))

file_name = File.basename(file_path)
targets_name.each do |target|
xcode_target = obtain_target(target, project)

unless file_type
if is_compile_source?(file_name)
file_type = 'source'
elsif is_bundle_resource?(file_name)
file_type = 'resource'
end
end

unless file_type.nil?
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
Expand All @@ -48,22 +42,6 @@ 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
#
Expand Down
43 changes: 21 additions & 22 deletions lib/generamba/module_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down