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.



29
30
31
32
# File 'lib/xcodeproject/pbx_group.rb', line 29

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(self.create_object_hash(name, path))
	PBXGroup.new(root, uuid, data)
end

Instance Method Details

#absolute_path(path) ⇒ Object



137
138
139
140
# File 'lib/xcodeproject/pbx_group.rb', line 137

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

#add_child_uuid(uuid) ⇒ Object



129
130
131
# File 'lib/xcodeproject/pbx_group.rb', line 129

def add_child_uuid (uuid)
	@children << uuid
end

#add_dir(path, gpath = nil) ⇒ Object

Raises:



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

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:



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.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



86
87
88
# File 'lib/xcodeproject/pbx_group.rb', line 86

def add_group (gpath)
	create_group(gpath)
end

#child(gpath) ⇒ Object



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

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



34
35
36
# File 'lib/xcodeproject/pbx_group.rb', line 34

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

#create_group(gpath, path = nil) ⇒ Object



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

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)


77
78
79
# File 'lib/xcodeproject/pbx_group.rb', line 77

def dir?
	not group?
end

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



68
69
70
71
# File 'lib/xcodeproject/pbx_group.rb', line 68

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

#filesObject



42
43
44
# File 'lib/xcodeproject/pbx_group.rb', line 42

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

#group(gpath) ⇒ Object



81
82
83
84
# File 'lib/xcodeproject/pbx_group.rb', line 81

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

#group?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/xcodeproject/pbx_group.rb', line 73

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

#groupsObject



38
39
40
# File 'lib/xcodeproject/pbx_group.rb', line 38

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

#relative_path(path) ⇒ Object



142
143
144
145
# File 'lib/xcodeproject/pbx_group.rb', line 142

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

#remove!Object



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

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



133
134
135
# File 'lib/xcodeproject/pbx_group.rb', line 133

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

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



147
148
149
150
# File 'lib/xcodeproject/pbx_group.rb', line 147

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

#remove_group(gpath) ⇒ Object



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

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