Skip to content

Commit

Permalink
Merge pull request #113 from alexgarbarev/develop
Browse files Browse the repository at this point in the history
New "file_name" property for template files in rambaspec
  • Loading branch information
etolstoy committed May 10, 2016
2 parents 114a740 + 65fd013 commit dbc8189
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
28 changes: 21 additions & 7 deletions lib/generamba/code_generation/content_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@ module Generamba
# Responsible for generating code using provided liquid templates
class ContentGenerator

# Generates and returns a body of a specific code file.
# 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 template [ModuleTemplate] The model describing a Generamba template used for code generation
#
# @return [String] The generated body
def self.create_file_content(file, code_module, template)
# @return [String], [String] The generated file_name and body
def self.create_file(file, code_module, 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')

template = Liquid::Template.parse(file_source)
file_name = File.basename(file[TEMPLATE_FILE_NAME_KEY])
filename_template = self.file_name_template(file)

file_basename = File.basename(file[TEMPLATE_FILE_NAME_KEY])

module_info = {
'name' => code_module.name,
'file_name' => file_name,
'description' => code_module.description,
'project_name' => code_module.project_name,
'project_targets' => code_module.project_targets,
Expand All @@ -39,10 +40,23 @@ def self.create_file_content(file, code_module, template)
'module_info' => module_info,
'prefix' => code_module.prefix
}

module_info['file_basename'] = file_basename

output = template.render(scope)
file_name = filename_template.render(scope)

return output
module_info['file_name'] = file_name
module_info.delete('file_basename')

content = template.render(scope)

return file_name, content
end

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
return Liquid::Template.parse(template_text)
end
end
end
1 change: 1 addition & 0 deletions lib/generamba/constants/rambaspec_constants.rb
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module Generamba
TEMPLATE_TEST_FILES_KEY = 'test_files'
TEMPLATE_FILE_NAME_KEY = 'name'
TEMPLATE_FILE_PATH_KEY = 'path'
TEMPLATE_FILE_FILENAME_KEY = 'file_name'

TEMPLATE_DEPENDENCIES_KEY = 'dependencies'
end
13 changes: 4 additions & 9 deletions lib/generamba/module_generator.rb
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,14 @@ def process_files_if_needed(files, name, code_module, template, project, targets

XcodeprojHelper.clear_group(project, targets, group_path)
files.each do |file|
# The target file's name consists of three parameters: project prefix, module name and template file name.
# E.g. RDS + Authorization + Presenter.h = RDSAuthorizationPresenter.h
file_basename = name + File.basename(file[TEMPLATE_NAME_KEY])
prefix = code_module.prefix
file_name = prefix ? prefix + file_basename : file_basename


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)
# 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|
Expand Down

0 comments on commit dbc8189

Please sign in to comment.