-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathcocoapods.rb
More file actions
620 lines (537 loc) · 20.8 KB
/
cocoapods.rb
File metadata and controls
620 lines (537 loc) · 20.8 KB
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
# coding: utf-8
# Copyright (c) 2012-2014, Laurent Sansonetti <lrz@hipbyte.com>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
unless defined?(Motion::Project::Config)
raise "This file must be required within a RubyMotion project Rakefile."
end
require 'xcodeproj'
require 'cocoapods'
module Motion::Project
class Config
variable :pods
def pods(vendor_options = {}, &block)
@pods ||= Motion::Project::CocoaPods.new(self, vendor_options)
if block
@pods.instance_eval(&block)
end
@pods.configure_project
@pods
end
end
class App
class << self
def build_with_cocoapods(platform, opts = {})
unless File.exist?(CocoaPods::PODS_ROOT)
$stderr.puts "[!] No CocoaPods dependencies found in #{CocoaPods::PODS_ROOT}, run the `[bundle exec] rake pod:install` task."
exit 1
end
build_without_cocoapods(platform, opts)
# Install the resource which will be generated after built
installed_resources = App.config.pods.install_resources
unless installed_resources.empty?
app_resources_dir = config.app_resources_dir(platform)
installed_resources.each do |path|
App.builder.copy_resource(path.to_s, File.join(app_resources_dir, path.basename.to_s))
end
end
end
alias_method "build_without_cocoapods", "build"
alias_method "build", "build_with_cocoapods"
end
end
#---------------------------------------------------------------------------#
class CocoaPods
PODS_ROOT = 'vendor/Pods'
TARGET_NAME = 'RubyMotion'
SUPPORT_FILES = File.join(PODS_ROOT, "Target Support Files/Pods-#{TARGET_NAME}")
attr_accessor :podfile
def initialize(config, vendor_options)
@config = config
@vendor_options = vendor_options
@use_frameworks = false
case @config.deploy_platform
when 'MacOSX'
platform = :osx
when 'iPhoneOS'
platform = :ios
when 'AppleTVOS'
platform = :tvos
when 'WatchOS'
platform = :watchos
else
App.fail "Unknown CocoaPods platform: #{@config.deploy_platform}"
end
@podfile = Pod::Podfile.new(Pathname.new(Rake.original_dir) + 'Rakefile') do
platform(platform, config.deployment_target)
target(TARGET_NAME)
install!('cocoapods', :integrate_targets => false)
end
cp_config.podfile = @podfile
cp_config.installation_root = Pathname.new(File.expand_path(config.project_dir)) + 'vendor'
if cp_config.verbose = !!ENV['COCOAPODS_VERBOSE']
require 'claide'
end
end
# Adds the Pods project to the RubyMotion config as a vendored project and
#
def configure_project
if (xcconfig = self.pods_xcconfig_hash) && ldflags = xcconfig['OTHER_LDFLAGS']
@config.resources_dirs << resources_dir.to_s
frameworks = installed_frameworks[:pre_built]
if frameworks
@config.embedded_frameworks += frameworks
@config.embedded_frameworks.uniq!
end
if @use_frameworks
configure_project_frameworks
else
configure_project_static_libraries
end
end
end
def configure_project_frameworks
if (xcconfig = self.pods_xcconfig_hash) && ldflags = xcconfig['OTHER_LDFLAGS']
# Add libraries to @config.libs
pods_libraries
frameworks = ldflags.scan(/-framework\s+"?([^\s"]+)"?/).map { |m| m[0] }
if build_frameworks = installed_frameworks[:build]
build_frameworks = build_frameworks.map { |path| File.basename(path, ".framework") }
frameworks.delete_if { |f| build_frameworks.include?(f) }
end
static_frameworks = pods_frameworks(frameworks)
static_frameworks_paths = static_frameworks_paths(static_frameworks)
search_path = static_frameworks_paths.inject("") { |s, path|
s += " -I'#{path}' -I'#{path}/Headers'"
}
@vendor_options[:bridgesupport_cflags] ||= ''
@vendor_options[:bridgesupport_cflags] << " #{header_search_path} #{framework_search_path} #{search_path}"
@config.weak_frameworks.concat(ldflags.scan(/-weak_framework\s+([^\s]+)/).map { |m| m[0] })
@config.weak_frameworks.uniq!
vendors = @config.vendor_project(PODS_ROOT, :xcode, {
:target => "Pods-#{TARGET_NAME}",
:products => build_frameworks.map { |name| "#{name}.framework" },
:allow_empty_products => build_frameworks.empty?,
}.merge(@vendor_options))
vendor = vendors.last
if vendor.respond_to?(:build_bridgesupport)
static_frameworks_paths.each do |path|
path = File.expand_path(path)
bs_file = File.join(Builder.common_build_dir, "#{path}.bridgesupport")
headers = Dir.glob(File.join(path, '**{,/*/**}/*.h'))
@config.vendor_project(PODS_ROOT, :bridgesupport, {
:bs_file => bs_file,
:headers => headers,
}.merge(@vendor_options))
end
end
end
end
def configure_project_static_libraries
# TODO replace this all once Xcodeproj has the proper xcconfig parser.
if (xcconfig = self.pods_xcconfig_hash) && ldflags = xcconfig['OTHER_LDFLAGS']
# Collect the Pod products
pods_libs = pods_libraries
# Initialize ':bridgesupport_cflags', in case the use
@vendor_options[:bridgesupport_cflags] ||= ''
@vendor_options[:bridgesupport_cflags] << " #{header_search_path} #{framework_search_path}"
frameworks = ldflags.scan(/-framework\s+"?([^\s"]+)"?/).map { |m| m[0] }
pods_frameworks(frameworks)
@config.weak_frameworks.concat(ldflags.scan(/-weak_framework\s+([^\s]+)/).map { |m| m[0] })
@config.weak_frameworks.uniq!
@config.vendor_project(PODS_ROOT, :xcode, {
:target => "Pods-#{TARGET_NAME}",
:headers_dir => "Headers/Public",
:products => pods_libs.map { |lib_name| "lib#{lib_name}.a" },
:allow_empty_products => pods_libs.empty?,
}.merge(@vendor_options))
end
end
# DSL
#-------------------------------------------------------------------------#
def source(source)
@podfile.source(source)
end
def pod(*name_and_version_requirements, &block)
@podfile.pod(*name_and_version_requirements, &block)
end
# Deprecated.
def dependency(*name_and_version_requirements, &block)
@podfile.dependency(*name_and_version_requirements, &block)
end
def post_install(&block)
@podfile.post_install(&block)
end
def use_frameworks!(flag = true)
@use_frameworks = flag
@podfile.use_frameworks!(flag)
end
# Installation
#-------------------------------------------------------------------------#
def pods_installer
@installer ||= Pod::Installer.new(cp_config.sandbox, @podfile, cp_config.lockfile)
end
# Performs a CocoaPods Installation.
#
# For now we only support one Pods target, this will have to be expanded
# once we work on more spec support.
#
# Let RubyMotion re-generate the BridgeSupport file whenever the list of
# installed pods changes.
#
def install!(update)
FileUtils.rm_rf(resources_dir)
pods_installer.update = update
pods_installer.install!
symlink_framework_headers
install_resources
copy_cocoapods_env_and_prefix_headers
end
PUBLIC_HEADERS_ROOT = File.join(PODS_ROOT, 'Headers/Public')
def framework_public_headers_dir(framework_path)
framework_name = framework_path[%r{/([^/]*)\.framework/}, 1]
File.join(@config.project_dir, PUBLIC_HEADERS_ROOT, framework_name)
end
def symlink_framework_headers
framework_search_paths.each do |framework_search_path|
Dir.glob("#{framework_search_path}/*.framework/Headers").each do |framework_path|
FileUtils.mkdir_p(framework_public_headers_dir(framework_path))
Dir.glob("#{framework_path}/*.h").each do |header_path|
relative_path = "../../..#{header_path.sub(File.join(@config.project_dir, PODS_ROOT), '')}"
File.symlink(relative_path, "#{framework_public_headers_dir(framework_path)}/#{File.basename(header_path)}")
end
end
end
end
# TODO this probably breaks in cases like resource bundles etc, need to test.
#
def install_resources
FileUtils.mkdir_p(resources_dir)
installed_resources = []
resources.each do |file|
begin
dst = resources_dir + file.basename
if file.exist? && !dst.exist?
FileUtils.cp_r(file, resources_dir)
installed_resources << dst
end
rescue ArgumentError => exc
unless exc.message =~ /same file/
raise
end
end
end
installed_resources
end
def copy_cocoapods_env_and_prefix_headers
headers = Dir.glob(["#{PODS_ROOT}/*.h", "#{PODS_ROOT}/*.pch", "#{PODS_ROOT}/Target Support Files/**/*.h", "#{PODS_ROOT}/Target Support Files/**/*.pch"])
headers.each do |header|
src = File.basename(header)
dst = src.sub(/\.pch$/, '.h')
dst_path = File.join(PUBLIC_HEADERS_ROOT, "____#{dst}")
unless File.exist?(dst_path)
FileUtils.mkdir_p(PUBLIC_HEADERS_ROOT)
FileUtils.cp(header, dst_path)
end
end
end
# Helpers
#-------------------------------------------------------------------------#
# This is the output that gets shown in `rake config`, so it should be
# short and sweet.
#
def inspect
cp_config.lockfile.to_hash['PODS'].map do |pod|
pod.is_a?(Hash) ? pod.keys.first : pod
end.inspect
end
def cp_config
Pod::Config.instance
end
def analyzer
Pod::Installer::Analyzer.new(cp_config.sandbox, @podfile, cp_config.lockfile)
end
def pods_xcconfig
@pods_xcconfig ||= begin
path = Pathname.new(@config.project_dir) + SUPPORT_FILES + "Pods-#{TARGET_NAME}.release.xcconfig"
Xcodeproj::Config.new(path) if path.exist?
end
@pods_xcconfig
end
def pods_xcconfig_hash
if xcconfig = pods_xcconfig
xcconfig.to_hash
end
end
def pods_libraries
xcconfig = pods_xcconfig_hash
ldflags = xcconfig['OTHER_LDFLAGS']
# Get the name of all static libraries that come pre-built with pods
pre_built_static_libs = lib_search_paths.map do |path|
Dir[File.join(path, '**/*.a')].map { |f| File.basename(f) }
end.flatten
pods_libs = []
@config.libs.concat(ldflags.scan(/-l"?([^\s"]+)"?/).map { |m|
lib_name = m[0]
next if lib_name.nil?
if lib_name.start_with?('Pods-')
# For CocoaPods 0.37.x or below. This block is marked as deprecated.
pods_libs << lib_name
nil
elsif pre_built_static_libs.include?("lib#{lib_name}.a")
"#{lib_search_path_flags} -ObjC -l#{lib_name}"
elsif File.exist?("/usr/lib/lib#{lib_name}.dylib")
"/usr/lib/lib#{lib_name}.dylib"
else
pods_libs << lib_name
nil
end
}.compact)
@config.libs.uniq!
pods_libs
end
def pods_frameworks(frameworks)
frameworks = frameworks.dup
if installed_frameworks[:pre_built]
installed_frameworks[:pre_built].each do |pre_built|
frameworks.delete(File.basename(pre_built, ".framework"))
end
end
static_frameworks = []
case @config.deploy_platform
when 'MacOSX'
@config.framework_search_paths.concat(framework_search_paths)
@config.framework_search_paths.uniq!
framework_search_paths.each do |framework_search_path|
frameworks.reject! do |framework|
path = File.join(framework_search_path, "#{framework}.framework")
if File.exist?(path)
@config.embedded_frameworks << path
@config.embedded_frameworks.uniq!
true
else
false
end
end
end
when 'iPhoneOS', 'AppleTVOS', 'WatchOS'
# If we would really specify these as ‘frameworks’ then the linker
# would not link the archive into the application, because it does not
# see any references to any of the symbols in the archive. Treating it
# as a static library (which it is) with `-ObjC` fixes this.
#
framework_search_paths.each do |framework_search_path|
frameworks.reject! do |framework|
path = File.join(framework_search_path, "#{framework}.framework")
if File.exist?(path)
@config.libs << "-ObjC '#{File.join(path, framework)}'"
@config.libs.uniq!
static_frameworks << framework
true
else
false
end
end
end
end
@config.frameworks.concat(frameworks)
@config.frameworks.uniq!
static_frameworks
end
def lib_search_path_flags
lib_search_paths.map { |p| "-L'#{p}'" }.join(' ')
end
def lib_search_paths
@lib_search_paths ||= begin
xcconfig = pods_xcconfig_hash
@lib_search_path_flags = xcconfig['LIBRARY_SEARCH_PATHS'] || ""
paths = []
@lib_search_path_flags = @lib_search_path_flags.split(/\s/).map do |path|
if path =~ /(\$\(inherited\))|(\$\{inherited\})|(\$CONFIGURATION_BUILD_DIR)|(\$PODS_CONFIGURATION_BUILD_DIR)/
nil
else
path = path.gsub(/(\$\(PODS_ROOT\))|(\$\{PODS_ROOT\})/, File.join(@config.project_dir, PODS_ROOT))
paths << path.gsub('"', '')
end
end.compact.join(' ')
paths
end
@lib_search_paths
end
def framework_search_paths
@framework_search_paths ||= begin
xcconfig = pods_xcconfig_hash
paths = []
if search_paths = xcconfig['FRAMEWORK_SEARCH_PATHS']
search_paths = search_paths.strip
unless search_paths.empty?
search_paths.scan(/"([^"]+)"/) do |search_path|
path = search_path.first.gsub!(/(\$\(PODS_ROOT\))|(\$\{PODS_ROOT\})/, "#{@config.project_dir}/#{PODS_ROOT}")
paths << path if path
end
# If we couldn't parse any search paths, then presumably nothing was properly quoted, so
# fallback to just assuming the whole value is one path.
if paths.empty?
path = search_paths.gsub!(/(\$\(PODS_ROOT\))|(\$\{PODS_ROOT\})/, "#{@config.project_dir}/#{PODS_ROOT}")
paths << path if path
end
end
end
paths
end
@framework_search_paths
end
def framework_search_path
framework_search_paths.map { |p| "-F'#{File.expand_path(File.join(p, '..'))}'" }.join(' ')
end
def header_search_paths
@header_search_paths ||= begin
xcconfig = pods_xcconfig_hash
paths = []
if search_paths = xcconfig['HEADER_SEARCH_PATHS']
search_paths = search_paths.strip
unless search_paths.empty?
search_paths.scan(/"([^"]+)"/) do |search_path|
path = search_path.first.gsub!(/(\$\(PODS_ROOT\))|(\$\{PODS_ROOT\})/, "#{@config.project_dir}/#{PODS_ROOT}")
paths << File.expand_path(path) if path
end
# If we couldn't parse any search paths, then presumably nothing was properly quoted, so
# fallback to just assuming the whole value is one path.
if paths.empty?
path = search_paths.gsub!(/(\$\(PODS_ROOT\))|(\$\{PODS_ROOT\})/, "#{@config.project_dir}/#{PODS_ROOT}")
paths << File.expand_path(path) if path
end
end
end
framework_search_paths.each do |framework_search_path|
Dir.glob("#{framework_search_path}/*.framework/Headers").each do |framework_path|
paths << framework_public_headers_dir(framework_path)
end
end
paths
end
@header_search_paths
end
def header_search_path
header_search_paths.map { |p| "-I'#{p}'" }.join(' ')
end
def static_frameworks_paths(frameworks)
paths = []
framework_search_paths.each do |framework_search_path|
paths += Dir.glob("#{framework_search_path}/*.framework")
end
paths.keep_if { |path|
frameworks.include?(File.basename(path, ".framework"))
}
paths
end
# Do not copy `.framework` bundles, these should be handled through RM's
# `embedded_frameworks` config attribute.
#
def resources
resources = []
script = Pathname.new(@config.project_dir) + SUPPORT_FILES + "Pods-#{TARGET_NAME}-resources.sh"
return resources unless File.exist?(script)
File.open(script) { |f|
f.each_line do |line|
if matched = line.match(/install_resource\s+(.*)/)
path = (matched[1].strip)[1..-2]
if path.start_with?('${PODS_ROOT}')
path = path.sub('${PODS_ROOT}/', '')
end
if path.include?("$PODS_CONFIGURATION_BUILD_DIR")
path = File.join(".build", File.basename(path))
end
unless File.extname(path) == '.framework'
resources << Pathname.new(@config.project_dir) + PODS_ROOT + path
end
end
end
}
resources.uniq
end
def resources_dir
Pathname.new(@config.project_dir) + PODS_ROOT + 'Resources'
end
def installed_frameworks
return @installed_frameworks if @installed_frameworks
@installed_frameworks = {}
path = Pathname.new(@config.project_dir) + SUPPORT_FILES + "Pods-#{TARGET_NAME}-frameworks.sh"
return @installed_frameworks unless path.exist?
@installed_frameworks[:pre_built] = []
@installed_frameworks[:build] = []
File.open(path) { |f|
f.each_line do |line|
if matched = line.match(/install_framework\s+(.*)/)
path = (matched[1].strip)[1..-2]
if path.include?('${PODS_ROOT}')
path = path.sub('${PODS_ROOT}', PODS_ROOT)
@installed_frameworks[:pre_built] << File.join(@config.project_dir, path)
@installed_frameworks[:pre_built].uniq!
elsif path.include?('$BUILT_PRODUCTS_DIR')
path = path.sub('$BUILT_PRODUCTS_DIR', "#{PODS_ROOT}/.build")
@installed_frameworks[:build] << File.join(@config.project_dir, path)
@installed_frameworks[:build].uniq!
end
end
end
}
@installed_frameworks
end
end
end
namespace :pod do
task :update_spec_repos do
$stderr.puts '[!] If you need to update CocoaPods repository to install newer libraries, please run "pod repo update" command before.'
end
desc "Download and integrate newly added pods"
task :install => :update_spec_repos do
# TODO Should ideally not have to be controller manually.
Pod::UserInterface.title_level = 1
pods = App.config.pods
begin
need_install = pods.analyzer.needs_install?
rescue
# TODO fix this, see https://github.com/HipByte/motion-cocoapods/issues/57#issuecomment-17810809
need_install = true
end
# TODO Should ideally not have to be controller manually.
Pod::UserInterface.title_level = 0
pods.install!(false) if need_install
end
desc "Update outdated pods"
task :update => :update_spec_repos do
pods = App.config.pods
pods.install!(true)
end
end
namespace :clean do
# This gets appended to the already existing clean:all task.
task :all do
dir = Motion::Project::CocoaPods::PODS_ROOT
if File.exist?(dir)
App.info 'Delete', dir
rm_rf dir
end
end
end