Module: XCFrameworkConverter

Defined in:
lib/xcframework_converter.rb,
lib/xcframework_converter/version.rb,
lib/xcframework_converter/creation.rb,
lib/xcframework_converter/patching.rb,
lib/xcframework_converter/arm_patcher.rb

Overview

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity

Defined Under Namespace

Modules: ArmPatcher

Constant Summary collapse

VERSION =
'0.7.1'

Class Method Summary collapse

Class Method Details

.convert_framework_to_xcframework(path, platform) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/xcframework_converter/creation.rb', line 23

def convert_framework_to_xcframework(path, platform)
  plist = Xcodeproj::Plist.read_from_path(plist_template_path)
  xcframework_path = Pathname.new(path).sub_ext('.xcframework')
  xcframework_path.mkdir
  plist['AvailableLibraries'].each do |slice|
    slice_library_identifier = slice['LibraryIdentifier'].sub('platform', platform.to_s)
    slice_path = xcframework_path.join(slice_library_identifier)
    slice_path.mkdir
    slice['LibraryPath'] = File.basename(path)
    slice['SupportedPlatform'] = platform.to_s
    slice['LibraryIdentifier'] = slice_library_identifier
    FileUtils.cp_r(path, slice_path)
  end
  Xcodeproj::Plist.write_to_path(plist, xcframework_path.join('Info.plist'))
  FileUtils.rm_rf(path)
  final_framework = Pod::Xcode::XCFramework.open_xcframework(xcframework_path)
  final_framework.slices.each do |slice|
    ArmPatcher.patch_arm_binary(slice) if slice.platform == platform && slice.platform_variant == :simulator
    ArmPatcher.cleanup_unused_archs(slice)
  end
end

.convert_frameworks_to_xcframeworks!(installer) ⇒ Object



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
# File 'lib/xcframework_converter.rb', line 20

def convert_frameworks_to_xcframeworks!(installer)
  installer.analysis_result.specifications.each do |spec|
    next if spec.source && spec.local?

    pod_path = installer.sandbox.pod_dir(Pod::Specification.root_name(spec.name))

    xcframeworks_to_patch = spec.available_platforms.map do |platform|
      consumer = Pod::Specification::Consumer.new(spec, platform)
      consumer.vendored_frameworks.select { |f| File.extname(f) == '.xcframework' }
              .map { |f| pod_path.join(f) }
    end.flatten.uniq

    patch_xcframeworks_if_needed(xcframeworks_to_patch)

    frameworks_to_convert = spec.available_platforms.map do |platform|
      consumer = Pod::Specification::Consumer.new(spec, platform)
      before_rename = consumer.vendored_frameworks.select { |f| File.extname(f) == '.framework' }
      next [] if before_rename.empty?

      after_rename = before_rename.map { |f| Pathname.new(f).sub_ext('.xcframework').to_s }
      proxy = Pod::Specification::DSL::PlatformProxy.new(spec, platform.symbolic_name)
      proxy.vendored_frameworks = consumer.vendored_frameworks - before_rename + after_rename
      before_rename.map { |f| [pod_path.join(f), platform.symbolic_name] }
    end.flatten(1).uniq

    convert_xcframeworks_if_present(frameworks_to_convert)

    remember_spec_as_patched(spec) unless frameworks_to_convert.empty?

    remove_troublesome_xcconfig_items(spec)
  end

  warn "Specs with patched XCFrameworks: #{patched_specs.sort.join(', ')}"
end

.convert_xcframeworks_if_present(frameworks_to_convert) ⇒ Object



55
56
57
58
59
# File 'lib/xcframework_converter.rb', line 55

def convert_xcframeworks_if_present(frameworks_to_convert)
  frameworks_to_convert.each do |path, platform|
    convert_framework_to_xcframework(path, platform) if Dir.exist?(path)
  end
end

.patch_xcframework(xcframework_path) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/xcframework_converter/patching.rb', line 19

def patch_xcframework(xcframework_path)
  xcframework = Pod::Xcode::XCFramework.open_xcframework(xcframework_path)
  slices_to_convert = xcframework.slices.select do |slice|
    slice.platform != :osx &&
      slice.platform_variant != :simulator &&
      slice.supported_archs.include?('arm64')
  end

  slices_to_convert.each do |slice|
    patch_xcframework_impl(xcframework_path, slice.platform.name)
  end
end

.patch_xcframeworks_if_needed(xcframeworks) ⇒ Object



61
62
63
64
65
# File 'lib/xcframework_converter.rb', line 61

def patch_xcframeworks_if_needed(xcframeworks)
  xcframeworks.each do |path|
    patch_xcframework(path) if Dir.exist?(path)
  end
end

.patched_specsObject



100
101
102
# File 'lib/xcframework_converter.rb', line 100

def patched_specs
  @patched_specs ||= Set.new
end

.plist_template_pathObject



19
20
21
# File 'lib/xcframework_converter/creation.rb', line 19

def plist_template_path
  Pathname.new(__FILE__).dirname.join('../xcframework_template.plist')
end

.remember_spec_as_patched(spec) ⇒ Object



96
97
98
# File 'lib/xcframework_converter.rb', line 96

def remember_spec_as_patched(spec)
  patched_specs << spec.root.name
end

.remove_troublesome_xcconfig_items(spec) ⇒ Object



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
# File 'lib/xcframework_converter.rb', line 67

def remove_troublesome_xcconfig_items(spec)
  # some pods put these as a way to NOT support arm64 sim
  # may stop working if a pod decides to put these in a platform proxy

  xcconfigs = %w[
    pod_target_xcconfig
    user_target_xcconfig
  ].map { |key| spec.attributes_hash[key] }.compact

  platforms = %w[
    iphonesimulator
    appletvsimulator
    watchsimulator
  ]

  (xcconfigs.product platforms).each do |xcconfig, platform|
    excluded_archs_key = "EXCLUDED_ARCHS[sdk=#{platform}*]"
    inlcuded_arch_key = "VALID_ARCHS[sdk=#{platform}*]"

    excluded_arm = xcconfig[excluded_archs_key]&.include?('arm64')
    not_inlcuded_arm = xcconfig[inlcuded_arch_key] && !xcconfig[inlcuded_arch_key].include?('arm64')

    remember_spec_as_patched(spec) if excluded_arm || not_inlcuded_arm

    xcconfig.delete(excluded_archs_key)
    xcconfig.delete(inlcuded_arch_key)
  end
end