Skip to content

Commit

Permalink
Merge branch 'develop' into feature/task-142
Browse files Browse the repository at this point in the history
# Conflicts:
#	lib/generamba/cli/gen_command.rb
  • Loading branch information
Beniamin Sarkisyan committed Sep 3, 2016
2 parents d73cb41 + 2779bc1 commit 6541fc3
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 51 deletions.
6 changes: 4 additions & 2 deletions lib/generamba/cli/gen_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require 'generamba/helpers/xcodeproj_helper.rb'
require 'generamba/helpers/dependency_checker.rb'
require 'generamba/helpers/gen_command_table_parameters_formatter.rb'
require 'generamba/helpers/module_info_generator.rb'

module Generamba::CLI
class Application < Thor
Expand Down Expand Up @@ -42,8 +43,9 @@ def gen(module_name, template_name)

rambafile = YAML.load_file(RAMBAFILE_NAME)

template = ModuleTemplate.new(template_name)
code_module = CodeModule.new(module_name, module_description, rambafile, options)
module_info = ModuleInfoGenerator.new(code_module)
template = ModuleTemplate.new(template_name, module_info.scope)

parameters = GenCommandTableParametersFormatter.prepare_parameters_for_displaying(code_module, template_name)
PrintTable.print_values(
Expand All @@ -65,7 +67,7 @@ def gen(module_name, template_name)
end
end

generator = Generamba::ModuleGenerator.new()
generator = Generamba::ModuleGenerator.new
generator.generate_module(module_name, code_module, template)
end

Expand Down
33 changes: 6 additions & 27 deletions lib/generamba/code_generation/content_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ module Generamba
class ContentGenerator

# Generates and returns a filename and a body of a specific code file.
# @param file []Hash<String,String>] A hashmap with template's filename and filepath
# @param code_module [CodeModule] The model describing a generating module
# @param file [Hash<String,String>] A hashmap with template's filename and filepath
# @param scope [Hash<String,String>] A hashmap with module info
# @param template [ModuleTemplate] The model describing a Generamba template used for code generation
#
# @return [String], [String] The generated file_name and body
def self.create_file(file, code_module, template)
def self.create_file(file, scope, template)
file_source = IO.read(template.template_path.join(file[TEMPLATE_FILE_PATH_KEY]))
Liquid::Template.file_system = Liquid::LocalFileSystem.new(template.template_path.join('snippets'), '%s.liquid')

Expand All @@ -20,29 +20,8 @@ def self.create_file(file, code_module, template)

file_basename = File.basename(file[TEMPLATE_FILE_NAME_KEY])

module_info = {
'name' => code_module.name,
'description' => code_module.description,
'project_name' => code_module.project_name,
'product_module_name' => code_module.product_module_name,
'project_targets' => code_module.project_targets,
'test_targets' => code_module.test_targets
}

developer = {
'name' => code_module.author,
'company' => code_module.company
}

scope = {
'year' => code_module.year,
'date' => Time.now.strftime('%d/%m/%Y'),
'developer' => developer,
'module_info' => module_info,
'prefix' => code_module.prefix,
'custom_parameters' => code_module.custom_parameters
}

module_info = scope['module_info']

module_info['file_basename'] = file_basename

file_name = filename_template.render(scope)
Expand All @@ -56,7 +35,7 @@ def self.create_file(file, code_module, template)
end

def self.file_name_template(file)
template_default_text = "{{ prefix }}{{ module_info.name }}{{ module_info.file_basename }}"
template_default_text = '{{ prefix }}{{ module_info.name }}{{ module_info.file_basename }}'
template_text = file[TEMPLATE_FILE_CUSTOM_NAME_KEY] || template_default_text
return Liquid::Template.parse(template_text)
end
Expand Down
14 changes: 11 additions & 3 deletions lib/generamba/code_generation/module_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@ module Generamba
class ModuleTemplate
attr_reader :template_name, :template_path, :code_files, :test_files, :dependencies

def initialize(name)
def initialize(name, options = nil)
spec_path = TemplateHelper.obtain_spec(name)
spec = YAML.load_file(spec_path)


unless options
spec = YAML.load_file(spec_path)
else
spec_source = IO.read(spec_path)
spec_template = Liquid::Template.parse(spec_source)
spec_content = spec_template.render(options)
spec = YAML.load(spec_content)
end

@code_files = spec[TEMPLATE_CODE_FILES_KEY]
@test_files = spec[TEMPLATE_TEST_FILES_KEY]
@template_name = spec[TEMPLATE_NAME_KEY]
Expand Down
32 changes: 32 additions & 0 deletions lib/generamba/helpers/module_info_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module Generamba

class ModuleInfoGenerator
attr_reader :scope

def initialize(code_module)
module_info = {
'name' => code_module.name,
'description' => code_module.description,
'project_name' => code_module.project_name,
'project_targets' => code_module.project_targets,
'test_targets' => code_module.test_targets
}

developer = {
'name' => code_module.author,
'company' => code_module.company
}

@scope = {
'year' => code_module.year,
'date' => Time.now.strftime('%d/%m/%Y'),
'developer' => developer,
'module_info' => module_info,
'prefix' => code_module.prefix,
'custom_parameters' => code_module.custom_parameters
}
end

end

end
40 changes: 26 additions & 14 deletions lib/generamba/helpers/xcodeproj_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@ def self.obtain_project(project_name)
# @param project [Xcodeproj::Project] The target xcodeproj file
# @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 dir_path [Pathname] The directory path for current file
# @param file_group_path [String] Directory path
# @param file_name [String] Current file name
# @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_is_resource = false)
module_group = self.retrieve_group_or_create_if_needed(group_path, project, true)
def self.add_file_to_project_and_targets(project, targets_name, group_path, dir_path, file_group_path, file_name, file_is_resource = false)
file_path = dir_path.join(file_group_path).join(file_name)
module_group = self.retrieve_group_or_create_if_needed(group_path, dir_path, file_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)

Expand All @@ -36,10 +38,12 @@ def self.add_file_to_project_and_targets(project, targets_name, group_path, file
# 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
# @param dir_path [Pathname] The directory path for current directory
# @param directory_name [String] Current directory name
#
# @return [void]
def self.add_group_to_project(project, group_path)
self.retrieve_group_or_create_if_needed(group_path, project, true)
def self.add_group_to_project(project, group_path, dir_path, directory_name)
self.retrieve_group_or_create_if_needed(group_path, dir_path, directory_name, project, true)
end

# File is a compiled source
Expand All @@ -64,7 +68,7 @@ def self.is_bundle_resource?(resource_name)
#
# @return [Void]
def self.clear_group(project, targets_name, group_path)
module_group = self.retrieve_group_or_create_if_needed(group_path, project, false)
module_group = self.retrieve_group_or_create_if_needed(group_path, nil, nil, project, false)
return unless module_group

files_path = self.files_path_from_group(module_group, project)
Expand All @@ -83,35 +87,43 @@ 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.retrieve_group_or_create_if_needed(group_path, project, false)
module_group = self.retrieve_group_or_create_if_needed(group_path, nil, nil, project, false)
module_group.nil? ? false : true
end

private

# Finds or creates a group in a xcodeproj file with a given path
# @param group_path [Pathname] The full group path
# @param group_path [Pathname] The Xcode group path for module
# @param dir_path [Pathname] The directory path for module
# @param file_group_path [String] Directory path
# @param project [Xcodeproj::Project] The working Xcode project file
# @param create_group_if_not_exists [TrueClass or FalseClass] If true notexistent group will be created
# @param create_group_if_not_exists [TrueClass or FalseClass] If true nonexistent group will be created
#
# @return [PBXGroup]
def self.retrieve_group_or_create_if_needed(group_path, project, create_group_if_not_exists)
def self.retrieve_group_or_create_if_needed(group_path, dir_path, file_group_path, project, create_group_if_not_exists)
group_names = path_names_from_path(group_path)
group_components_count = group_names.count
group_names += path_names_from_path(file_group_path) if file_group_path

final_group = project

group_names.each do |group_name|
group_names.each_with_index do |group_name, index|
next_group = final_group[group_name]

unless next_group
return nil unless create_group_if_not_exists

new_group_path = group_name
next_group = final_group.new_group(group_name, new_group_path)
if group_path != dir_path && index == group_components_count-1
next_group = final_group.new_group(group_name, dir_path, :project)
else
next_group = final_group.new_group(group_name, group_name)
end
end

final_group = next_group
end

final_group
end

Expand Down
19 changes: 14 additions & 5 deletions lib/generamba/module_generator.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'fileutils'

require 'generamba/helpers/xcodeproj_helper.rb'
require 'generamba/helpers/module_info_generator.rb'

module Generamba

Expand Down Expand Up @@ -51,8 +52,8 @@ def generate_module(name, code_module, template)
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
puts "Test file path: #{code_module.test_file_path}".green if code_module.test_file_path
puts "Test group path: #{code_module.test_group_path}".green if code_module.test_group_path
end

def process_files_if_needed(files, name, code_module, template, project, targets, group_path, dir_path)
Expand All @@ -69,15 +70,17 @@ def process_files_if_needed(files, name, code_module, template, project, targets
file_group = dir_path.join(directory_name)

FileUtils.mkdir_p file_group
XcodeprojHelper.add_group_to_project(project, file_group)
XcodeprojHelper.add_group_to_project(project, group_path, dir_path, directory_name)

next
end

file_group = File.dirname(file[TEMPLATE_NAME_KEY])

module_info = ModuleInfoGenerator.new(code_module)

# Generating the content of the code file and it's name
file_name, file_content = ContentGenerator.create_file(file, code_module, template)
file_name, file_content = ContentGenerator.create_file(file, module_info.scope, template)
file_path = dir_path.join(file_group).join(file_name)

# Creating the file in the filesystem
Expand All @@ -89,7 +92,13 @@ def process_files_if_needed(files, name, code_module, template, project, targets
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)
XcodeprojHelper.add_file_to_project_and_targets(project,
targets,
group_path,
dir_path,
file_group,
file_name,
file_is_resource)
end
end
end
Expand Down

0 comments on commit 6541fc3

Please sign in to comment.