CocoaPods - 源码篇

本文详细剖析了CocoaPods在`pod install`过程中的关键步骤,包括`install!`、`download_dependencies`等,以及`Podfile`的各个部分如`platform`、`use_frameworks`等。重点讨论了Pods Scheme自动消失的问题,通过源码分析发现是由于每次`pod install`都会重新生成scheme文件,导致isShown设为false。为了解决这个问题,文章提供了一种在`post_install`中自定义代码的方法,通过延迟执行更新scheme属性来显示Pods Scheme。
摘要由CSDN通过智能技术生成
前言

由于项目里业务线很多,集成了很多第三方 pod库 和 私有 pod库,整个 pod project体积非常大。默认的Xcode 编译行为寻找依赖的project进行编译,并且是并行的。



我们做了如下改动,加快主工程编译速度:

.去除了主工程对 Pods target的依赖编译
.取消上面两个勾选
.在 Manage Scheme 里勾选了Pods project,以便于可以手动选择 Pods project进行编译

如果对 Pods库更改了,我们可以手动选择 Pods Scheme 进行编译,然后再编译主工程,这样避免 Pods不必要编译。


问题

每次pod install之后,pods scheme 自动消失了,我们找到 xcschememanagement.plist文件

cd demo/Pods/Pods.xcodeproj/xcuserdata/Green.xcuserdatad/xcschemes
cat xcschememanagement.plist

# 输出
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
    <key>Pods.xcscheme</key>
    <dict>
        <key>isShown</key>
        <false/>
    </dict>
...

可以看到:Pods.xcscheme isShownfalse,这样导致scheme 中没有Pods

Pod Install 剖析

我们来看看 CocoaPods 源码

cd /Library/Ruby/Gems/2.0.0/gems/
#这里有很多版本,我们只看0.38.2

当我们执行 pod install,其实调用到Installer对象,

#文件位置:`cocoapods-0.38.2/lib/cocoapods/command/project.rb`

#初始化 Installer 对象       
def run_install_with_update(update)
    installer = Installer.new(config.sandbox, config.podfile, config.lockfile)
    installer.update = update

    #install 方法
    installer.install!
  end
end
install!
#文件位置:`cocoapods-0.38.2/lib/cocoapods/installer.rb`文件,

  #install 方法
def install!
  prepare
  resolve_dependencies

  #下载依赖
  download_dependencies
  determine_dependency_product_types
  verify_no_duplicate_framework_names
  verify_no_static_framework_transitive_dependencies
  verify_framework_usage

  #合成 pods project
  generate_pods_project

  integrate_user_project if config.integrate_targets?
  perform_post_install_actions
end
download_dependencies
#下载pods 资源
def download_dependencies
  UI.section 'Downloading dependencies' do
    create_file_accessors
    install_pod_sources
    run_podfile_pre_install_hooks
    clean_pod_sources
  end
end
generate_pods_project
#合成 pods project
def generate_pods_project
  UI.section 'Generating Pods project' do
    prepare_pods_project
    install_file_references
    install_libraries
    set_target_dependencies

    #执行Podfile 的post_install 代码块
    run_podfile_post_install_hooks

    #重新写入pod project,就是在这里修改了所有`
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值