Module: ZergXcode::Archiver

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

Defined Under Namespace

Classes: IdGenerator

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.


9
10
11
12
# File 'lib/zerg_xcode/file_format/archiver.rb', line 9

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

.archive_to_hash(root_object) ⇒ Object


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/zerg_xcode/file_format/archiver.rb', line 35

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.


3
4
5
6
# File 'lib/zerg_xcode/file_format/archiver.rb', line 3

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.


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/zerg_xcode/file_format/archiver.rb', line 15

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