Class: Ovaltine::XcodeProject

Inherits:
Object
  • Object
show all
Defined in:
lib/ovaltine/xcode_project.rb,
lib/ovaltine/xcode_project/pbxgroup.rb,
lib/ovaltine/xcode_project/pbxobject.rb,
lib/ovaltine/xcode_project/pbxfilereference.rb

Defined Under Namespace

Classes: PBXContainerItemProxy, PBXFileReference, PBXFrameworksBuildPhase, PBXGroup, PBXNativeTarget, PBXObject, PBXProject, PBXReferenceProxy, PBXResourcesBuildPhase, PBXSourcesBuildPhase, XCBuildConfiguration

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ XcodeProject

Returns a new instance of XcodeProject.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ovaltine/xcode_project.rb', line 19

def initialize path
  load_dependencies
  if path =~ /\.xcodeproj$/
    @path = File.join(path, 'project.pbxproj')
  else
    @path = path
  end
  @json = JSON.parse(`plutil -convert json -o - "#{@path}"`)

  @json["objects"].each do |uuid, hash|
    klass = PBXObject
    begin
      klass = Ovaltine::XcodeProject.const_get "#{hash['isa']}"
    rescue
    end

    obj = klass.new uuid, hash
    obj.project_file = self

    @json["objects"][uuid] = obj
  end
end

Instance Method Details

#add_file_ref(path) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/ovaltine/xcode_project.rb', line 71

def add_file_ref path
  relpath = File.expand_path(path).gsub(File.dirname(File.dirname(File.expand_path(@path))), '')[1..-1]
  relpath = relpath[(relpath.index('/') + 1)..-1]
  return nil if files.detect {|ref| ref["path"] == relpath}
  ref = PBXFileReference.create(relpath, file_type(path))
  add_object(ref)
  main_group = groups.detect do |g|
    !g["name"] && g["path"] && !g["path"].include?("Test")
  end
  unless group = groups.detect {|g| g["name"] == "Generated Files"}
    group = PBXGroup.create("Generated Files")
    add_object(group)
    main_group.add_object(group)
  end
  group.add_object(ref)
  ref
end

#add_object(obj) ⇒ Object



89
90
91
92
# File 'lib/ovaltine/xcode_project.rb', line 89

def add_object obj
  obj.project_file = self
  @json["objects"][obj.uuid] = obj
end

#file_type(path) ⇒ Object



94
95
96
97
98
99
100
101
102
# File 'lib/ovaltine/xcode_project.rb', line 94

def file_type path
  file_type = 'sourcecode'
  if path =~ /\.m$/
    file_type = 'sourcecode.c.objc'
  elsif path =~ /\.h$/
    file_type = 'sourcecode.c.h'
  end
  file_type
end

#filesObject



54
55
56
# File 'lib/ovaltine/xcode_project.rb', line 54

def files
  objects_of_class(PBXFileReference)
end

#groupsObject



50
51
52
# File 'lib/ovaltine/xcode_project.rb', line 50

def groups
  objects_of_class(PBXGroup)
end

#load_dependenciesObject



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/ovaltine/xcode_project.rb', line 5

def load_dependencies
  unless @@loaded ||= false
    unless Object.const_defined?(:JSON)
      begin
        require 'json'
      rescue LoadError
        require File.expand_path(File.join(File.dirname(__FILE__),'../../vendor/json_pure/parser'))
        require File.expand_path(File.join(File.dirname(__FILE__),'../../vendor/json_pure/generator'))
      end
    end
    @@loaded = true
  end
end

#object_with_uuid(uuid) ⇒ Object



67
68
69
# File 'lib/ovaltine/xcode_project.rb', line 67

def object_with_uuid uuid
  @json["objects"][uuid]
end

#objectsObject



46
47
48
# File 'lib/ovaltine/xcode_project.rb', line 46

def objects
  @json["objects"].values
end

#objects_of_class(klass) ⇒ Object



58
59
60
61
# File 'lib/ovaltine/xcode_project.rb', line 58

def objects_of_class klass
  str = klass.to_s.split('::').last
  objects.select {|obj| obj["isa"] == str }
end

#objects_with_uuids(uuids) ⇒ Object



63
64
65
# File 'lib/ovaltine/xcode_project.rb', line 63

def objects_with_uuids uuids
  uuids.map {|uuid| self.object_with_uuid uuid }
end

#saveObject



42
43
44
# File 'lib/ovaltine/xcode_project.rb', line 42

def save
  File.open(@path, "w") { |f| f.write @json.to_plist }
end