-
Notifications
You must be signed in to change notification settings - Fork 349
/
podfile.rb
436 lines (380 loc) · 12.3 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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
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 is stored in a file named `Podfile`.
#
# The Podfile creates a hierarchy of target definitions that store the
# information necessary to generate the CocoaPods libraries.
#
class Podfile
# @!group DSL support
include Pod::Podfile::DSL
#-------------------------------------------------------------------------#
class StandardError < ::StandardError; end
#-------------------------------------------------------------------------#
# @return [Pathname] the path used to load the Podfile. 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.abstract = 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
# @return [Array<TargetDefinition>] all target definitions in the Podfile.
#
def target_definition_list
root_target_definitions.map { |td| [td, td.recursive_children] }.flatten
end
# @return [Array<TargetDefinition>] The root target definitions.
#
attr_accessor :root_target_definitions
# @return [Array<Dependency>] the dependencies of all of the target
# definitions.
#
def dependencies
target_definition_list.map(&:dependencies).flatten.uniq
end
#-------------------------------------------------------------------------#
public
# @!group Attributes
# @return [Array<String>] The names 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 [Boolean] whether the podfile should generate a BridgeSupport
# metadata document.
#
def generate_bridge_support?
get_hash_value('generate_bridge_support')
end
# @return [Boolean] 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
# @return [(String,Hash)] the installation strategy and installation options
# to be used during installation.
#
def installation_method
get_hash_value('installation_method', 'name' => 'cocoapods', 'options' => {}).
values_at('name', 'options')
end
#-------------------------------------------------------------------------#
public
# @!group Hooks
# Calls the pre install callback if defined.
#
# @param [Pod::Installer] installer
# the installer that is performing the installation.
#
# @return [Boolean] 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 pre integrate callback if defined.
#
# @param [Pod::Installer] installer
# the installer that is performing the installation.
#
# @return [Boolean] whether a pre integrate callback was specified and it was
# called.
#
def pre_integrate!(installer)
if @pre_integrate_callback
@pre_integrate_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 [Boolean] 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
# Calls the post integrate callback if defined.
#
# @param [Pod::Installer] installer
# the installer that is performing the installation.
#
# @return [Boolean] whether a post install callback was specified and it was
# called.
#
def post_integrate!(installer)
if @post_integrate_callback
@post_integrate_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(
installation_method
workspace
sources
plugins
set_arc_compatibility_flag
generate_bridge_support
target_definitions
).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
require 'cocoapods-core/yaml_helper'
"---\n" << YAMLHelper.convert_hash(to_hash, HASH_KEYS)
end
# @return [String] The SHA1 digest of the file in which the Podfile
# is defined.
#
# @return [Nil] If the podfile is not defined in a file.
#
def checksum
@checksum ||= begin
unless defined_in_file.nil?
require 'digest'
checksum = Digest::SHA1.hexdigest(File.read(defined_in_file))
checksum = checksum.encode('UTF-8') if checksum.respond_to?(:encode)
checksum
end
end
end
def ==(other)
self.class == other.class &&
to_hash == other.to_hash
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', '.rb'
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 [Pathname] path
# The path from which the Podfile is loaded.
#
# @param [String] contents
# The ruby string which will configure the Podfile with the DSL.
#
# @return [Podfile] the new Podfile
#
def self.from_ruby(path, contents = nil)
contents ||= File.open(path, 'r:utf-8', &:read)
# Work around for Rubinius incomplete encoding in 1.9 mode
if contents.respond_to?(:encoding) && contents.encoding.name != 'UTF-8'
contents.encode!('UTF-8')
end
if contents.tr!('“”‘’‛', %(""'''))
# Changes have been made
CoreUI.warn "Smart quotes were detected and ignored in your #{path.basename}. " \
'To avoid issues in the future, you should not use ' \
'TextEdit for editing it. If you are not using TextEdit, ' \
'you should turn off smart quotes in your editor of choice.'
end
podfile = Podfile.new(path) do
# rubocop:disable Lint/RescueException
begin
# rubocop:disable Security/Eval
eval(contents, nil, path.to_s)
# rubocop:enable Security/Eval
rescue Exception => e
message = "Invalid `#{path.basename}` file: #{e.message}"
raise DSLError.new(message, path, e, contents)
end
# rubocop:enable Lint/RescueException
end
podfile
end
# Configures a new Podfile from the given YAML representation.
#
# @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', &: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 [StandardError] 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.
#
# @param default
# The default value to return if the internal hash has no entry for
# the given `key`.
#
# @raise [StandardError] If the key is not recognized.
#
# @return [Object] The value for the key.
#
def get_hash_value(key, default = nil)
unless HASH_KEYS.include?(key)
raise StandardError, "Unsupported hash key `#{key}`"
end
internal_hash.fetch(key, default)
end
# @return [TargetDefinition] The current target definition to which the DSL
# commands apply.
#
attr_accessor :current_target_definition
#-------------------------------------------------------------------------#
end
end