Module: Xcode::Group

Included in:
VariantGroup, VariantGroup
Defined in:
lib/xcode/group.rb

Overview

Within the project file there are logical separation of resources into groups these groups may contain subgroups, files, or other objects. They have children.

Group here provides the methods to traverse the groups to find these children resources as well as provide the ability to generate child resources.

7165D451146B4EA100DE2F0E /* Products */ = {
  isa = PBXGroup;
  children = (
    7165D450146B4EA100DE2F0E /* TestProject.app */,
    7165D46B146B4EA100DE2F0E /* TestProjectTests.octest */,
    E21EB9D614E357CF0058122A /* Specs.app */,
  );
  name = Products;
  sourceTree = "<group>";
};

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#supergroupObject

Note:

this value is only set if the group has been discovered by traversing groups to this group.

This is the group for which this file is contained within.



42
43
44
# File 'lib/xcode/group.rb', line 42

def supergroup
  @supergroup
end

Class Method Details

.logical_group(name) ⇒ Hash

Return the hash that maps to the properties for a logical group

Parameters:

  • name (String)

    of the logical group

Returns:

  • (Hash)

    the properties for a Group



31
32
33
34
35
36
# File 'lib/xcode/group.rb', line 31

def self.logical_group(name)
  { 'isa' => 'PBXGroup', 
    'name' => name,
    'sourceTree' => '<group>',
    'children' => [] }
end

Instance Method Details

#create_file(file_properties) ⇒ FileReference

Add a file to the specified group. Currently the file creation requires the path to the physical file.

Examples:

creating a file with just a path


project.main_group.create_file 'AppDelegate.m'

creating a file with a name and path


project.main_group.create_file 'name' => 'AppDelegate.m', 'path' => 'Subfolder/AppDelegate.m'

Parameters:

  • path (String, Hash)

    to the file that is being added or a hash that contains the values would be merged with the default values.

Returns:



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/xcode/group.rb', line 140

def create_file(file_properties)
  # This allows both support for the string value or the hash as the parameter
  file_properties = { 'path' => file_properties } if file_properties.is_a? String
  
  # IF the file already exists then we will not create the file with the
  # parameters that are being supplied, instead we will return what we
  # found.
  
  find_file_by = file_properties['name'] || file_properties['path']
  
  found_or_created_file = exists?(find_file_by).first
  
  unless found_or_created_file
    found_or_created_file = create_child_object FileReference.file(file_properties)
  end
  
  found_or_created_file.supergroup = self
  
  found_or_created_file
end

#create_framework(framework_properties) ⇒ FileReference

Create a framework within this group.

Examples:

Custom.Framework


project.frameworks_group.create_framework 'name' => 'Custom.framework', 
  'path' => 'Vendor/Custom/Custom.framework' 

Parameters:

  • framework_properties (Hash)

    the properties to merge with the default properties.

Returns:



174
175
176
# File 'lib/xcode/group.rb', line 174

def create_framework(framework_properties)
  find_or_create_child_object FileReference.framework(framework_properties)
end

#create_group(name) ⇒ Group

Note:

A group may be added that has the same name as another group as they are distinguished by a unique identifier and not by name.

Adds a group as a child to current group with the given name.

Parameters:

  • name (String)

    of the group that you want to add as a child group of the specified group.

Returns:

  • (Group)

    the created group



117
118
119
120
121
# File 'lib/xcode/group.rb', line 117

def create_group(name)
  new_group = create_child_object Group.logical_group(name)
  new_group.supergroup = self
  new_group
end

#create_infoplist(infoplist_properties) ⇒ VariantGroup

Create an infoplist within this group.

Parameters:

  • infoplist_properties (Hash)

    the properties to merge with the default properties.

Returns:

See Also:



216
217
218
# File 'lib/xcode/group.rb', line 216

def create_infoplist(infoplist_properties)
  create_child_object VariantGroup.info_plist(infoplist_properties)
end

#create_product_reference(name) ⇒ FileReference

Note:

this is usually performed through the target as it is necessary within the target to specify what is the product reference.

Create a product reference witin this group.

Parameters:

  • name (String)

    the name of the product to generate

Returns:

See Also:



231
232
233
# File 'lib/xcode/group.rb', line 231

def create_product_reference(name)
  create_child_object FileReference.app_product(name)
end

#create_system_framework(name) ⇒ FileReference

Create a system framework reference within this group

Examples:

creating ‘CoreGraphics’ and ‘Foundation’ frameworks


project.frameworks_group.create_system_framework "CoreGraphics.framework"
project.frameworks_group.create_system_framework "Foundation"

Parameters:

  • name (String)

    the name of the System framework to add to this group.

Returns:



189
190
191
# File 'lib/xcode/group.rb', line 189

def create_system_framework(name)
  find_or_create_child_object FileReference.system_framework(name)
end

#create_system_library(name) ⇒ FileReference

Create a system library reference within this group

@example libz.dylib

   project.frameworks_group.create_system_library "libz.dylib"

Parameters:

  • name (String)

    the name of the System Library to add to this group.

Returns:



203
204
205
# File 'lib/xcode/group.rb', line 203

def create_system_library(name)
  find_or_create_child_object FileReference.system_library(name)
end

#exists?(name) ⇒ Array<Resource>

Check whether a file or group is contained within this group that has a name that matches the one specified

Parameters:

  • name (String)

    of the group attempting to be found.

Returns:

  • (Array<Resource>)

    resource with the name that matches; empty array if no matches were found.



100
101
102
103
104
# File 'lib/xcode/group.rb', line 100

def exists?(name)
  children.find_all do |child| 
    child.name ? (child.name == name) : (File.basename(child.path) == name)
  end
end

#file(name) ⇒ Array<FileReference>

Find all the files that have have a name that matches the specified name.

Parameters:

  • name (String)

    of the file that you are looking to return.

Returns:

  • (Array<FileReference>)

    the files with the same mathching name. This could be no files, one file, or multiple files.



87
88
89
# File 'lib/xcode/group.rb', line 87

def file(name)
  files.find_all {|file| file.name == name or file.path == name }
end

#filesArray<Resource>

Find all the non-group objects within the group and return them

Returns:

  • (Array<Resource>)

    the children of the group, excluding the groups, which is usually composed of FileReferences



76
77
78
# File 'lib/xcode/group.rb', line 76

def files
  children.reject {|child| child.is_a?(Group) }
end

#group(name) ⇒ Array<Group>

Find all the child groups that have a name that matches the specified name.

Parameters:

  • name (String)

    of the group that you are looking to return.

Returns:

  • (Array<Group>)

    the groups with the same matching name. This could be no groups, one group, or multiple groups.



66
67
68
# File 'lib/xcode/group.rb', line 66

def group(name)
  groups.find_all {|group| group.name == name or group.path == name }
end

#groupsArray

Returns the sub-groups contained within this group.

Examples:

Return all the sub-groups of the main group


main_group = Xcode.project('MyProject.xcodeproj').groups
main_group.groups

Returns:

  • (Array)

    the sub-groups contained within this group.



52
53
54
55
56
57
# File 'lib/xcode/group.rb', line 52

def groups
  children.find_all {|child| child.is_a?(Group) }.map do |group|
    group.supergroup = self
    group
  end
end

#remove! {|_self| ... } ⇒ Object

Note:

all children objects of this group are removed as well.

Remove the resource from the registry.

Yields:

  • (_self)

Yield Parameters:

  • _self (Xcode::Group)

    the object that the method was called on



240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/xcode/group.rb', line 240

def remove!(&block)
  
  # @note #groups and #files is used because it adds the very precious
  #   supergroup to each of the child items.
  
  groups.each {|group| group.remove!(&block) }
  files.each {|file| file.remove!(&block) }
  yield self if block_given?
  
  child_identifier = identifier
  supergroup.instance_eval { remove_child_object(child_identifier) }
  @registry.remove_object identifier
end