Class: Xcodeproj::Project::Object::GroupableHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/xcodeproj/project/object/helpers/groupable_helper.rb

Constant Summary collapse

SOURCE_TREES_BY_KEY =

Returns The source tree values by they symbol representation.

Returns:

  • (Hash{Symbol => String})

    The source tree values by they symbol representation.

{
  :absolute        => '<absolute>',
  :group           => '<group>',
  :project         => 'SOURCE_ROOT',
  :built_products  => 'BUILT_PRODUCTS_DIR',
  :developer_dir   => 'DEVELOPER_DIR',
  :sdk_root        => 'SDKROOT',
}.freeze

Class Method Summary collapse

Class Method Details

.hierarchy_path(object) ⇒ String

Returns A representation of the group hierarchy.

Parameters:

Returns:

  • (String)

    A representation of the group hierarchy.



48
49
50
51
52
53
54
# File 'lib/xcodeproj/project/object/helpers/groupable_helper.rb', line 48

def hierarchy_path(object)
  unless main_group?(object)
    parent = parent(object)
    parent = parent.hierarchy_path if parent.respond_to?(:hierarchy_path)
    "#{parent}/#{object.display_name}"
  end
end

.main_group?(object) ⇒ Bool

Returns Wether the object is the main group of the project.

Parameters:

Returns:

  • (Bool)

    Wether the object is the main group of the project.



61
62
63
# File 'lib/xcodeproj/project/object/helpers/groupable_helper.rb', line 61

def main_group?(object)
  object.equal?(object.project.main_group)
end

.move(object, new_parent) ⇒ void

This method returns an undefined value.

Moves the object to a new parent.

Parameters:



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/xcodeproj/project/object/helpers/groupable_helper.rb', line 75

def move(object, new_parent)
  unless object
    raise "[Xcodeproj] Attempt to move nil object to `#{new_parent}`."
  end
  unless new_parent
    raise "[Xcodeproj] Attempt to move object `#{object}` to nil parent."
  end
  if new_parent.equal?(object)
    raise "[Xcodeproj] Attempt to move object `#{object}` to itself."
  end
  if parents(new_parent).include?(object)
    raise "[Xcodeproj] Attempt to move object `#{object}` to a child object `#{new_parent}`."
  end

  object.parent.children.delete(object)
  new_parent << object
end

.parent(object) ⇒ PBXGroup, PBXProject

Returns The parent of the object.

Parameters:

Returns:



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/xcodeproj/project/object/helpers/groupable_helper.rb', line 11

def parent(object)
  referrers = object.referrers.uniq
  if referrers.count > 1
    referrers = referrers.grep(PBXGroup)
  end

  if referrers.count == 0
    raise '[Xcodeproj] Consistency issue: no parent ' \
      "for object `#{object.display_name}`: "\
      "`#{object.referrers.join('`, `')}`"
  elsif referrers.count > 1
    raise '[Xcodeproj] Consistency issue: unexpected multiple parents ' \
      "for object `#{object.display_name}`: "\
      "#{object.referrers}"
  end
  referrers.first
end

.parents(object) ⇒ Array<PBXGroup, PBXProject>

Returns The parents of the object.

Parameters:

Returns:



34
35
36
37
38
39
40
41
# File 'lib/xcodeproj/project/object/helpers/groupable_helper.rb', line 34

def parents(object)
  if main_group?(object)
    []
  else
    parent = parent(object)
    parents(parent).push(parent)
  end
end

.real_path(object) ⇒ Pathname

Returns The absolute path of the object resolving the source tree.

Parameters:

Returns:

  • (Pathname)

    The absolute path of the object resolving the source tree.



99
100
101
102
103
104
105
106
107
# File 'lib/xcodeproj/project/object/helpers/groupable_helper.rb', line 99

def real_path(object)
  source_tree = source_tree_real_path(object)
  path = object.path || ''
  if source_tree
    source_tree + path
  else
    Pathname(path)
  end
end

.set_path_with_source_tree(object, path, source_tree) ⇒ void

This method returns an undefined value.

Sets the path of the given object according to the provided source tree key. The path is converted to relative according to the real path of the source tree for group and project source trees, if both paths are relative or absolute. Otherwise the path is set as provided.

Parameters:



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/xcodeproj/project/object/helpers/groupable_helper.rb', line 175

def set_path_with_source_tree(object, path, source_tree)
  path = Pathname.new(path)
  source_tree = normalize_source_tree(source_tree)
  object.source_tree = source_tree

  if source_tree == SOURCE_TREES_BY_KEY[:absolute]
    unless path.absolute?
      raise '[Xcodeproj] Attempt to set a relative path with an ' \
        "absolute source tree: `#{path}`"
    end
    object.path = path.to_s
  elsif source_tree == SOURCE_TREES_BY_KEY[:group] || source_tree == SOURCE_TREES_BY_KEY[:project]
    source_tree_real_path = GroupableHelper.source_tree_real_path(object)
    if source_tree_real_path && source_tree_real_path.absolute? == path.absolute?
      relative_path = path.relative_path_from(source_tree_real_path)
      object.path = relative_path.to_s
    else
      object.path = path.to_s
    end
  else
    object.path = path.to_s
  end
end

.set_source_tree(object, source_tree) ⇒ void

This method returns an undefined value.

Sets the source tree of the given object.

Parameters:

  • source_tree (Symbol, String)

    The source tree, either a string or a key for SOURCE_TREES_BY_KEY.



152
153
154
155
# File 'lib/xcodeproj/project/object/helpers/groupable_helper.rb', line 152

def set_source_tree(object, source_tree)
  source_tree = normalize_source_tree(source_tree)
  object.source_tree = source_tree
end

.source_tree_real_path(object) ⇒ Pathname

Returns The absolute path of the source tree of the object.

Parameters:

Returns:

  • (Pathname)

    The absolute path of the source tree of the object.



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/xcodeproj/project/object/helpers/groupable_helper.rb', line 115

def source_tree_real_path(object)
  case object.source_tree
  when '<group>'
    if parent(object).isa == 'PBXProject'
      object.project.path.dirname
    else
      real_path(parent(object))
    end
  when 'SOURCE_ROOT'
    object.project.path.dirname
  when '<absolute>'
    nil
  else
    Pathname.new("${#{object.source_tree}}")
  end
end