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

Helpers code style #133

Merged
merged 2 commits into from
Aug 12, 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
.idea/workspace.xml
.rakeTasks
encodings.xml
Gemfile.lock
10 changes: 3 additions & 7 deletions lib/generamba/helpers/dependency_checker.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
require 'cocoapods-core'

module Generamba

# Provides methods for check dependencies from rambaspec in podfile
class DependencyChecker

# Check Podfile for dependencies
# @param dependencies [Array] Array of dependencies name
# @param podfile_path [String] String of Podfile path
#
# @return [void]
def self.check_all_required_dependencies_has_in_podfile(dependencies, podfile_path)
return if !dependencies or dependencies.count == 0 or !podfile_path
return if !dependencies || dependencies.count == 0 || !podfile_path

dependencies_names = []
Pod::Podfile.from_file(Pathname.new(podfile_path)).dependencies.each do |dependency|
Expand All @@ -37,7 +35,7 @@ def self.check_all_required_dependencies_has_in_podfile(dependencies, podfile_pa
#
# @return [void]
def self.check_all_required_dependencies_has_in_cartfile(dependencies, cartfile_path)
return if !dependencies or dependencies.count == 0 or !cartfile_path
return if !dependencies || dependencies.count == 0 || !cartfile_path

cartfile_string = File.read(cartfile_path)

Expand All @@ -52,7 +50,5 @@ def self.check_all_required_dependencies_has_in_cartfile(dependencies, cartfile_
puts "[Warning] Dependencies #{not_existing_dependency} missed in Cartfile".yellow
end
end

end

end
end
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
module Generamba

# Provides methods for prepare parameters for displaying in table.
class GenCommandTableParametersFormatter

# This method prepared parameter for displaying
def self.prepare_parameters_for_displaying(parameters)
params = parameters.clone
Expand All @@ -15,9 +13,7 @@ def self.prepare_parameters_for_displaying(parameters)

params['templates'] = templates.join("\n")

return params
params
end

end

end
6 changes: 1 addition & 5 deletions lib/generamba/helpers/print_table.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
module Generamba

# Provides methods for print parameters in nice table.
class PrintTable

# This method prints out all the user inputs in a nice table.
def self.print_values(values: nil, title: nil)
require 'terminal-table'
Expand All @@ -15,7 +13,5 @@ def self.print_values(values: nil, title: nil)
puts Terminal::Table.new(params)
puts ''
end

end

end
end
29 changes: 12 additions & 17 deletions lib/generamba/helpers/rambafile_validator.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
module Generamba

# Provides methods for validating Rambafile contents
class RambafileValidator

# Method validates Rambafile contents
# @param path [String] The path to a Rambafile
#
Expand Down Expand Up @@ -44,7 +42,7 @@ def validate(path)
#
# @return [Array]
def all_project_failure_fields(preferences)
return all_nil_mandatory_fields_for_target_type("project", preferences)
all_nil_mandatory_fields_for_target_type('project', preferences)
end

# Method which return all test failure fields
Expand All @@ -59,23 +57,21 @@ def all_test_failure_fields(preferences)

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)
return [] unless has_test_fields

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_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]
Expand All @@ -86,20 +82,19 @@ def all_nil_mandatory_fields_for_target_type(target_type, preferences)
fields.push(target_const_value)
end

file_path_const_value = Generamba.const_get(target_type + "_FILE_PATH_KEY")
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
end

group_path_const_value = Generamba.const_get(target_type + "_GROUP_PATH_KEY")
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
fields
end

end
end
end
14 changes: 6 additions & 8 deletions lib/generamba/helpers/template_helper.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
module Generamba

# Provides a number of helper methods for manipulating Generamba template files
class TemplateHelper

# Returns a file path for a specific template .rambaspec file
# @param template_name [String] The Generamba template name
#
Expand All @@ -11,7 +9,7 @@ def self.obtain_spec(template_name)
template_path = self.obtain_path(template_name)
spec_path = template_path.join(template_name + RAMBASPEC_EXTENSION)

return spec_path
spec_path
end

# Returns a file path for a specific template folder
Expand All @@ -20,13 +18,13 @@ def self.obtain_spec(template_name)
# @return [Pathname]
def self.obtain_path(template_name)
path = Pathname.new(Dir.getwd)
.join(TEMPLATES_FOLDER)
.join(template_name)
.join(TEMPLATES_FOLDER)
.join(template_name)

error_description = "Cannot find template named #{template_name}! Add it to the Rambafile and run *generamba template install*".red
raise StandardError.new(error_description) unless path.exist?
raise StandardError, error_description unless path.exist?

return path
path
end
end
end
end
65 changes: 28 additions & 37 deletions lib/generamba/helpers/xcodeproj_helper.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
module Generamba

# Provides a number of helper methods for working with xcodeproj gem
class XcodeprojHelper

# Returns a PBXProject class for a given name
# @param project_name [String] The name of the project file
#
Expand All @@ -20,23 +18,23 @@ def self.obtain_project(project_name)
#
# @return [void]
def self.add_file_to_project_and_targets(project, targets_name, group_path, file_path, file_type = nil)
module_group = self.retrieve_group_or_create_if_needed(group_path, project, true)
module_group = 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 = self.obtain_target(target, project)
xcode_target = obtain_target(target, project)

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

if file_type != nil
self.add_file_to_target(xcode_target, xcode_file, file_type)
unless file_type.nil?
add_file_to_target(xcode_target, xcode_file, file_type)
end
end
end
Expand All @@ -49,20 +47,20 @@ def self.add_file_to_project_and_targets(project, targets_name, group_path, file
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"
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

Expand Down Expand Up @@ -97,7 +95,7 @@ def self.clear_group(project, targets_name, group_path)
files_path.each do |file_path|
self.remove_file_by_file_path(file_path, targets_name, project)
end

module_group.clear
end

Expand All @@ -108,7 +106,7 @@ 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)
return module_group == nil ? false : true
module_group.nil? ? false : true
end

private
Expand All @@ -128,18 +126,15 @@ def self.retrieve_group_or_create_if_needed(group_path, project, create_group_if
next_group = final_group[group_name]

unless next_group
unless create_group_if_not_exists
return nil
end
return nil unless create_group_if_not_exists

new_group_path = group_name
next_group = final_group.new_group(group_name, new_group_path)
end

final_group = next_group
end

return final_group
final_group
end

# Returns an AbstractTarget class for a given name
Expand All @@ -149,22 +144,19 @@ def self.retrieve_group_or_create_if_needed(group_path, project, create_group_if
# @return [Xcodeproj::AbstractTarget]
def self.obtain_target(target_name, project)
project.targets.each do |target|
if target.name == target_name
return target
end
return target if target.name == target_name
end

error_description = "Cannot find a target with name #{target_name} in Xcode project".red
raise StandardError.new(error_description)
raise StandardError, error_description
end

# Splits the provided Xcode path to an array of separate paths
# @param path The full group or file path
#
# @return [[String]]
def self.path_names_from_path(path)
paths = path.to_s.split('/')
return paths
path.to_s.split('/')
end

# Remove build file from target build phase
Expand All @@ -188,7 +180,7 @@ def self.remove_file_by_file_path(file_path, targets_name, project)
end

def self.remove_file_from_build_phases(file_path, build_phases)
return if build_phases == nil
return if build_phases.nil?

build_phases.each do |build_phase|
build_phase.files.each do |build_file|
Expand Down Expand Up @@ -220,7 +212,7 @@ def self.build_phases_from_targets(targets_name, project)
end
end

return build_phases
build_phases
end

# Find and return target resources build phase
Expand All @@ -236,7 +228,7 @@ def self.resources_build_phase_from_targets(targets_name, project)
resource_build_phase.push(xcode_target.resources_build_phase)
end

return resource_build_phase
resource_build_phase
end

# Get configure file full path
Expand All @@ -247,26 +239,25 @@ def self.configure_file_ref_path(file_ref)
build_file_ref_path = file_ref.hierarchy_path.to_s
build_file_ref_path[0] = ''

return build_file_ref_path
build_file_ref_path
end

# Get all files path from group path
# @param module_group [PBXGroup] The module group
# @param project [Xcodeproj::Project] The target xcodeproj file
#
# @return [[String]]
def self.files_path_from_group(module_group, project)
def self.files_path_from_group(module_group, _project)
files_path = []

module_group.recursive_children.each do |file_ref|
if file_ref.isa == 'PBXFileReference'
file_ref_path = self.configure_file_ref_path(file_ref)
file_ref_path = configure_file_ref_path(file_ref)
files_path.push(file_ref_path)
end
end

return files_path
files_path
end

end
end
end