Module: ZergXcode::Archiver

Defined in:
lib/zerg_xcode/file_format/archiver.rb

Overview

Flattens and restores PBX (Xcode project) object graphs.

Class Method Summary collapse

Class Method Details

.archive(project) ⇒ Object

Archives an Xcode object graph to a string to be written to a .pbxproj file.



20
21
22
23
# File 'lib/zerg_xcode/file_format/archiver.rb', line 20

def self.archive(project)
  proj_hash = archive_to_hash project
  ZergXcode::Encoder.encode proj_hash
end

.archive_to_hash(root_object) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/zerg_xcode/file_format/archiver.rb', line 46

def self.archive_to_hash(root_object)
  archived = ZergXcode::XcodeObject.from root_object
  id_generator = IdGenerator.new
  visited = Set.new([archived])
  
  archived.visit do |object, parent, key, value|
    next true unless value.kind_of? ZergXcode::XcodeObject
    
    if visited.include? value
      parent[key] = value.archive_id
      next false
    else
      visited << value
      value.archive_id = id_generator.id_for(value)
      parent[key] = value.archive_id
      next true
    end      
  end
  root_object_id = archived.archive_id

  objects = {}
  visited.each { |object| objects[object.archive_id] = object._attr_hash }
  return { 'archiveVersion' => '1',
           'objectVersion' => root_object.version.to_s,
           'classes' => Hash.new, 'rootObject' => root_object_id,
           'objects' => objects }
end

.unarchive(string) ⇒ Object

Unarchives an Xcode object graph from the contents of a file.



14
15
16
17
# File 'lib/zerg_xcode/file_format/archiver.rb', line 14

def self.unarchive(string)
  proj_hash = ZergXcode::Parser.parse string
  unarchive_hash proj_hash
end

.unarchive_hash(hash) ⇒ Object

Unarchives an Xcode object graph serialized to a hash.



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

def self.unarchive_hash(hash)
  raise 'Uknown archive version' unless hash['archiveVersion'] == '1'
  raise 'Classes not implemented' unless hash['classes'] = {}
  version = hash['objectVersion'].to_i
  
  objects = {}
  hash['objects'].each do |archive_id, object_hash|
    object = ZergXcode::XcodeObject.from object_hash
    object.version, object.archive_id = version, archive_id
    objects[archive_id] = object
  end
  objects.each do |object_id, object|
    object.visit do |object, parent, key, value|
      parent[key] = objects[value] if objects.has_key? value
      next true
    end
  end
  return objects[hash['rootObject']]
end