Class: XcodeProject::PBXGroup

Inherits:
FileNode show all
Defined in:
lib/xcodeproject/pbx_group.rb

Constant Summary

Constants inherited from FileNode

FileNode::SourceTreeMap

Instance Attribute Summary

Attributes inherited from FileNode

#name, #path, #source_tree

Attributes inherited from Node

#isa, #uuid

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from FileNode

#group_path, #parent, #total_path

Constructor Details

#initialize(root, uuid, data) ⇒ PBXGroup

Returns a new instance of PBXGroup.



31
32
33
34
# File 'lib/xcodeproject/pbx_group.rb', line 31

def initialize(root, uuid, data)
  super(root, uuid, data)
  @children = data['children']
end

Class Method Details

.add(root, name, path = nil) ⇒ Object



180
181
182
183
# File 'lib/xcodeproject/pbx_group.rb', line 180

def self.add(root, name, path = nil)
  uuid, data = root.add_object(create_object_hash(name, path))
  PBXGroup.new(root, uuid, data)
end

.create_object_hash(name, path = nil) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/xcodeproject/pbx_group.rb', line 191

def self.create_object_hash(name, path = nil)
  path = path.to_s

  data = []
  data << %w[isa PBXGroup]
  data << ['children', []]
  data << ['name', name]
  data << ['path', path] unless path.empty?
  data << ['sourceTree', '<group>']

  Hash[data]
end

Instance Method Details

#absolute_path(path) ⇒ Object



139
140
141
142
# File 'lib/xcodeproject/pbx_group.rb', line 139

def absolute_path(path)
  path = Pathname.new(path)
  path.absolute? ? path : root.absolute_path(total_path.join(path))
end

#add_child_uuid(uuid) ⇒ Object



131
132
133
# File 'lib/xcodeproject/pbx_group.rb', line 131

def add_child_uuid(uuid)
  @children << uuid
end

#add_dir(path, gpath = nil) ⇒ Object

Raises:



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/xcodeproject/pbx_group.rb', line 92

def add_dir(path, gpath = nil)
  path = absolute_path(path)
  raise FilePathError, "No such directory '#{path}'." unless path.exist?

  gpath ||= path.basename
  parent = create_group(gpath, path)

  chs = path.entries.select { |obj| obj.to_s.start_with?('.') ? false : true }
  chs.each do |pn|
    parent.absolute_path(pn).directory? ? parent.add_dir(pn) : parent.add_file(pn)
  end
  parent
end

#add_file_ref(path) ⇒ Object Also known as: add_file

Raises:



168
169
170
171
172
173
174
175
176
177
178
# File 'lib/xcodeproject/pbx_group.rb', line 168

def add_file_ref(path)
  raise FilePathError, "No such file '#{absolute_path(path)}'." unless absolute_path(path).exist?

  name = File.basename(path)
  obj = file_ref(name)
  if obj.nil?
    obj = PBXFileReference.add(root, relative_path(path))
    add_child_uuid(obj.uuid)
  end
  obj
end

#add_group(gpath) ⇒ Object



88
89
90
# File 'lib/xcodeproject/pbx_group.rb', line 88

def add_group(gpath)
  create_group(gpath)
end

#child(gpath) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/xcodeproject/pbx_group.rb', line 48

def child(gpath)
  gpath = Pathname.new(gpath).cleanpath

  if gpath == gpath.basename
    name = gpath.to_s
    case name
    when '.'
      self
    when '..'
      parent
    else
      chs = children.select { |obj| obj.name == name }
      raise GroupPathError, 'The group contains two children with the same name.' if chs.size > 1
      chs.first
    end
  else
    pn, name = gpath.split
    group = child(pn)
    group.child(name) if group.is_a?(PBXGroup)
  end
end

#childrenObject



36
37
38
# File 'lib/xcodeproject/pbx_group.rb', line 36

def children
  @children.map { |uuid| root.object!(uuid) }
end

#create_group(gpath, path = nil) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/xcodeproject/pbx_group.rb', line 106

def create_group(gpath, path = nil)
  gpath = Pathname.new(gpath).cleanpath

  if gpath == gpath.basename
    name = gpath.to_s
    case name
    when '.'
      self
    when '..'
      parent
    else
      obj = group(name)
      if obj.nil?
        path = relative_path(path) unless path.nil?
        obj = PBXGroup.add(root, name, path)
        add_child_uuid(obj.uuid)
      end
      obj
    end
  else
    pn, name = gpath.split
    create_group(pn).create_group(name)
  end
end

#dir?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/xcodeproject/pbx_group.rb', line 79

def dir?
  !group?
end

#file_ref(gpath) ⇒ Object Also known as: file



70
71
72
73
# File 'lib/xcodeproject/pbx_group.rb', line 70

def file_ref(gpath)
  obj = child(gpath)
  obj.is_a?(PBXFileReference) ? obj : nil
end

#filesObject



44
45
46
# File 'lib/xcodeproject/pbx_group.rb', line 44

def files
  children.select { |child| child.is_a?(PBXFileReference) }
end

#group(gpath) ⇒ Object



83
84
85
86
# File 'lib/xcodeproject/pbx_group.rb', line 83

def group(gpath)
  obj = child(gpath)
  obj.is_a?(PBXGroup) ? obj : nil
end

#group?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/xcodeproject/pbx_group.rb', line 75

def group?
  data['path'].nil?
end

#groupsObject



40
41
42
# File 'lib/xcodeproject/pbx_group.rb', line 40

def groups
  children.select { |child| child.is_a?(PBXGroup) }
end

#relative_path(path) ⇒ Object



144
145
146
147
# File 'lib/xcodeproject/pbx_group.rb', line 144

def relative_path(path)
  path = Pathname.new(path)
  path.relative? ? path : path.relative_path_from(total_path)
end

#remove!Object



159
160
161
162
163
164
165
166
# File 'lib/xcodeproject/pbx_group.rb', line 159

def remove!
  return if parent.nil?

  children.each(&:remove!)

  parent.remove_child_uuid(uuid)
  root.remove_object(uuid)
end

#remove_child_uuid(uuid) ⇒ Object



135
136
137
# File 'lib/xcodeproject/pbx_group.rb', line 135

def remove_child_uuid(uuid)
  @children.delete(uuid)
end

#remove_file_ref(gpath) ⇒ Object Also known as: remove_file



149
150
151
152
# File 'lib/xcodeproject/pbx_group.rb', line 149

def remove_file_ref(gpath)
  obj = file(gpath)
  obj.remove! unless obj.nil?
end

#remove_group(gpath) ⇒ Object



154
155
156
157
# File 'lib/xcodeproject/pbx_group.rb', line 154

def remove_group(gpath)
  obj = group(gpath)
  obj.remove! unless obj.nil?
end