Module: Xcode::Registry

Defined in:
lib/xcode/registry.rb

Overview

The Registry represents the parsed data from the Xcode Project file. The registry is a Hash that provides additional functionality to allow the the ability to query, add, and remove resources from the object hash.

Opening the Xcode project file in a text-editor you’ll notice that it is a big hash/plist. The registry represents this structure and provides access to the top-level items: ‘rootObject’; ‘objectVersion’; ‘archiveVersion’ and ‘objects’. The registry provides a number of methods to access these items.

The most important key is the ‘objects’ dictionary which maintains the master-list of Identifiers to properties. The registry provides additional methods get, add, remove, and update the objects stored within it. The registry returns Resource objects which is a wrapper class around the properties hash that would normally be returned. Providing additional functionality to make it easier to traverse the project.

See Also:

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.is_identifier?(value) ⇒ TrueClass, FalseClass

This method is used internally to determine if the value that is being retrieved is an identifier.

Parameters:

  • value (String)

    is the specified value in the form of an identifier

Returns:

  • (TrueClass, FalseClass)

    true if the value specified is a valid identifier; false if it is not.

See Also:



50
51
52
# File 'lib/xcode/registry.rb', line 50

def self.is_identifier? value
  value =~ /^[0-9a-fA-F]{24,}$/
end

.isa_to_module(isa) ⇒ Array<Module>

All object parameters contain an ‘isa` property, which represents the class within Xcode. Here the `isa` string value is translated into a Ruby module.

Initially the ‘isa` values were mapped directly to Ruby modules but that was changed to this model to make it easy to repair if Xcode were to change their `isa` values and also provide the ability to mixin different functionality as needed.

Parameters:

  • isa (String)

    the type of the object.

Returns:

  • (Array<Module>)

    an array of modules that are mapped to the string name.

See Also:



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/xcode/registry.rb', line 69

def self.isa_to_module isa

  modules = { 'PBXProject' => [ProjectReference, ConfigurationOwner],
    'XCBuildConfiguration' => Configuration,
    'PBXFileReference' => FileReference,
    'PBXGroup' => Group,
    'PBXNativeTarget' => [Target, ConfigurationOwner ],
    'PBXLegacyTarget' => [Target, ConfigurationOwner ],
    'PBXAggregateTarget' => [ Target, ConfigurationOwner ],
    'PBXFrameworksBuildPhase' => BuildPhase,
    'PBXSourcesBuildPhase' => BuildPhase,
    'PBXResourcesBuildPhase' => BuildPhase,
    'PBXHeadersBuildPhase' => BuildPhase,
    'PBXShellScriptBuildPhase' => BuildPhase,
    'PBXTargetDependency' => TargetDependency,
    'PBXContainerItemProxy' => ContainerItemProxy,
    'PBXBuildFile' => BuildFile,
    'PBXVariantGroup' => VariantGroup,
    'XCConfigurationList' => ConfigurationList,
    'PBXVariantGroup' => VariantGroup }[isa]
    
    Array(modules)
end

Instance Method Details

#add_object(object_properties) ⇒ Object

Note:

generally this method should not be called directly and instead resources should provide the ability to assist with generating the correct objects for the registry.

Provides a method to generically add objects to the registry. This will create a unqiue identifier and add the specified parameters to the registry. As all objecst within a the project maintain a reference to this registry they can immediately query for newly created items.

Parameters:

  • object_properties (Hash)

    a hash that contains all the properties that are known for the particular item.



167
168
169
170
171
172
173
# File 'lib/xcode/registry.rb', line 167

def add_object(object_properties)
  new_identifier = SimpleIdentifierGenerator.generate :existing_keys => objects.keys
  
  objects[new_identifier] = object_properties
  
  Resource.new new_identifier, self
end

#archive_versionFixnum

Returns the archive version.

Returns:

  • (Fixnum)

    the archive version



125
126
127
# File 'lib/xcode/registry.rb', line 125

def archive_version
  self['archiveVersion'].to_i
end

#object(identifier) ⇒ Resource

Retrieve a Resource for the given identifier.

Parameters:

  • identifier (String)

    the unique identifier for the resource you are attempting to find.

Returns:

  • (Resource)

    the Resource object the the data properties that would be stored wihin it.



137
138
139
# File 'lib/xcode/registry.rb', line 137

def object(identifier)
  Resource.new identifier, self
end

#object_versionFixnum

Returns the object version.

Returns:

  • (Fixnum)

    the object version



118
119
120
# File 'lib/xcode/registry.rb', line 118

def object_version
  self['objectVersion'].to_i
end

#objectsHash

This is a hash of all the objects within the project. The keys are the unique identifiers which are 24 length hexadecimal strings. The values are the objects that the keys represent.

Returns:

  • (Hash)

    that contains all the objects in the project.



111
112
113
# File 'lib/xcode/registry.rb', line 111

def objects
  self['objects']
end

#properties(identifier) ⇒ Hash

Retrieve the properties Hash for the given identifer.

Parameters:

  • identifier (String)

    the unique identifier for the resource you are attempting to find.

Returns:

  • (Hash)

    the raw, properties hash for the particular resource; nil if nothing matches the identifier.



150
151
152
# File 'lib/xcode/registry.rb', line 150

def properties(identifier)
  objects[identifier]
end

#remove_object(identifier) ⇒ Object

Note:

removing an item from the regitry does not remove all references to the item within the project. At this time, this could leave resources with references to resources that are invalid.

Parameters:

  • identifier (String)

    of the object to remove from the registry.



197
198
199
# File 'lib/xcode/registry.rb', line 197

def remove_object(identifier)
  objects.delete identifier
end

#rootResource

This is the root object of the project. This is generally an identifier pointing to a project.

Returns:

  • (Resource)

    this is traditionally the root, project object.



99
100
101
# File 'lib/xcode/registry.rb', line 99

def root
  self['rootObject']
end

#set_object(resource) ⇒ Object

Replace an existing object that shares that same identifier. This is how a Resource is saved back into the registry. So that it will be known to all other objects that it has changed.

Parameters:

  • resource (Resource)

    the resource that you want to set at the specified identifier. If an object exists at that identifier already it will be replaced.

See Also:



186
187
188
# File 'lib/xcode/registry.rb', line 186

def set_object(resource)
  objects[resource.identifier] = resource.properties
end