Skip to content

Instantly share code, notes, and snippets.

@edwardean
Created June 25, 2019 01:59
Show Gist options
  • Save edwardean/b2abcef7dfc1367c5ff0aad43946887e to your computer and use it in GitHub Desktop.
Save edwardean/b2abcef7dfc1367c5ff0aad43946887e to your computer and use it in GitHub Desktop.
Swift接入
require 'cocoapods'
def integrate_umbrella_header(installer)
# 把"xxx-Bridging-Header.h"桥接头文件插入到"xxx-umbrella.h"第一个import语句之前,解决引入Swift之后头文件引用的报错问题
installer.pod_targets.each do |target|
next unless target.uses_swift? and File.exist?(target.umbrella_header_path)
sandbox = File.expand_path(target.pod_target_srcroot.to_s.gsub(/\$\{PODS_ROOT\}/, target.sandbox.root.to_s))
bridging_header_file_name = "#{target.name}-Bridging-Header.h"
bridging_header_files = Dir["#{sandbox}/**/#{bridging_header_file_name}"]
if bridging_header_files.empty?
puts "#{target.name}使用了Swift但是没有添加#{bridging_header_file_name}桥接头文件"
next
end
umbrella_header_import_content = "#import \"#{bridging_header_file_name}\""
umbrella_header = File.read(target.umbrella_header_path)
umbrella_header.gsub!(/\n#import."#{bridging_header_file_name}"/, '')
umbrella_header.sub!(/\n\n/, "\n\n#{umbrella_header_import_content}\n")
File.write(target.umbrella_header_path, umbrella_header)
end
# 手动将Swift库的"xxx-Swift.h"拷贝到"Pods/Headers/Public/xxx"目录下
installer.pods_project.targets.each do |target|
next if target.build_phases.find { |ph| ph.display_name == 'Copy generated compatibility header' }.nil?
build_phase = target.new_shell_script_build_phase('Copy Swift桥接头文件')
build_phase.shell_script = <<-SH.strip_heredoc
COMPATIBILITY_HEADER_PATH="${BUILT_PRODUCTS_DIR}/Swift Compatibility Header/${PRODUCT_MODULE_NAME}-Swift.h"
ditto "${COMPATIBILITY_HEADER_PATH}" "${PODS_ROOT}/Headers/Public/${PRODUCT_MODULE_NAME}/${PRODUCT_MODULE_NAME}-Swift.h"
SH
end
end
# hook cocoapods中的verify_swift_pods_have_module_dependencies方法,屏蔽掉关于use_modular_headers!的报错问题
#[!] The following Swift pods cannot yet be integrated as static libraries:
#The Swift pod `xxx` depends upon `xxx1` and `xxx2`, which do not define modules. To opt into those targets generating module maps (which is necessary to import them from Swift when building as static libraries), you may set `use_modular_headers!` globally in your Podfile, or specify `:modular_headers => true` for particular dependencies.
module Pod
module SwiftIntegration
module PodTargetInstallerExtension
def verify_swift_pods_have_module_dependencies
true
end
end
end
end
Pod::Installer::Xcode::TargetValidator.prepend Pod::SwiftIntegration::PodTargetInstallerExtension
@Davidxiaoshuo
Copy link

hello, 请问针对 verify_swift_pods_have_module_dependencies 这个问题,该怎么应用这个 rb 脚本呢?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment