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

.full_path(object) ⇒ Pathname

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

Parameters:

Returns:

  • (Pathname)

    The path of the object without resolving the source tree.



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/xcodeproj/project/object/helpers/groupable_helper.rb', line 116

def full_path(object)
  folder =  case object.source_tree
            when '<group>'
              object_parent = parent(object)
              if object_parent.isa == 'PBXProject'.freeze
                nil
              else
                full_path(object_parent)
              end
            when 'SOURCE_ROOT'
              nil
            when '<absolute>'
              Pathname.new('/'.freeze)
            else
              Pathname.new("${#{object.source_tree}}")
            end
  folder ||= Pathname.new('')
  if object.path
    folder + object.path
  else
    folder
  end
end

.hierarchy_path(object) ⇒ String

Returns A representation of the group hierarchy.

Parameters:

Returns:

  • (String)

    A representation of the group hierarchy.



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

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.



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

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:



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

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:



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

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:



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

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.



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

def real_path(object)
  source_tree = source_tree_real_path(object)
  path = object.path || ''.freeze
  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:



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/xcodeproj/project/object/helpers/groupable_helper.rb', line 207

def set_path_with_source_tree(object, path, source_tree)
  path = Pathname(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.



184
185
186
187
# File 'lib/xcodeproj/project/object/helpers/groupable_helper.rb', line 184

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.



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/xcodeproj/project/object/helpers/groupable_helper.rb', line 146

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