cocoapods-kz 0.0.13 → 0.0.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (29) hide show
  1. checksums.yaml +4 -4
  2. data/lib/cocoapods-kz/command/info.rb +5 -2
  3. data/lib/cocoapods-kz/command/repair.rb +19 -12
  4. data/lib/cocoapods-kz/gem_version.rb +1 -1
  5. data/lib/cocoapods-kz/helpers/kz_analyzer.rb +29 -0
  6. data/lib/cocoapods-kz/helpers/kz_framework_manager.rb +24 -1
  7. data/lib/cocoapods-kz/helpers/{kz_generator.rb → kz_generator_hmap.rb} +2 -222
  8. data/lib/cocoapods-kz/helpers/kz_generator_on_demand_resources.rb +231 -0
  9. data/lib/cocoapods-kz/helpers/kz_global_helper.rb +38 -8
  10. data/lib/cocoapods-kz/helpers/kz_pod_target.rb +79 -5
  11. data/lib/cocoapods-kz/helpers/repair_module_import.rb +1 -1
  12. data/lib/cocoapods-kz/native/build_configuration.rb +12 -0
  13. data/lib/cocoapods-kz/native/dls.rb +13 -7
  14. data/lib/cocoapods-kz/native/file_accessor.rb +25 -5
  15. data/lib/cocoapods-kz/native/installer.rb +11 -2
  16. data/lib/cocoapods-kz/native/path_list.rb +11 -0
  17. data/lib/cocoapods-kz/native/pod_target_installer.rb +0 -20
  18. data/lib/cocoapods-kz/native/specification.rb +6 -0
  19. data/lib/cocoapods-kz/native/target_installer_helper.rb +36 -1
  20. data/lib/cocoapods-kz/native/target_integrator.rb +6 -12
  21. data/lib/cocoapods-kz/native.rb +2 -0
  22. data/lib/cocoapods-kz/resources/FlexCompiler_V1 +0 -0
  23. data/lib/cocoapods-kz/resources/on_demand_resources/kz_on_demand_resources_process.rb +194 -0
  24. data/lib/cocoapods-kz/resources/on_demand_resources/kz_on_demand_resources_xocde.sh +4 -81
  25. metadata +8 -7
  26. data/lib/cocoapods-kz/native/dsl_spec.rb +0 -7
  27. data/lib/cocoapods-kz/resources/on_demand_resources/kz_complete_asset_pack_output_spec_plist.rb +0 -26
  28. data/lib/cocoapods-kz/resources/on_demand_resources/kz_create_asset_pack_manifest_plist.rb +0 -44
  29. data/lib/cocoapods-kz/resources/on_demand_resources/kz_create_on_demand_resources_plis.rb +0 -26
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 60b78485f551ed394bb167d18aad9e08008ea9c432ef555ade88cfc241f918bb
4
- data.tar.gz: '082f62c4d943cde90357a9a5a55c10636a39ad232e9aded7230170301c01453e'
3
+ metadata.gz: 772c4e4e048e8879e3a0e9a922b887c3206338c9ec397f8747b017891f47e51d
4
+ data.tar.gz: 995dfcee40ec45c2c6b23d43bc7d08a8e3c8e2c6570fc9d7881c76850b679014
5
5
  SHA512:
6
- metadata.gz: ea906c0ac96d2c98a2eab4631a356cb1753de215ca0c0694cd13af454b3418f96f1fe4400d081c63f69115c6870efabdd146ef839168cdaa8d9de2ba3e2bdc6d
7
- data.tar.gz: 295dec357984a4e81fea271b0b261af9e0dc6a0e383d048df8e46224521d3e77ee58124116b0e0d42731121af35d8d4234b92b63c51ca0fc26028e714cba830a
6
+ metadata.gz: 802feb2c0501d20a2cf5b18031ee8f029d8b234a28bb9c8e6b6fbf6a21ac27d429a90af7f26aadb6977fb4b76d9e9e75cb07e94b33b1acbd0d0eb969bf7e065b
7
+ data.tar.gz: 3b9c1b52b50bb27d1b5f2ae8d595cd427409c0fc0653a0781ae5ae394efdfa5d2642d1fe6d29f2e12ef9b92c6e08ce276201ab2843b777bb9b9dcacacc9899a6
@@ -11,8 +11,11 @@ module Pod
11
11
  DESC
12
12
 
13
13
  def initialize(argv)
14
- KZ::KZGlobalHelper.instance.analyze_special_parameters(true, false, argv.arguments!)
15
- KZ::KZGlobalHelper.instance.generate_kz_pod_targets = true
14
+ if Pod.match_version?('~> 1.11')
15
+ KZ::KZGlobalHelper.instance.kz_pod_enable = true
16
+ KZ::KZGlobalHelper.instance.analyze_special_parameters(true, false, argv.arguments!)
17
+ KZ::KZGlobalHelper.instance.generate_kz_pod_targets = true
18
+ end
16
19
  super
17
20
  end
18
21
 
@@ -25,24 +25,31 @@ module Pod
25
25
  @repair_private_hmap = argv.flag?('private-hmap')
26
26
  banner! unless @repair_module_import || @repair_dynamic_swift || @repair_private_hmap
27
27
 
28
- KZ::KZGlobalHelper.instance.analyze_special_parameters(true, false, argv.arguments!)
29
- KZ::KZGlobalHelper.instance.generate_kz_pod_targets = true
28
+ if Pod.match_version?('~> 1.11')
29
+ KZ::KZGlobalHelper.instance.kz_pod_enable = true
30
+ KZ::KZGlobalHelper.instance.analyze_special_parameters(true, false, argv.arguments!)
31
+ KZ::KZGlobalHelper.instance.generate_kz_pod_targets = true
32
+ end
30
33
 
31
34
  super
32
35
  end
33
36
 
34
37
  def run
35
- installer = installer_for_config
36
- installer.prepare
37
- installer.resolve_dependencies
38
- installer.download_dependencies
39
-
40
- if @repair_dynamic_swift
41
- KZ::KZSwiftAttachOCFeature.new.repair
42
- elsif @repair_module_import
43
- KZ::KZRepairModuleImport.new(installer.aggregate_targets.first.user_project).repair
44
- else @repair_private_hmap
38
+ KZ::KZGlobalHelper.instance.prepare
39
+
40
+ if KZ::KZGlobalHelper.instance.kz_pod_enable
41
+ installer = installer_for_config
42
+ installer.prepare
43
+ installer.resolve_dependencies
44
+ installer.download_dependencies
45
+
46
+ if @repair_dynamic_swift
47
+ KZ::KZSwiftAttachOCFeature.new.repair
48
+ elsif @repair_module_import
49
+ KZ::KZRepairModuleImport.new(installer.aggregate_targets.first.user_project).repair
50
+ else @repair_private_hmap
45
51
  KZ::KZGlobalHelper.instance.kz_generator.create_hamp
52
+ end
46
53
  end
47
54
  end
48
55
  end
@@ -1,5 +1,5 @@
1
1
  module CocoapodsKz
2
- VERSION = "0.0.13"
2
+ VERSION = "0.0.15"
3
3
  end
4
4
 
5
5
  module Pod
@@ -3,18 +3,41 @@ require_relative 'kz_global_helper'
3
3
 
4
4
  module KZ
5
5
 
6
+ class KZFlexLibInfo
7
+ attr_accessor :have_flexLib_pod
8
+ attr_accessor :have_kzswiftui_pod
9
+ attr_accessor :flexLib_version
10
+
11
+ def initialize
12
+ @have_flexLib_pod = false
13
+ @have_kzswiftui_pod = false
14
+ @flexLib_version = Pod::Version::ZERO
15
+ end
16
+ end
17
+
6
18
  class KZAnalyzer
7
19
  attr_accessor :all_kz_pod_targets
20
+ attr_accessor :pod_of_flexlib_info
8
21
 
9
22
  def initialize(native_pod_targets, development_pods)
10
23
  @native_pod_targets = native_pod_targets
11
24
  @development_pods = development_pods.keys
12
25
  @all_kz_pod_targets = {}
26
+ @pod_of_flexlib_info = KZFlexLibInfo.new
13
27
  end
14
28
 
15
29
  def analyer
16
30
  @native_pod_targets.each { |native_pod_target|
17
31
  create_kz_pod_target_from(native_pod_target, :kz_pod_origin_mode)
32
+
33
+ if native_pod_target.root_spec.name == "FlexLib"
34
+ @pod_of_flexlib_info ||= KZFlexLibInfo()
35
+ @pod_of_flexlib_info.have_flexLib_pod = true
36
+ @pod_of_flexlib_info.flexLib_version = native_pod_target.root_spec.version
37
+ elsif native_pod_target.root_spec.name == "KZSwiftUI"
38
+ @pod_of_flexlib_info ||= KZFlexLibInfo()
39
+ @pod_of_flexlib_info.have_kzswiftui_pod = true
40
+ end
18
41
  }
19
42
 
20
43
  # 检测是否有product名称相同的target
@@ -59,6 +82,12 @@ module KZ
59
82
  kz_pod_target = KZPodTarget.new(native_pod_target)
60
83
  is_dev_pod = @development_pods.include?(native_pod_target_name)
61
84
  kz_pod_target.is_dev_pod = is_dev_pod
85
+ if KZGlobalHelper.instance.on_demand_resources_info.enable
86
+ on_demand_resources_config_patterns = KZGlobalHelper.instance.on_demand_resources_info.config_plist_path_patterns[kz_pod_target.root_name]
87
+ unless on_demand_resources_config_patterns.nil?
88
+ kz_pod_target.on_demand_resources_config_patterns = on_demand_resources_config_patterns
89
+ end
90
+ end
62
91
  kz_pod_target.force_config_pod_mode = get_force_config_pod_mode(kz_pod_target)
63
92
  if config_pod_mode != :kz_pod_origin_mode && !is_dev_pod
64
93
  kz_pod_target.config_pod_mode = config_pod_mode
@@ -2,6 +2,7 @@ require_relative 'kz_pod_target'
2
2
  require_relative 'kz_global_helper'
3
3
  require_relative 'kz_config_result'
4
4
  require 'tmpdir'
5
+ require 'shellwords'
5
6
  require_relative 'kz_log'
6
7
 
7
8
  module KZ
@@ -50,6 +51,26 @@ module KZ
50
51
  @@all_resources_cache_info
51
52
  end
52
53
 
54
+ def self.handle_origin_library(origin_libraries, kz_pod_target)
55
+ tempdir = Pathname(Dir.mktmpdir)
56
+ lib_frameworks_path = tempdir + "lib_frameworks"
57
+ FileUtils.mkdir_p(lib_frameworks_path)
58
+
59
+ origin_lib_frameworks = []
60
+ origin_libraries.each do |origin_library_path|
61
+ lib_framework_name = origin_library_path.basename(origin_library_path.extname)
62
+ lib_framework_path = lib_frameworks_path + "#{lib_framework_name}.framework"
63
+ FileUtils.mkdir_p(lib_framework_path)
64
+
65
+ FileUtils.cp(origin_library_path, lib_framework_path + lib_framework_name)
66
+ origin_lib_frameworks.append(lib_framework_path)
67
+ end
68
+
69
+ new_frameworks = handle_origin_framework(origin_lib_frameworks, kz_pod_target)
70
+ FileUtils.rm_r(tempdir)
71
+ new_frameworks
72
+ end
73
+
53
74
  def self.handle_origin_framework(origin_frameworks, kz_pod_target)
54
75
  new_frameworks = []
55
76
  old_destination_path = kz_pod_target.pod_config_cache_path(false , true)
@@ -181,7 +202,9 @@ module KZ
181
202
  FileUtils.mkdir_p(ar_x_path)
182
203
  ar_x_success = system("cd #{ar_x_path}; ar x #{arm64_framework_exe_path}")
183
204
  if ar_x_success
184
- system("for file in #{ar_x_path}/*.o; do #{ARM64_TO_SIMULATOR_EXECUTE_PATH} $file; done")
205
+ Dir["#{ar_x_path}/*.o"].each do |file|
206
+ system(ARM64_TO_SIMULATOR_EXECUTE_PATH, file)
207
+ end
185
208
  system("ar crv #{temp_framwork_exe_path} #{ar_x_path}/*.o &> /dev/null")
186
209
  else
187
210
  FileUtils.rm_r(tempdir)
@@ -1,5 +1,6 @@
1
1
  require 'xcodeproj'
2
2
  require 'fileutils'
3
+ require_relative 'kz_generator_on_demand_resources'
3
4
 
4
5
  module KZ
5
6
 
@@ -8,8 +9,6 @@ module KZ
8
9
  def initialize(main_project)
9
10
  @all_kz_pod_targets = KZGlobalHelper.instance.kz_analyzer.all_kz_pod_targets
10
11
  @main_project = main_project
11
- @installation_root = Pod::Config.instance.installation_root
12
- @on_demand_resources_folder = KZ_POD_CONFIG_ROOT + "OnDemandResources"
13
12
  end
14
13
 
15
14
  def add_framework_generator_build_phase(native_target)
@@ -346,224 +345,5 @@ module KZ
346
345
  json_file.close
347
346
  end
348
347
 
349
- def create_on_demand_resources
350
- # 清除历史配置
351
- FileUtils.rm_r(@on_demand_resources_folder) if File.exist?(@on_demand_resources_folder)
352
- FileUtils.mkdir_p(@on_demand_resources_folder)
353
-
354
- # 汇总所有配置文件,用于代码中加载资源
355
- on_demand_resources_all_config = {}
356
- @all_kz_pod_targets.each do |target_name, kz_pod_target|
357
- if kz_pod_target.is_dev_pod
358
- # 寻找配置文件KZOnDemandResourcesConfig.plist
359
- kz_pod_target.native_pod_target.file_accessors.each do |file_accessor|
360
- file_accessor.resource_bundles.map do |bundle_name, paths|
361
- paths.each do |path|
362
- if path.basename.to_s == "KZOnDemandResourcesConfig.plist"
363
- plist_hash = Xcodeproj::Plist.read_from_path(path)
364
- if plist_hash.size > 0
365
- on_demand_resources_all_config[kz_pod_target.name] = plist_hash
366
- analyze_on_demand_resources_config_plist(plist_hash, kz_pod_target, paths)
367
- kz_pod_target.add_on_demand_resources(path)
368
- end
369
- end
370
- end
371
- end
372
- end
373
- end
374
- end
375
-
376
- if on_demand_resources_all_config.size > 0
377
- use_for_code_plist_path = @on_demand_resources_folder + "KZOnDemandResourcesAllConfig.plist"
378
- Xcodeproj::Plist.write_to_path(on_demand_resources_all_config, use_for_code_plist_path)
379
- end
380
- end
381
-
382
- def analyze_on_demand_resources_config_plist(plist_hash, kz_pod_target, all_resources_paths)
383
- resources_path = @on_demand_resources_folder + "Bundles/#{kz_pod_target.name}"
384
- FileUtils.mkdir_p(resources_path)
385
-
386
- # 生成AssetPackOutputSpecifications.plist,用于Images.xcassets编译
387
- asset_pack_output_specifications_resources = []
388
-
389
- # 用于生成OnDemandResources.plist文件,放到main bundle中标记按需加载资源
390
- on_demand_resources_plist = {}
391
- bundle_resource_request_tags = {}
392
- on_demand_resources_plist["NSBundleResourceRequestTags"] = bundle_resource_request_tags
393
- bundle_resource_request_asset_packs = {}
394
- on_demand_resources_plist["NSBundleResourceRequestAssetPacks"] = bundle_resource_request_asset_packs
395
-
396
- # 统计所有的xcassets资源
397
- have_config_xcassets = false
398
-
399
- # 从配置plist中读取'Initial Install Tags'资源
400
- initial_plist_hash = plist_hash["Initial Install Tags"]
401
- on_deman_resources_from_plist(initial_plist_hash, kz_pod_target, 1, all_resources_paths, resources_path) do |tag, bundle_id, bundle_path, normal_file_names, have_xcassets_paths|
402
- # 如有配置中有.xcassets资源,需要先标记,等待编译时单独再单独编译
403
- if have_xcassets_paths
404
- have_config_xcassets = true
405
-
406
- resource_item = {}
407
- resource_item["bundle-id"] = bundle_id
408
- resource_item["bundle-path"] = bundle_path
409
- resource_item["tags"] = [tag]
410
- asset_pack_output_specifications_resources << resource_item
411
- end
412
-
413
- bundle_resource_request_tags[tag] = { "NSAssetPacks" => [bundle_id] }
414
- bundle_resource_request_asset_packs[bundle_id] = normal_file_names if normal_file_names.size > 0
415
- end
416
-
417
- # Prefetched Tag Order
418
- prefetched_plist_hash = plist_hash["Prefetched Tag Order"]
419
- on_deman_resources_from_plist(prefetched_plist_hash, kz_pod_target, 0.5, all_resources_paths, resources_path) do |tag, bundle_id, bunlde_path, normal_file_names, have_xcassets_paths|
420
- if have_xcassets_paths
421
- have_config_xcassets = true
422
-
423
- resource_item = {}
424
- resource_item["bundle-id"] = bundle_id
425
- resource_item["bundle-path"] = bunlde_path
426
- resource_item["tags"] = [tag]
427
- asset_pack_output_specifications_resources << resource_item
428
- end
429
-
430
- bundle_resource_request_tags[tag] = { "NSAssetPacks" => [bundle_id] }
431
- bundle_resource_request_asset_packs[bundle_id] = normal_file_names if normal_file_names.size > 0
432
- end
433
-
434
- # Download Only On Demand
435
- download_plist_hash = plist_hash["Download Only On Demand"]
436
- on_deman_resources_from_plist(download_plist_hash, kz_pod_target, 0, all_resources_paths, resources_path) do |tag, bundle_id, bunlde_path, normal_file_names, have_xcassets_paths|
437
- if have_xcassets_paths
438
- have_config_xcassets = true
439
-
440
- resource_item = {}
441
- resource_item["bundle-id"] = bundle_id
442
- resource_item["bundle-path"] = bunlde_path
443
- resource_item["tags"] = [tag]
444
- asset_pack_output_specifications_resources << resource_item
445
- end
446
-
447
- bundle_resource_request_tags[tag] = { "NSAssetPacks" => [bundle_id] }
448
- bundle_resource_request_asset_packs[bundle_id] = normal_file_names if normal_file_names.size > 0
449
- end
450
-
451
- if asset_pack_output_specifications_resources.size > 0
452
- write_to_path(asset_pack_output_specifications_resources, resources_path + "AssetPackOutputSpecifications.plist")
453
- end
454
-
455
- if bundle_resource_request_tags.size > 0 || bundle_resource_request_asset_packs.size > 0
456
- Xcodeproj::Plist.write_to_path(on_demand_resources_plist, resources_path + "OnDemandResources.plist")
457
- end
458
-
459
- # 一个组件可能有多个xcassets,只要其中一个有配置on demand resources,所有xcassets都需要单独编译
460
- all_xcassets_paths = []
461
- if have_config_xcassets
462
- all_resources_paths.each do |path|
463
- if path.extname == ".xcassets"
464
- all_xcassets_paths << path
465
- end
466
- end
467
- end
468
-
469
- target_bundle_info = {}
470
- target_bundle_info["name"] = kz_pod_target.name
471
- target_bundle_info["xcassets_paths"] = all_xcassets_paths
472
- Xcodeproj::Plist.write_to_path(target_bundle_info, resources_path + "TargetBundleInfo.plist")
473
-
474
- end
475
-
476
- def on_deman_resources_from_plist(plist_hash, kz_pod_target, priority, all_resources_paths, resources_path)
477
- return unless (!plist_hash.nil? && plist_hash.size > 0)
478
-
479
- plist_hash.each do |tag, file_paths|
480
- normal_file_names = []
481
- normal_file_paths = []
482
- have_xcassets_paths = false
483
- file_paths.each do |rel_file_path|
484
- file_path = @installation_root + rel_file_path.strip
485
- next unless file_path.exist?
486
-
487
- if file_path.extname == ".imageset"
488
- # imageset文件配置
489
- xcassets_path = find_xcassets_path(file_path)
490
- if all_resources_paths.include?(xcassets_path)
491
- kz_pod_target.add_on_demand_resources(xcassets_path)
492
- have_xcassets_paths = true
493
- end
494
-
495
- content_json_path = file_path + "Contents.json"
496
- content_json = JSON.parse(File.read(file_path + "Contents.json"))
497
- content_json["properties"] = { "on-demand-resource-tags" => [tag] }
498
- content_json_file = File.new(content_json_path, 'w')
499
- content_json_file.syswrite(JSON.pretty_generate(content_json))
500
- else
501
- # 普通文件配置
502
- if all_resources_paths.include?(file_path)
503
- kz_pod_target.add_on_demand_resources(file_path)
504
- normal_file_paths << file_path
505
- normal_file_names << file_path.basename
506
- end
507
- end
508
- end
509
-
510
- create_bundle_info_plist(tag, priority, normal_file_paths, resources_path) do |bundle_id, bundle_path|
511
- yield(tag, bundle_id, bundle_path, normal_file_names, have_xcassets_paths)
512
- end
513
- end
514
- end
515
-
516
- def find_xcassets_path(path)
517
- return "" if path.root?
518
-
519
- if path.extname == ".xcassets"
520
- path
521
- else
522
- find_xcassets_path(path + "..")
523
- end
524
- end
525
-
526
- def create_bundle_info_plist(tag, priority, normal_file_paths, resource_path)
527
- # project bundle identifier
528
- project_bundle_identifier = KZGlobalHelper.instance.on_demand_resources_bundle_id
529
- tag_md5 = md5_sign(tag)
530
- # bundle folder
531
- demand_bundle_folder_name = "#{project_bundle_identifier}.#{tag}-#{tag_md5}.assetpack"
532
- demand_bundle_folder = resource_path + "OnDemandResources/#{demand_bundle_folder_name}"
533
- # create bundle folder
534
- FileUtils.mkdir_p(demand_bundle_folder) unless FileTest::exist?(demand_bundle_folder)
535
- # copy file
536
- normal_file_paths.each do |file|
537
- FileUtils.cp(file, demand_bundle_folder)
538
- end
539
-
540
- bundle_info_plist = {}
541
- bundle_info_plist["Priority"] = priority if priority > 0
542
- bundle_info_plist["Tags"] = [tag]
543
- demand_bundle_id = "#{project_bundle_identifier}.asset-pack-#{tag_md5}"
544
- bundle_info_plist["CFBundleIdentifier"] = demand_bundle_id
545
- Xcodeproj::Plist.write_to_path(bundle_info_plist, demand_bundle_folder + "Info.plist")
546
-
547
- yield(demand_bundle_id, demand_bundle_folder_name)
548
- end
549
-
550
- def md5_sign(key)
551
- OpenSSL::Digest::MD5.hexdigest(key)
552
- end
553
-
554
- # Xcodeproj::Plist.write_to_path只支持hash,如果是数组需要重写write方法
555
- def write_to_path(hash, path)
556
- unless path.is_a?(String) || path.is_a?(Pathname)
557
- raise TypeError, "The given `#{path}` must be a string or 'pathname'."
558
- end
559
- path = path.to_s
560
- raise IOError, 'Empty path.' if path.empty?
561
-
562
- File.open(path, 'w') do |f|
563
- plist = Nanaimo::Plist.new(hash, :xml)
564
- Nanaimo::Writer::XMLWriter.new(plist, :pretty => true, :output => f, :strict => false).write
565
- end
566
- end
567
-
568
348
  end
569
- end
349
+ end
@@ -0,0 +1,231 @@
1
+ require 'xcodeproj'
2
+ require 'fileutils'
3
+
4
+ module KZ
5
+ class KZGenerator
6
+
7
+ def create_on_demand_resources
8
+ kz_on_demand_resources_all_config_path = KZ_POD_CONFIG_ROOT + "KZOnDemandResourcesAllConfig.plist"
9
+ FileUtils.rm(kz_on_demand_resources_all_config_path) if File.exist?(kz_on_demand_resources_all_config_path)
10
+
11
+ # 汇总所有配置文件,用于代码中加载资源
12
+ on_demand_resources_all_config = {}
13
+ @all_kz_pod_targets.each do |target_name, kz_pod_target|
14
+ on_demand_resources_info = kz_pod_target.on_demand_resources_info
15
+ next if on_demand_resources_info.empty?
16
+
17
+ analyze_on_demand_resources_config_plist(on_demand_resources_info, kz_pod_target)
18
+ on_demand_resources_all_config[target_name] = on_demand_resources_info
19
+ end
20
+
21
+ # 将汇总的配置写入app中,供代码读取
22
+ if on_demand_resources_all_config.size > 0
23
+ write_to_path(on_demand_resources_all_config, kz_on_demand_resources_all_config_path)
24
+ end
25
+ end
26
+
27
+ def get_all_xcassets(kz_pod_target)
28
+ all_xcassets_paths = []
29
+ kz_pod_target.native_pod_target.file_accessors.each do |file_accessor|
30
+ file_accessor.resource_bundles.map do |bundle_name, paths|
31
+ paths.each do |path|
32
+ if path.extname == ".xcassets"
33
+ all_xcassets_paths << path
34
+ end
35
+ end
36
+ end
37
+ end
38
+ all_xcassets_paths
39
+ end
40
+
41
+ def analyze_on_demand_resources_config_plist(plist_hash, kz_pod_target)
42
+ resources_path = kz_pod_target.pod_config_cache_path(false, false, true)
43
+ FileUtils.rm_r(resources_path) if File.exist?(resources_path)
44
+ FileUtils.mkdir_p(resources_path)
45
+ kz_pod_target.on_demand_resources_config_path = resources_path.to_s
46
+
47
+ # 生成AssetPackOutputSpecifications.plist,用于Images.xcassets编译
48
+ asset_pack_output_specifications_resources = []
49
+
50
+ # 用于生成OnDemandResources.plist文件,放到main bundle中标记按需加载资源
51
+ on_demand_resources_plist = {}
52
+ bundle_resource_request_tags = {}
53
+ on_demand_resources_plist["NSBundleResourceRequestTags"] = bundle_resource_request_tags
54
+ bundle_resource_request_asset_packs = {}
55
+ on_demand_resources_plist["NSBundleResourceRequestAssetPacks"] = bundle_resource_request_asset_packs
56
+
57
+ # 用于odr资源编译时读取所需的参数
58
+ odr_build_info = {}
59
+
60
+ # 从配置plist中读取'Initial Install Tags'资源
61
+ initial_plist_hash = plist_hash["Initial Install Tags"]
62
+ on_demand_resources_from_plist(initial_plist_hash, kz_pod_target, 1, resources_path, odr_build_info) do |tag, bundle_id, bundle_path, normal_file_names, have_xcassets_paths|
63
+ # 如有配置中有.xcassets资源,需要先标记,等待编译时单独再单独编译
64
+ if have_xcassets_paths
65
+ resource_item = {}
66
+ resource_item["bundle-id"] = bundle_id
67
+ resource_item["bundle-path"] = bundle_path
68
+ resource_item["tags"] = [tag]
69
+ asset_pack_output_specifications_resources << resource_item
70
+ end
71
+
72
+ bundle_resource_request_tags[tag] = { "NSAssetPacks" => [bundle_id] }
73
+ bundle_resource_request_asset_packs[bundle_id] = normal_file_names if normal_file_names.size > 0
74
+ end
75
+
76
+ # Prefetched Tag Order
77
+ prefetched_plist_hash = plist_hash["Prefetched Tag Order"]
78
+ on_demand_resources_from_plist(prefetched_plist_hash, kz_pod_target, 0.5, resources_path, odr_build_info) do |tag, bundle_id, bunlde_path, normal_file_names, have_xcassets_paths|
79
+ if have_xcassets_paths
80
+ resource_item = {}
81
+ resource_item["bundle-id"] = bundle_id
82
+ resource_item["bundle-path"] = bunlde_path
83
+ resource_item["tags"] = [tag]
84
+ asset_pack_output_specifications_resources << resource_item
85
+ end
86
+
87
+ bundle_resource_request_tags[tag] = { "NSAssetPacks" => [bundle_id] }
88
+ bundle_resource_request_asset_packs[bundle_id] = normal_file_names if normal_file_names.size > 0
89
+ end
90
+
91
+ # Download Only On Demand
92
+ download_plist_hash = plist_hash["Download Only On Demand"]
93
+ on_demand_resources_from_plist(download_plist_hash, kz_pod_target, 0, resources_path, odr_build_info) do |tag, bundle_id, bunlde_path, normal_file_names, have_xcassets_paths|
94
+ if have_xcassets_paths
95
+ resource_item = {}
96
+ resource_item["bundle-id"] = bundle_id
97
+ resource_item["bundle-path"] = bunlde_path
98
+ resource_item["tags"] = [tag]
99
+ asset_pack_output_specifications_resources << resource_item
100
+ end
101
+
102
+ bundle_resource_request_tags[tag] = { "NSAssetPacks" => [bundle_id] }
103
+ bundle_resource_request_asset_packs[bundle_id] = normal_file_names if normal_file_names.size > 0
104
+ end
105
+
106
+ if asset_pack_output_specifications_resources.size > 0
107
+ write_to_path(asset_pack_output_specifications_resources, resources_path + "AssetPackOutputSpecifications.plist")
108
+ end
109
+
110
+ if bundle_resource_request_tags.size > 0 || bundle_resource_request_asset_packs.size > 0
111
+ write_to_path(on_demand_resources_plist, resources_path + "OnDemandResources.plist")
112
+ end
113
+
114
+ unless odr_build_info.empty?
115
+ all_xcassets_dest_plist_path = kz_pod_target.pod_config_cache_path(false, false, true) + "odr_build_info.plist"
116
+ write_to_path(odr_build_info, all_xcassets_dest_plist_path)
117
+ end
118
+ end
119
+
120
+ def on_demand_resources_from_plist(plist_hash, kz_pod_target, priority, resources_path, odr_build_info)
121
+ return unless (!plist_hash.nil? && plist_hash.size > 0)
122
+
123
+ all_normal_file_names = []
124
+ plist_hash.each do |tag, file_paths|
125
+ normal_file_names = []
126
+ normal_file_paths = []
127
+
128
+ have_xcassets_paths = false
129
+ file_paths.each do |file_path|
130
+ next unless file_path.exist?
131
+
132
+ if file_path.extname == ".imageset"
133
+ have_xcassets_paths = true
134
+ xcassets_path = find_xcassets_path(file_path)
135
+ unless xcassets_path.nil?
136
+ on_demand_resources_xcassets(kz_pod_target, odr_build_info, xcassets_path) do |tag_info|
137
+ tag_info[file_path.relative_path_from(xcassets_path)] = tag
138
+ end
139
+ end
140
+ else
141
+ # 普通文件配置
142
+ normal_file_paths << file_path
143
+ normal_file_names << file_path.basename
144
+ all_normal_file_names << file_path.basename
145
+ end
146
+ end
147
+
148
+ create_bundle_info_plist(tag, priority, normal_file_paths, resources_path) do |bundle_id, bundle_path|
149
+ yield(tag, bundle_id, bundle_path, normal_file_names, have_xcassets_paths)
150
+ end
151
+ end
152
+
153
+ unless all_normal_file_names.empty?
154
+ odr_build_info["normal_files"] ||= []
155
+ odr_normal_files = odr_build_info["normal_files"]
156
+ odr_normal_files.concat(all_normal_file_names)
157
+ end
158
+ end
159
+
160
+ def find_xcassets_path(path)
161
+ return nil if path.root?
162
+
163
+ if path.extname == ".xcassets"
164
+ path
165
+ else
166
+ find_xcassets_path(path.parent)
167
+ end
168
+ end
169
+
170
+ def on_demand_resources_xcassets(kz_pod_target, odr_build_info, xcassets_path)
171
+ if odr_build_info["all_xcassets"].nil?
172
+ all_xcassets_info = {}
173
+ all_xcassets_paths = kz_pod_target.all_xcassets_paths
174
+ all_xcassets_paths.each do |path|
175
+ all_xcassets_info[path.basename.to_s] = { "path" => path.to_s, "tags" => {} }
176
+ end
177
+ odr_build_info["all_xcassets"] = all_xcassets_info
178
+ end
179
+ all_xcassets_info = odr_build_info["all_xcassets"]
180
+ xcassets_name = xcassets_path.basename.to_s
181
+ xcassets_info = all_xcassets_info[xcassets_name]
182
+ if xcassets_info.nil?
183
+ all_xcassets_info[xcassets_name] = { "path" => xcassets_path.to_s, "tags" => {} }
184
+ end
185
+ yield(all_xcassets_info[xcassets_name]["tags"])
186
+ end
187
+
188
+ def create_bundle_info_plist(tag, priority, normal_file_paths, resource_path)
189
+ # project bundle identifier
190
+ project_bundle_identifier = KZGlobalHelper.instance.on_demand_resources_info.bundle_id
191
+ tag_md5 = md5_sign(tag)
192
+ # bundle folder
193
+ demand_bundle_folder_name = "#{project_bundle_identifier}.#{tag}-#{tag_md5}.assetpack"
194
+ demand_bundle_folder = resource_path + "OnDemandResources/#{demand_bundle_folder_name}"
195
+ # create bundle folder
196
+ FileUtils.mkdir_p(demand_bundle_folder) unless FileTest::exist?(demand_bundle_folder)
197
+ # copy file
198
+ normal_file_paths.each do |file|
199
+ FileUtils.cp(file, demand_bundle_folder)
200
+ end
201
+
202
+ bundle_info_plist = {}
203
+ bundle_info_plist["Priority"] = priority if priority > 0
204
+ bundle_info_plist["Tags"] = [tag]
205
+ demand_bundle_id = "#{project_bundle_identifier}.asset-pack-#{tag_md5}"
206
+ bundle_info_plist["CFBundleIdentifier"] = demand_bundle_id
207
+ Xcodeproj::Plist.write_to_path(bundle_info_plist, demand_bundle_folder + "Info.plist")
208
+
209
+ yield(demand_bundle_id, demand_bundle_folder_name)
210
+ end
211
+
212
+ def md5_sign(key)
213
+ OpenSSL::Digest::MD5.hexdigest(key)
214
+ end
215
+
216
+ # Xcodeproj::Plist.write_to_path只支持hash,如果是数组需要重写write方法
217
+ def write_to_path(hash, path)
218
+ unless path.is_a?(String) || path.is_a?(Pathname)
219
+ raise TypeError, "The given `#{path}` must be a string or 'pathname'."
220
+ end
221
+ path = path.to_s
222
+ raise IOError, 'Empty path.' if path.empty?
223
+
224
+ File.open(path, 'w') do |f|
225
+ plist = Nanaimo::Plist.new(hash, :xml)
226
+ Nanaimo::Writer::XMLWriter.new(plist, :pretty => true, :output => f, :strict => false).write
227
+ end
228
+ end
229
+
230
+ end
231
+ end