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

Fixed #64: Errors if default path is empty #66

Merged
merged 6 commits into from
Jan 7, 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
97 changes: 77 additions & 20 deletions generamba/lib/generamba/helpers/rambafile_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,100 @@ class RambafileValidator

# Method validates Rambafile contents
# @param path [String] The path to a Rambafile
#
# @return [Void]
def validate(path)
file_contents = open(path).read
preferences = file_contents.empty? ? {} : YAML.load(file_contents).to_hash

mandatory_fields = [COMPANY_KEY,
PROJECT_NAME_KEY,
XCODEPROJ_PATH_KEY,
PROJECT_FILE_PATH_KEY,
PROJECT_GROUP_PATH_KEY,
TEMPLATES_KEY]

interchangable_fields = [[PROJECT_TARGETS_KEY, PROJECT_TARGET_KEY]]
XCODEPROJ_PATH_KEY]

mandatory_fields.each do |field|
unless preferences.has_key?(field)
error_description = "Rambafile is broken! Cannot find #{field} field, which is mandatory. Either add it manually, or run *generamba setup*.".red
raise StandardError.new(error_description)
unless preferences[field]
puts "Rambafile is broken! *#{field}* field cannot be empty, because it is mandatory. Either add it manually, or run *generamba setup*.".red
exit
end
end

interchangable_fields.each do |fields_array|
has_value = false
fields_array.each do |field|
has_value = preferences.has_key?(field) || has_value
end
project_failure_fields = all_project_failure_fields(preferences)
test_failure_fields = all_test_failure_fields(preferences)
failure_fields = project_failure_fields + test_failure_fields

unless has_value
error_description = "Rambafile is broken! Cannot find any of #{fields_array} fields, one of them is mandatory. Either add it manually, or run *generamba setup*.".red
raise StandardError.new(error_description)
end
if failure_fields.count > 0
puts "Rambafile is broken! Cannot find any of #{failure_fields} fields, one of them is mandatory. Either add it manually, or run *generamba setup*.".red
exit
end

unless preferences[TEMPLATES_KEY]
error_description = "You can't run *generamba gen* without any templates installed. Add their declarations to a Rambafile and run *generamba template install*.".red
raise StandardError.new(error_description)
puts "You can't run *generamba gen* without any templates installed. Add their declarations to a Rambafile and run *generamba template install*.".red
exit
end
end

private

# Method which return all project failure fields
# @param preferences [Hash] Converted Rambafile
#
# @return [Array]
def all_project_failure_fields(preferences)
return all_nil_mandatory_fields_for_target_type("project", preferences)
end

# Method which return all test failure fields
# @param preferences [Hash] Converted Rambafile
#
# @return [Array]
def all_test_failure_fields(preferences)
target = preferences[TEST_TARGET_KEY]
targets = preferences[TEST_TARGETS_KEY]
file_path = preferences[TEST_FILE_PATH_KEY]
group_path = preferences[TEST_GROUP_PATH_KEY]

has_test_fields = target || targets || file_path || group_path

unless has_test_fields
return []
end

return all_nil_mandatory_fields_for_target_type("test", preferences)
end

# Method which return all failure fields for target_type
# @param target_type [String] "project" or "test"
# @param preferences [Hash] Converted Rambafile
#
# @return [Array]
def all_nil_mandatory_fields_for_target_type(target_type, preferences)
target_type = target_type.upcase

target_const_value = Generamba.const_get(target_type + "_TARGET_KEY")
targets_const_value = Generamba.const_get(target_type + "_TARGETS_KEY")

target = preferences[target_const_value]
targets = preferences[targets_const_value]

fields = []

if !target && (!targets || (targets && targets.count == 0))
fields.push(target_const_value)
end

file_path_const_value = Generamba.const_get(target_type + "_FILE_PATH_KEY")

unless preferences[file_path_const_value]
fields.push(file_path_const_value)
end

group_path_const_value = Generamba.const_get(target_type + "_GROUP_PATH_KEY")

unless preferences[group_path_const_value]
fields.push(group_path_const_value)
end

return fields
end

end
Expand Down
56 changes: 33 additions & 23 deletions generamba/lib/generamba/module_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Generamba

# Responsible for creating the whole code module using information from the CLI
class ModuleGenerator

def generate_module(name, code_module, template)
# Setting up Xcode objects
project = XcodeprojHelper.obtain_project(code_module.xcodeproj_path)
Expand All @@ -20,33 +21,41 @@ def generate_module(name, code_module, template)
# Creating code files
puts('Creating code files...')
process_files_if_needed(template.code_files,
name,
code_module,
template,
project,
code_module.project_targets,
code_module.module_group_path,
code_module.module_file_path)
name,
code_module,
template,
project,
code_module.project_targets,
code_module.module_group_path,
code_module.module_file_path)

# Creating test files
puts('Creating test files...')
process_files_if_needed(template.test_files,
name,
code_module,
template,
project,
code_module.test_targets,
code_module.test_group_path,
code_module.test_file_path)
included_tests = code_module.test_targets && code_module.test_group_path && code_module.test_file_path

if included_tests
puts('Creating test files...')
process_files_if_needed(template.test_files,
name,
code_module,
template,
project,
code_module.test_targets,
code_module.test_group_path,
code_module.test_file_path)
end

# 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: #{code_module.test_file_path}".green + "\n" +
"Test group path: #{code_module.test_group_path}".green)
"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)
end

def process_files_if_needed(files, name, code_module, template, project, targets, group_path, dir_path)
Expand All @@ -56,7 +65,7 @@ def process_files_if_needed(files, name, code_module, template, project, targets
return
end

XcodeprojHelper.clear_group(project, targets, group_path)
XcodeprojHelper.clear_group(project, 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
Expand All @@ -68,7 +77,8 @@ def process_files_if_needed(files, name, code_module, template, project, targets

# Generating the content of the code file
file_content = ContentGenerator.create_file_content(file, code_module, template)
file_path = dir_path.join(file_group).join(file_name)
file_path = dir_path.join(file_group)
.join(file_name)

# Creating the file in the filesystem
FileUtils.mkdir_p File.dirname(file_path)
Expand Down