Skip to content

Commit

Permalink
[RESOLVED] Updated logic of rambafile and module validation #144
Browse files Browse the repository at this point in the history
  • Loading branch information
Beniamin Sarkisyan committed Sep 3, 2016
1 parent c371453 commit e4133fb
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 71 deletions.
10 changes: 6 additions & 4 deletions lib/generamba/cli/gen_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require 'generamba/helpers/dependency_checker.rb'
require 'generamba/helpers/gen_command_table_parameters_formatter.rb'
require 'generamba/helpers/module_info_generator.rb'
require 'generamba/helpers/module_validator.rb'

module Generamba::CLI
class Application < Thor
Expand Down Expand Up @@ -38,12 +39,13 @@ def gen(module_name, template_name)
setup_username_command = Generamba::CLI::SetupUsernameCommand.new
setup_username_command.setup_username

default_module_description = "#{module_name} module"
module_description = options[:description] ? options[:description] : default_module_description

rambafile = YAML.load_file(RAMBAFILE_NAME)

code_module = CodeModule.new(module_name, module_description, rambafile, options)
code_module = CodeModule.new(module_name, rambafile, options)

module_validator = ModuleValidator.new
module_validator.validate(code_module)

module_info = ModuleInfoGenerator.new(code_module)
template = ModuleTemplate.new(template_name, module_info.scope)

Expand Down
15 changes: 8 additions & 7 deletions lib/generamba/code_generation/Rambafile.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ project_name: {{ project_name }}
xcodeproj_path: {{ xcodeproj_path }}
{% if prefix != "" %}prefix: {{ prefix }}{% endif %}

### Code generation settings section
# The main project target name
project_target: {{ project_target }}
{% if (project_target != nil and project_target != "") or (project_file_path != nil and project_file_path != "") or (project_group_path != nil and project_group_path != "") %}### Code generation settings section
{% if project_target != nil and project_target != "" %}# The main project target name
project_target: {{ project_target }}{% endif %}

# The file path for new modules
project_file_path: {{ project_file_path }}
{% if project_file_path != nil and project_file_path != "" %}# The file path for new modules
project_file_path: {{ project_file_path }}{% endif %}

# The Xcode group path to new modules
project_group_path: {{ project_group_path }}
{% if project_group_path != nil and project_group_path != "" %}# The Xcode group path to new modules
project_group_path: {{ project_group_path }}{% endif %}
{% endif %}

{% if (test_target != nil and test_target != "") or (test_file_path != nil and test_file_path != "") or (test_group_path != nil and test_group_path != "") %}### Tests generation settings section
{% if test_target != nil and test_target != "" %}# The tests target name
Expand Down
92 changes: 33 additions & 59 deletions lib/generamba/code_generation/code_module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ module Generamba

SLASH_REGEX = /^\/|\/$/
C99IDENTIFIER = /[^\w]/


PATH_TYPE_MODULE = 'module'
PATH_TYPE_TEST = 'test'

# Represents currently generating code module
class CodeModule
attr_reader :name,
Expand All @@ -24,17 +27,11 @@ class CodeModule
:cartfile_path,
:custom_parameters

def initialize(name, description, rambafile, options)
def initialize(name, rambafile, options)
# Base initialization
@name = name
@description = description

if rambafile[AUTHOR_NAME_KEY] != nil
@author = rambafile[AUTHOR_NAME_KEY]
else
@author = UserPreferences.obtain_username
end

@description = options[:description] ? options[:description] : "#{name} module"
@author = rambafile[AUTHOR_NAME_KEY] ? rambafile[AUTHOR_NAME_KEY] : UserPreferences.obtain_username
@company = rambafile[COMPANY_KEY]
@year = Time.now.year.to_s

Expand All @@ -43,27 +40,14 @@ def initialize(name, description, rambafile, options)
@product_module_name = rambafile[PRODUCT_MODULE_NAME_KEY] || @project_name.gsub(C99IDENTIFIER, '_')
@xcodeproj_path = rambafile[XCODEPROJ_PATH_KEY]

@module_file_path = rambafile[PROJECT_FILE_PATH_KEY].gsub(SLASH_REGEX, '')
@module_file_path = Pathname.new(@module_file_path).join(@name)

@module_group_path = rambafile[PROJECT_GROUP_PATH_KEY].gsub(SLASH_REGEX, '')
@module_group_path = Pathname.new(@module_group_path).join(@name)

if rambafile[TEST_FILE_PATH_KEY] != nil
@test_file_path = rambafile[TEST_FILE_PATH_KEY].gsub(SLASH_REGEX, '')
@test_file_path = Pathname.new(@test_file_path).join(@name)
end

if rambafile[TEST_GROUP_PATH_KEY] != nil
@test_group_path = rambafile[TEST_GROUP_PATH_KEY].gsub(SLASH_REGEX, '')
@test_group_path = Pathname.new(@test_group_path).join(@name)
end
setup_file_and_group_paths(rambafile[PROJECT_FILE_PATH_KEY], rambafile[PROJECT_GROUP_PATH_KEY], PATH_TYPE_MODULE)
setup_file_and_group_paths(rambafile[TEST_FILE_PATH_KEY], rambafile[TEST_GROUP_PATH_KEY], PATH_TYPE_TEST)

@project_targets = [rambafile[PROJECT_TARGET_KEY]] if rambafile[PROJECT_TARGET_KEY] != nil
@project_targets = rambafile[PROJECT_TARGETS_KEY] if rambafile[PROJECT_TARGETS_KEY] != nil
@project_targets = [rambafile[PROJECT_TARGET_KEY]] if rambafile[PROJECT_TARGET_KEY]
@project_targets = rambafile[PROJECT_TARGETS_KEY] if rambafile[PROJECT_TARGETS_KEY]

@test_targets = [rambafile[TEST_TARGET_KEY]] if rambafile[TEST_TARGET_KEY] != nil
@test_targets = rambafile[TEST_TARGETS_KEY] if rambafile[TEST_TARGETS_KEY] != nil
@test_targets = [rambafile[TEST_TARGET_KEY]] if rambafile[TEST_TARGET_KEY]
@test_targets = rambafile[TEST_TARGETS_KEY] if rambafile[TEST_TARGETS_KEY]

# Custom parameters
@custom_parameters = options[:custom_parameters]
Expand All @@ -73,42 +57,32 @@ def initialize(name, description, rambafile, options)
@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]
@module_file_path = options[:module_file_path].gsub(SLASH_REGEX, '')
@module_file_path = Pathname.new(@module_file_path).join(@name)
end
setup_file_and_group_paths(options[:module_file_path], options[:module_group_path], PATH_TYPE_MODULE)
setup_file_and_group_paths(options[:test_file_path], options[:test_group_path], PATH_TYPE_TEST)

if options[:module_group_path]
@module_group_path = options[:module_group_path].gsub(SLASH_REGEX, '')
@module_group_path = Pathname.new(@module_group_path).join(@name)
end
# The priority is given to `module_path` and 'test_path' options
setup_file_and_group_paths(options[:module_path], options[:module_path], PATH_TYPE_MODULE)
setup_file_and_group_paths(options[:test_path], options[:test_path], PATH_TYPE_TEST)

if options[:test_file_path]
@test_file_path = options[:test_file_path].gsub(SLASH_REGEX, '')
@test_file_path = Pathname.new(@test_file_path).join(@name)
end
@podfile_path = rambafile[PODFILE_PATH_KEY] if rambafile[PODFILE_PATH_KEY]
@cartfile_path = rambafile[CARTFILE_PATH_KEY] if rambafile[CARTFILE_PATH_KEY]
end

if options[:test_group_path]
@test_group_path = options[:test_group_path].gsub(SLASH_REGEX, '')
@test_group_path = Pathname.new(@test_group_path).join(@name)
end
def setup_file_and_group_paths(file_path, group_path, path_type)
if file_path || group_path
file_path = group_path unless file_path
group_path = file_path unless group_path

# The priority is given to `module_path` and 'test_path' options
if options[:module_path]
@module_path = options[:module_path].gsub(SLASH_REGEX, '')
@module_file_path = Pathname.new(@module_path).join(@name)
@module_group_path = Pathname.new(@module_path).join(@name)
end
variable_name = "#{path_type}_file_path"
variable_value = file_path.gsub(SLASH_REGEX, '')
variable_value = Pathname.new(variable_value).join(@name)
instance_variable_set("@#{variable_name}", variable_value)

if options[:test_path]
@test_path = options[:test_path].gsub(SLASH_REGEX, '')
@test_file_path = Pathname.new(@test_path).join(@name)
@test_group_path = Pathname.new(@test_path).join(@name)
variable_name = "#{path_type}_group_path"
variable_value = group_path.gsub(SLASH_REGEX, '')
variable_value = Pathname.new(variable_value).join(@name)
instance_variable_set("@#{variable_name}", variable_value)
end

@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
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def self.prepare_parameters_for_displaying(code_module, template_name)

params['Template'] = template_name

if code_module.custom_parameters
unless code_module.custom_parameters.empty?
params['Custom parameters'] = code_module.custom_parameters.to_json
end

Expand Down
27 changes: 27 additions & 0 deletions lib/generamba/helpers/module_validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Generamba
# Provides methods for validating module
class ModuleValidator

# Method validates module
# @param code_module [CodeModule] The instance of CodeModule
#
# @return [Void]
def validate(code_module)
mandatory_fields = [COMPANY_KEY,
PROJECT_PREFIX_KEY,
PROJECT_NAME_KEY,
XCODEPROJ_PATH_KEY,
PROJECT_TARGETS_KEY,
'module_file_path',
'module_group_path']

mandatory_fields.each do |field|
unless code_module.instance_variable_get("@#{field}")
puts "Module is broken! *#{field}* field cannot be empty, because it is mandatory.".red
exit
end
end
end

end
end
5 changes: 5 additions & 0 deletions lib/generamba/helpers/rambafile_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ def validate(path)
#
# @return [Array]
def all_project_failure_fields(preferences)
file_path = preferences[PROJECT_FILE_PATH_KEY]
group_path = preferences[PROJECT_GROUP_PATH_KEY]

return [] if !file_path || !group_path

all_nil_mandatory_fields_for_target_type('project', preferences)
end

Expand Down

0 comments on commit e4133fb

Please sign in to comment.