Class: ZergXcode::Plugins::Addlibrary

Inherits:
Object
  • Object
show all
Includes:
Objects
Defined in:
lib/zerg_xcode/plugins/addlibrary.rb

Instance Method Summary collapse

Instance Method Details

#add_library!(library_target, target, project) ⇒ Object

Adds a library to a project.


41
42
43
44
45
46
# File 'lib/zerg_xcode/plugins/addlibrary.rb', line 41

def add_library!(library_target, target, project)
  add_target_dependency! library_target, target, project
  add_target_to_build_phases! library_target, target
  
  add_linker_option! '-ObjC', target if has_objc_files? target
end

#add_linker_option!(option, target) ⇒ Object

Adds a linker option to the given project.


97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/zerg_xcode/plugins/addlibrary.rb', line 97

def add_linker_option!(option, target)
  key = 'OTHER_LDFLAGS' 
  target['buildConfigurationList']['buildConfigurations'].each do |config|
    config['buildSettings'] ||= {}
    settings = config['buildSettings']
    if settings[key]
      settings[key] = [settings[key]] if settings[key].kind_of? String
      settings[key] << option unless settings[key].include? option
    else
      settings[key] = option
    end
  end
end

#add_target_dependency!(dependency, dependent, project) ⇒ Object

Adds a target as a dependency of another target.


49
50
51
52
# File 'lib/zerg_xcode/plugins/addlibrary.rb', line 49

def add_target_dependency!(dependency, dependent, project)
  return if has_target_dependency? dependency, dependent
  dependent['dependencies'] << PBXTargetDependency.for(dependency, project)
end

#add_target_to_build_phases!(dependency, dependent) ⇒ Object

Adds a target to the build phases of another target.


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/zerg_xcode/plugins/addlibrary.rb', line 62

def add_target_to_build_phases!(dependency, dependent)
  return if has_target_in_build_phases? dependency, dependent
  
  # find or create the frameworks build phase
  frameworks_phase = dependent['buildPhases'].find do |phase|
    phase['isa'] == 'PBXFrameworksBuildPhase'
  end
  unless frameworks_phase
    frameworks_phase = PBXBuildPhase.new_phase 'PBXFrameworksBuildPhase'
    target['buildPhases'] << frameworks_phase
  end
  
  dep_file = dependency['productReference']
  frameworks_phase['files'] << PBXBuildFile.for(dep_file)
end

#find_target(name, project) ⇒ Object


36
37
38
# File 'lib/zerg_xcode/plugins/addlibrary.rb', line 36

def find_target(name, project)
  project['targets'].find { |target| target['name'] == name }
end

#has_linker_option?(option, target) ⇒ Boolean

True if a target has the given linker option.

Returns:

  • (Boolean)

112
113
114
115
116
117
118
119
120
# File 'lib/zerg_xcode/plugins/addlibrary.rb', line 112

def has_linker_option?(option, target)
  key = 'OTHER_LDFLAGS'
  target['buildConfigurationList']['buildConfigurations'].all? do |config|
    next false unless settings = config['buildSettings']
    setting = settings[key]
    setting && (setting == option ||
        (setting.kind_of?(Enumerable) && setting.include?(option)))
  end
end

#has_objc_files?(target) ⇒ Boolean

True if the target builds any objective C files.

Returns:

  • (Boolean)

90
91
92
93
94
# File 'lib/zerg_xcode/plugins/addlibrary.rb', line 90

def has_objc_files?(target)
  target.all_files.any? do |file|
    file[:build_object].file_type == 'sourcecode.c.objc'
  end
end

#has_target_dependency?(dependency, target) ⇒ Boolean

True if the given target is dependent on the other given target.

Returns:

  • (Boolean)

55
56
57
58
59
# File 'lib/zerg_xcode/plugins/addlibrary.rb', line 55

def has_target_dependency?(dependency, target)
  target['dependencies'].any? do |dep|
    dep.kind_of?(PBXTargetDependency) && dep.target == dependency 
  end
end

#has_target_in_build_phases?(dependency, dependent) ⇒ Boolean

Returns:

  • (Boolean)

78
79
80
81
82
83
84
85
86
87
# File 'lib/zerg_xcode/plugins/addlibrary.rb', line 78

def has_target_in_build_phases?(dependency, dependent)
  frameworks_phase = dependent['buildPhases'].find do |phase|
    phase['isa'] == 'PBXFrameworksBuildPhase'
  end
  return false unless frameworks_phase
  
  frameworks_phase['files'].any? do |file|
    file['fileRef'] == dependency['productReference']
  end
end

#helpObject


4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/zerg_xcode/plugins/addlibrary.rb', line 4

def help
  {:short => 'adds a library to a target',
   :long => <<"END" }
Usage: addlibrary library_target target [path]

Adds a library as a dependency to a target, in the project at the given path.
If no path is given, looks for a project in the current directory.

library_target should be a static library. If library_target contains ObjectiveC
files, the "-ObjC" option is added to the target linker options.
END
end

#run(args) ⇒ Object


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/zerg_xcode/plugins/addlibrary.rb', line 17

def run(args)
  lib_target_name = args.shift
  target_name = args.shift
  project = ZergXcode.load(args.shift || '.')

  ad = "'zerg-xcode lstargets' can list project targets."
  unless lib_target = find_target(lib_target_name, project)
    print "#{lib_target_name} not found. #{ad}"
    return
  end
  unless target = find_target(target_name, project)      
    print "#{target_name} not found. #{ad}"
    return
  end

  add_library! lib_target, target, project
  project.save!
end