去年10月的学习笔记,更新复习一下~
官方文档:CocoaPods Guides
1. pod install & pod update
1.1 概念
Podfile 文件:描述工程的依赖库,可以理解为是一份“标准”。
Podfile.lock 文件:记录和追踪已生成的 Pod 版本,里面有之前 pod install 时使用的各个库的版本以及依赖的第三方库版本。可以理解为是一份“历史记录”。
pod install
:重新下载并安装 pods ,版本号从 Podfile.lock 文件中获取,lock 文件中有记录则安装记录版本(不检查更新),无记录则安装 Podfile 指定版本的 podspod 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
- 准备工作:prepare
- 准备沙盒:一些文件以及目录的删除以及创建
- 迁移沙盒中部分文件(区分Pods版本迁移地址不同)
- 执行 pre_install 的 Hook 函数
- 解决依赖冲突:resolve_dependencies
- 启动 hooks 并创建一个 analyzer
- 使用 analyzer 更新本地 specs 库、处理版本依赖 (如果设置了 —no-repo-update,就不更新本地索引库)
- 下载依赖文件: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
- 决定依赖库的类型:determine_dependency_product_types
- 验证没有重名的 framework:verify_no_duplicate_framework_names
- 验证静态库的传递依赖:verify_no_static_framework_transitive_dependencies 检查静态库里是否包含了引用的静态库, 形成传递依赖。静态库的传递依赖如果形成会主动抛出 transitive dependencies that include static binaries 异常。
- 验证 framework 的使用:verify_framework_usage
- 生成工程: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
- 整合用户项目:integrate_user_project
- 负责创建 xcode 的 workspace, 并整合所有的 target 到新的 workspace 中.
- 抛出 Podfile 空项目依赖和 xcconfig 是否被原有的 xcconfig 所覆盖依赖相关的警告。
- 执行 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 Blog
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
应慎重。
常用参数
- --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.podspec
- --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