Class: XcodeProject::PBXGroup

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

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.



7
8
9
10
# File 'lib/xcodeproject/pbx_group.rb', line 7

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

Class Method Details

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



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

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

.create_object_hash(name, path = nil) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/xcodeproject/pbx_group.rb', line 163

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

	data = []
	data << ['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



115
116
117
118
# File 'lib/xcodeproject/pbx_group.rb', line 115

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

#add_child_uuid(uuid) ⇒ Object



107
108
109
# File 'lib/xcodeproject/pbx_group.rb', line 107

def add_child_uuid (uuid)
	@children << uuid
end

#add_dir(path, gpath = nil) ⇒ Object

Raises:



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/xcodeproject/pbx_group.rb', line 68

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

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

	chs = path.entries.select {|obj| obj.to_s =~ /\A\./ ? 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:



141
142
143
144
145
146
147
148
149
150
151
# File 'lib/xcodeproject/pbx_group.rb', line 141

def add_file_ref (path)
	raise FilePathError.new("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



64
65
66
# File 'lib/xcodeproject/pbx_group.rb', line 64

def add_group (gpath)
	create_group(gpath)
end

#child(gpath) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/xcodeproject/pbx_group.rb', line 24

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.new("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



12
13
14
# File 'lib/xcodeproject/pbx_group.rb', line 12

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

#create_group(gpath, path = nil) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/xcodeproject/pbx_group.rb', line 82

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)


55
56
57
# File 'lib/xcodeproject/pbx_group.rb', line 55

def dir?
	not group?
end

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



46
47
48
49
# File 'lib/xcodeproject/pbx_group.rb', line 46

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

#filesObject



20
21
22
# File 'lib/xcodeproject/pbx_group.rb', line 20

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

#group(gpath) ⇒ Object



59
60
61
62
# File 'lib/xcodeproject/pbx_group.rb', line 59

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

#group?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/xcodeproject/pbx_group.rb', line 51

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

#groupsObject



16
17
18
# File 'lib/xcodeproject/pbx_group.rb', line 16

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

#relative_path(path) ⇒ Object



120
121
122
123
# File 'lib/xcodeproject/pbx_group.rb', line 120

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

#remove!Object



130
131
132
133
134
135
136
137
138
139
# File 'lib/xcodeproject/pbx_group.rb', line 130

def remove!
	return if parent.nil?

	children.each do |obj|
		obj.remove!
	end

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

#remove_child_uuid(uuid) ⇒ Object



111
112
113
# File 'lib/xcodeproject/pbx_group.rb', line 111

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

#remove_group(gpath) ⇒ Object



125
126
127
128
# File 'lib/xcodeproject/pbx_group.rb', line 125

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