Class: Generamba::XcodeprojHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/generamba/helpers/xcodeproj_helper.rb

Overview

Provides a number of helper methods for working with xcodeproj gem

Class Method Summary collapse

Class Method Details

.add_file_to_project_and_targets(project, targets_name, group_path, dir_path, file_group_path, file_name, root_path, file_is_resource = false) ⇒ void

This method returns an undefined value.

Adds a provided file to a specific Project and Target

Parameters:

  • project (Xcodeproj::Project)

    The target xcodeproj file

  • targets_name (String)

    Array of targets name

  • group_path (Pathname)

    The Xcode group path for current file

  • dir_path (Pathname)

    The directory path for current file

  • file_group_path (String)

    Directory path

  • file_name (String)

    Current file name

  • file_is_resource (TrueClass or FalseClass) (defaults to: false)

    If true then file is resource



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/generamba/helpers/xcodeproj_helper.rb', line 22

def self.add_file_to_project_and_targets(project, targets_name, group_path, dir_path, file_group_path, file_name, root_path, file_is_resource = false)
  
  if root_path
      file_path = root_path
  else
      file_path = dir_path
      file_path = file_path.join(file_group_path) if file_group_path
  end

  file_path = file_path.join(file_name) if file_name

  module_group = self.retrieve_group_or_create_if_needed(group_path, dir_path, file_group_path, project, true, root_path)
  xcode_file = module_group.new_file(File.absolute_path(file_path))

  targets_name.each do |target|
    xcode_target = obtain_target(target, project)

    if file_is_resource || self.is_bundle_resource?(file_name)
      xcode_target.add_resources([xcode_file])
    elsif self.is_compile_source?(file_name)
      xcode_target.add_file_references([xcode_file])
    end
  end
end

.add_group_to_project(project, group_path, dir_path, directory_name, group_is_logical) ⇒ void

This method returns an undefined value.

Adds a provided directory to a specific Project

Parameters:

  • project (Xcodeproj::Project)

    The target xcodeproj file

  • group_path (Pathname)

    The Xcode group path for current directory

  • dir_path (Pathname)

    The directory path for current directory

  • directory_name (String)

    Current directory name



54
55
56
# File 'lib/generamba/helpers/xcodeproj_helper.rb', line 54

def self.add_group_to_project(project, group_path, dir_path, directory_name, group_is_logical)
  self.retrieve_group_or_create_if_needed(group_path, dir_path, directory_name, project, true, group_is_logical)
end

.clear_group(project, targets_name, group_path, group_is_logical) ⇒ Void

Recursively clears children of the given group

Parameters:

  • project (Xcodeproj::Project)

    The working Xcode project file

  • group_path (Pathname)

    The full group path

Returns:

  • (Void)


79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/generamba/helpers/xcodeproj_helper.rb', line 79

def self.clear_group(project, targets_name, group_path, group_is_logical)
  module_group = self.retrieve_group_or_create_if_needed(group_path, nil, nil, project, false, group_is_logical)
  return unless module_group

  files_path = self.files_path_from_group(module_group, project)
  return unless files_path

  files_path.each do |file_path|
    self.remove_file_by_file_path(file_path, targets_name, project)
  end

  module_group.clear
end

.is_bundle_resource?(resource_name) ⇒ TrueClass or FalseClass

File is a resource

Parameters:

  • resource_name (String)

    String of resource name

Returns:

  • (TrueClass or FalseClass)


70
71
72
# File 'lib/generamba/helpers/xcodeproj_helper.rb', line 70

def self.is_bundle_resource?(resource_name)
  File.extname(resource_name) == '.xib' || File.extname(resource_name) == '.storyboard'
end

.is_compile_source?(file_name) ⇒ TrueClass or FalseClass

File is a compiled source

Parameters:

  • file_name (String)

    String of file name

Returns:

  • (TrueClass or FalseClass)


62
63
64
# File 'lib/generamba/helpers/xcodeproj_helper.rb', line 62

def self.is_compile_source?(file_name)
  File.extname(file_name) == '.m' || File.extname(file_name) == '.swift' || File.extname(file_name) == '.mm'
end

.module_with_group_path_already_exists(project, group_path, group_is_logical) ⇒ TrueClass or FalseClass

Finds a group in a xcodeproj file with a given path

Parameters:

  • project (Xcodeproj::Project)

    The working Xcode project file

  • group_path (Pathname)

    The full group path

Returns:

  • (TrueClass or FalseClass)


98
99
100
101
# File 'lib/generamba/helpers/xcodeproj_helper.rb', line 98

def self.module_with_group_path_already_exists(project, group_path, group_is_logical)
  module_group = self.retrieve_group_or_create_if_needed(group_path, nil, nil, project, false, group_is_logical)
  module_group.nil? ? false : true
end

.obtain_project(project_name) ⇒ Xcodeproj::Project

Returns a PBXProject class for a given name

Parameters:

  • project_name (String)

    The name of the project file

Returns:

  • (Xcodeproj::Project)


8
9
10
# File 'lib/generamba/helpers/xcodeproj_helper.rb', line 8

def self.obtain_project(project_name)
  Xcodeproj::Project.open(project_name)
end