-
Notifications
You must be signed in to change notification settings - Fork 346
/
podfile.rb
371 lines (320 loc) · 10.2 KB
/
podfile.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
require 'cocoapods-core/podfile/dsl'
require 'cocoapods-core/podfile/target_definition'
module Pod
# The Podfile is a specification that describes the dependencies of the
# targets of an Xcode project.
#
# It supports its own DSL and generally is stored in files named
# `CocoaPods.podfile` or `Podfile`.
#
# The Podfile creates a hierarchy of target definitions that that store the
# information of necessary to generate the CocoaPods libraries.
#
class Podfile
# @!group DSL support
include Pod::Podfile::DSL
#-------------------------------------------------------------------------#
class StandardError < ::StandardError; end
#-------------------------------------------------------------------------#
# @return [Pathname] the path where the podfile was loaded from. It is nil
# if the podfile was generated programmatically.
#
attr_accessor :defined_in_file
# @param [Pathname] defined_in_file
# the path of the podfile.
#
# @param [Proc] block
# an optional block that configures the podfile through the DSL.
#
# @example Creating a Podfile.
#
# platform :ios, "6.0"
# target :my_app do
# pod "AFNetworking", "~> 1.0"
# end
#
def initialize(defined_in_file = nil, internal_hash = {}, &block)
self.defined_in_file = defined_in_file
@internal_hash = internal_hash
if block
default_target_def = TargetDefinition.new('Pods', self)
default_target_def.link_with_first_target = true
@root_target_definitions = [default_target_def]
@current_target_definition = default_target_def
instance_eval(&block)
else
@root_target_definitions = []
end
end
# @return [String] a string useful to represent the Podfile in a message
# presented to the user.
#
def to_s
'Podfile'
end
#-------------------------------------------------------------------------#
public
# @!group Working with a podfile
# @return [Hash{Symbol,String => TargetDefinition}] the target definitions
# of the podfile stored by their name.
#
def target_definitions
Hash[target_definition_list.map { |td| [td.name, td] }]
end
def target_definition_list
root_target_definitions.map { |td| [td, td.recursive_children] }.flatten
end
# @return [Array<TargetDefinition>] The root target definition.
#
attr_accessor :root_target_definitions
# @return [Array<Dependency>] the dependencies of the all the target
# definitions.
#
def dependencies
target_definition_list.map(&:dependencies).flatten.uniq
end
#-------------------------------------------------------------------------#
public
# @!group Attributes
# @return [Array<String>] The name of the sources.
#
def sources
get_hash_value('sources') || []
end
# @return [Hash<String, Hash>] The plugins, keyed by name.
#
def plugins
get_hash_value('plugins') || {}
end
# @return [String] the path of the workspace if specified by the user.
#
def workspace_path
path = get_hash_value('workspace')
if path
if File.extname(path) == '.xcworkspace'
path
else
"#{path}.xcworkspace"
end
end
end
# @return [Bool] whether the podfile should generate a BridgeSupport
# metadata document.
#
def generate_bridge_support?
get_hash_value('generate_bridge_support')
end
# @return [Bool] whether the -fobjc-arc flag should be added to the
# OTHER_LD_FLAGS.
#
def set_arc_compatibility_flag?
get_hash_value('set_arc_compatibility_flag')
end
#-------------------------------------------------------------------------#
public
# @!group Hooks
# Calls the pre install callback if defined.
#
# @param [Pod::Installer] installer
# the installer that is performing the installation.
#
# @return [Bool] whether a pre install callback was specified and it was
# called.
#
def pre_install!(installer)
if @pre_install_callback
@pre_install_callback.call(installer)
true
else
false
end
end
# Calls the post install callback if defined.
#
# @param [Pod::Installer] installer
# the installer that is performing the installation.
#
# @return [Bool] whether a post install callback was specified and it was
# called.
#
def post_install!(installer)
if @post_install_callback
@post_install_callback.call(installer)
true
else
false
end
end
#-------------------------------------------------------------------------#
public
# @!group Representations
# @return [Array] The keys used by the hash representation of the Podfile.
#
HASH_KEYS = %w(
target_definitions
workspace
sources
generate_bridge_support
set_arc_compatibility_flag
plugins
).freeze
# @return [Hash] The hash representation of the Podfile.
#
def to_hash
hash = {}
hash['target_definitions'] = root_target_definitions.map(&:to_hash)
hash.merge!(internal_hash)
hash
end
# @return [String] The YAML representation of the Podfile.
#
def to_yaml
to_hash.to_yaml
end
# @!group Class methods
#-------------------------------------------------------------------------#
# Initializes a podfile from the file with the given path.
#
# @param [Pathname] path
# the path from where the podfile should be loaded.
#
# @return [Podfile] the generated podfile.
#
def self.from_file(path)
path = Pathname.new(path)
unless path.exist?
raise Informative, "No Podfile exists at path `#{path}`."
end
case path.extname
when '', '.podfile'
Podfile.from_ruby(path)
when '.yaml'
Podfile.from_yaml(path)
else
raise Informative, "Unsupported Podfile format `#{path}`."
end
end
# Configures a new Podfile from the given ruby string.
#
# @param [String] string
# The ruby string which will configure the podfile with the DSL.
#
# @param [Pathname] path
# The path from which the Podfile is loaded.
#
# @return [Podfile] the new Podfile
#
def self.from_ruby(path)
string = File.open(path, 'r:utf-8') { |f| f.read }
# Work around for Rubinius incomplete encoding in 1.9 mode
if string.respond_to?(:encoding) && string.encoding.name != 'UTF-8'
string.encode!('UTF-8')
end
podfile = Podfile.new(path) do
begin
# rubocop:disable Eval
eval(string, nil, path.to_s)
# rubocop:enable Eval
rescue => e
message = "Invalid `#{path.basename}` file: #{e.message}"
raise DSLError.new(message, path, e.backtrace)
end
end
podfile
end
# Configures a new Podfile from the given YAML representation.
#
# @param [String] yaml
# The YAML encoded hash which contains the information of the
# Podfile.
#
# @param [Pathname] path
# The path from which the Podfile is loaded.
#
# @return [Podfile] the new Podfile
#
def self.from_yaml(path)
string = File.open(path, 'r:utf-8') { |f| f.read }
# Work around for Rubinius incomplete encoding in 1.9 mode
if string.respond_to?(:encoding) && string.encoding.name != 'UTF-8'
string.encode!('UTF-8')
end
hash = YAMLHelper.load_string(string)
from_hash(hash, path)
end
# Configures a new Podfile from the given hash.
#
# @param [Hash] hash
# The hash which contains the information of the Podfile.
#
# @param [Pathname] path
# The path from which the Podfile is loaded.
#
# @return [Podfile] the new Podfile
#
def self.from_hash(hash, path = nil)
internal_hash = hash.dup
target_definitions = internal_hash.delete('target_definitions') || []
podfile = Podfile.new(path, internal_hash)
target_definitions.each do |definition_hash|
definition = TargetDefinition.from_hash(definition_hash, podfile)
podfile.root_target_definitions << definition
end
podfile
end
#-------------------------------------------------------------------------#
private
# @!group Private helpers
# @return [Hash] The hash which store the attributes of the Podfile.
#
attr_accessor :internal_hash
# Set a value in the internal hash of the Podfile for the given key.
#
# @param [String] key
# The key for which to store the value.
#
# @param [Object] value
# The value to store.
#
# @raise If the key is not recognized.
#
# @return [void]
#
def set_hash_value(key, value)
unless HASH_KEYS.include?(key)
raise StandardError, "Unsupported hash key `#{key}`"
end
internal_hash[key] = value
end
# Returns the value for the given key in the internal hash of the Podfile.
#
# @param [String] key
# The key for which the value is needed.
#
# @raise If the key is not recognized.
#
# @return [Object] The value for the key.
#
def get_hash_value(key)
unless HASH_KEYS.include?(key)
raise StandardError, "Unsupported hash key `#{key}`"
end
internal_hash[key]
end
# @return [TargetDefinition] The current target definition to which the DSL
# commands apply.
#
attr_accessor :current_target_definition
public
# @!group Deprecations
#-------------------------------------------------------------------------#
# @deprecated Deprecated in favour of the more succinct {#pod}. Remove for
# CocoaPods 1.0.
#
def dependency(name = nil, *requirements, &block)
CoreUI.warn "[DEPRECATED] `dependency' is deprecated (use `pod')"
pod(name, *requirements, &block)
end
#-------------------------------------------------------------------------#
end
end