CocoaPods 基础初探

去年10月的学习笔记,更新复习一下~git


官方文档:CocoaPods Guidesgithub

1. pod install & pod update

1.1 概念

Podfile 文件:描述工程的依赖库,能够理解为是一份“标准”。
Podfile.lock 文件:记录和追踪已生成的 Pod 版本,里面有以前 pod install 时使用的各个库的版本以及依赖的第三方库版本。能够理解为是一份“历史记录”。xcode

  • pod install:从新下载并安装 pods ,版本号从 Podfile.lock 文件中获取,lock 文件中有记录则安装记录版本(不检查更新),无记录则安装 Podfile 指定版本的 pods
  • pod update:无视 Podfile.lock 锁定的版本号,查找并更新到知足 Podfile 中指定版本号要求范围的最新版本 pods
    • 更新指定库 pod update [PODNAME],若无 PODNAME 则默认更新 Podfile 中所有 Pods

扩展学习:iOS里的动态库和静态库缓存

1.2 pod install 内部逻辑

class Install < Command
  include Project
  # ...
  def run
    verify_podfile_exists!
    run_install_with_update(false)
  end
end
复制代码
#初始化 Installer 对象
def run_install_with_update(update)
  installer = Installer.new(config.sandbox, config.podfile, config.lockfile)
  installer.update = update
  installer.install!
end
复制代码
#install 方法
def install!
  prepare                              // 1.准备工做
  resolve_dependencies                 // 2.解决依赖冲突
  download_dependencies                // 3.下载依赖文件
  determine_dependency_product_types   // 4.决定依赖库的类型
  verify_no_duplicate_framework_names  // 5.验证没有重名的framework
  verify_no_static_framework_transitive_dependencies // 6.验证静态库的传递依赖
  verify_framework_usage               // 7.验证framework的使用
  generate_pods_project                // 8.生成工程
  integrate_user_project               // 9.整合用户项目
  perform_post_install_actions         // 10.执行 install 后的行为
end
复制代码
  1. 准备工做:prepare
    • 准备沙盒:一些文件以及目录的删除以及建立
    • 迁移沙盒中部分文件(区分Pods版本迁移地址不一样)
    • 执行 pre_install 的 Hook 函数
  2. 解决依赖冲突:resolve_dependencies
    • 启动 hooks 并建立一个 analyzer
    • 使用 analyzer 更新本地 specs 库、处理版本依赖 (若是设置了 —no-repo-update,就不更新本地索引库)
  3. 下载依赖文件:download_dependencies
def download_dependencies
  UI.section 'Downloading dependencies' do
    create_file_accessors         // 3.1 准备沙盒文件访问器
    install_pod_sources           // 3.2 下载安装Pods依赖库源文件
    run_podfile_pre_install_hooks // 3.3 执行Pods依赖库的pre_install的Hook函数
    clean_pod_sources             // 3.4 根据Config和Installers参数清理Pods的源文件
  end
end
复制代码
  1. 决定依赖库的类型:determine_dependency_product_types
  2. 验证没有重名的 framework:verify_no_duplicate_framework_names
  3. 验证静态库的传递依赖:verify_no_static_framework_transitive_dependencies 检查静态库里是否包含了引用的静态库, 造成传递依赖。静态库的传递依赖若是造成会主动抛出 transitive dependencies that include static binaries 异常。
  4. 验证 framework 的使用:verify_framework_usage
  5. 生成工程:generate_pods_project
def generate_pods_project
  UI.section 'Generating Pods project' do
    prepare_pods_project           // 8.1 准备Pods工程
    install_file_references        // 8.2 安装文件引用
    install_libraries              // 8.3 安装库
    set_target_dependencies        // 8.4 为Target设置依赖
    run_podfile_post_install_hooks // 8.5 执行Podfile的post_install代码块
    write_pod_project              // 8.6 执行Project类的Save方法保存配置
    share_development_pod_schemes  // 8.7 共享依赖库的Target Scheme
    write_lockfiles                // 8.8 修改Pods工程的LockFile文件
  end
复制代码
  1. 整合用户项目:integrate_user_project
    • 负责建立 xcode 的 workspace, 并整合全部的 target 到新的 workspace 中.
    • 抛出 Podfile 空项目依赖和 xcconfig 是否被原有的 xcconfig 所覆盖依赖相关的警告。
  2. 执行 install 后的行为:perform_post_install_actions
    • Pods 库下的文件以便执行 post_install 的 Hook 函数
    • 执行 post_install 的 Hook 函数
    • 抛出签名执行收集的 Spec 废弃警告
    • 从新锁定 Lock Pods 库下的文件防止用户误修改

pod 源码:GitHub - CocoaPods/CocoaPods: The Cocoa Dependency Manager.
源码探究方法参考:pod install和pod update背后那点事 | Startry Blogruby

1.3 install & update 的使用

  • 工程首次执行 pod 命令时,install 和 update 效果一致
  • 安装新 pod 时,使用 pod install 或 pod update [NEW_POD],能够只对新 pod 操做而不更新其余 pods,防止更新其余 pods 可能引发的适配问题。
  • 更新 pod 版本时,根据只需更新一个 pod /需更新所有 pods 来使用pod update [PODNAME]/pod update,使用 pod update 应慎重。

经常使用参数ide

  • --no-repo-update:不更新本地索引库
  • --verbose 和 --silent:显示所有操做信息/不显示信息

2. pod cache

  • pod cache list [NAME]:列出本地 pods 缓存记录
  • pod cache clean [NAME]:删除本地 pods 缓存记录

缓存地址:~/Library/Caches/CocoaPods/函数

3. podspec 公有库发布

3.1 建立项目

  • 使用项目模版建立项目: pod lib create 'xxxxxx'
  • 直接使用本身的项目:使用 pod spec create 'xxxxxx' 给项目添加 Podspec 文件

3.2 验证代码和 Podspec 文件是否有问题

pod lib lint xxxxxx.podspecpost

  • --verbose:输出全部的消息信息
  • --allow-warnings:屏蔽警告
  • --fail-fast:出现第一个错误的时候就中止
  • --sources:若是一个库的 podspec 包含了除 Cocoapods 仓库之外的其余库的引用,则须要该参数指明,用逗号分隔

3.3 打 tag

3.4 上传到 Cocoapods 官方仓库

pod trunk push xxxxxx.podspec 查询是否上传成功:pod search xxxxxx 加入其余开发者:pod trunk add-owner 'xxxxxx' '邮箱'学习

推荐阅读: CocoaPods 结构详解:我所理解的 CocoaPods ui

相关文章
相关标签/搜索