Class: ZergXcode::Plugins::Addlibrary

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

Overview

Adds a library target as a dependence to another Xcode target.

Instance Method Summary collapse

Instance Method Details

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

Adds a library to a project.



52
53
54
55
56
57
# File 'lib/zerg_xcode/plugins/addlibrary.rb', line 52

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.



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

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.



60
61
62
63
# File 'lib/zerg_xcode/plugins/addlibrary.rb', line 60

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.



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

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



47
48
49
# File 'lib/zerg_xcode/plugins/addlibrary.rb', line 47

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)


123
124
125
126
127
128
129
130
131
# File 'lib/zerg_xcode/plugins/addlibrary.rb', line 123

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)


101
102
103
104
105
# File 'lib/zerg_xcode/plugins/addlibrary.rb', line 101

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)


66
67
68
69
70
# File 'lib/zerg_xcode/plugins/addlibrary.rb', line 66

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)


89
90
91
92
93
94
95
96
97
98
# File 'lib/zerg_xcode/plugins/addlibrary.rb', line 89

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



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/zerg_xcode/plugins/addlibrary.rb', line 15

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



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/zerg_xcode/plugins/addlibrary.rb', line 28

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