iOS 伪组件专题程序员iOS备忘录

知乎 iOS 客户端基于 CocoaPods 实现的二进制化方案

2018-11-17  本文已影响120人  老邢Thierry

更多移动技术文章请关注本文集:知乎移动平台专栏

背景

随着公司业务规模的增长,iOS 客户端的代码量也越来越大,编译一次项目的时间也越来越长。减少编译时间成了一个不得不面对的问题。

现有的二进制方案如 CarthageRome 等都是在本地生成 framework,没法实现「一次编译,处处使用」的目标。

为了实现这个目标,就需要一个人或者一个 CI Job,把编译好的二进制产物上传到某个的地方,集中化地管理这些二进制形式的依赖。然后在每个人 pod install 的时候,检查该 pod 版本对应的二进制是否存在,如果有就使用,没有就继续采用源码的方式依赖。

上面的方案隐藏了许多细节,比如到底应该如何集中管理这些 pod,如何知道对应的版本是否存在,如何在 pod install 的时候动态地把这些 pod 从源码形式的依赖换成二进制形式的依赖等等。

因为这整个流程涉及生产方(产生二进制)和消费方(使用二进制),我就把整个方案分为两个流程详细介绍一下。

产生二进制

代码结构

产生二进制的流程在一个 CI Job 中,每隔一段时间,它会同步主仓库最新的 dev 分支,然后运行管理此环节的工具,Platypus。

它的结构如下:


image.png

config.yml 是与工程相关联的配置,其中包含了需要二进制化的名单(pod_names),project 文件相关信息,以及工程初始化的 action(prebuild_action)等等。specs_repo 是私有的 podspec 仓库,需要单独创建,负责集中管理已经二进制好的 pod 信息。

具体流程

如下图:


image.png

下面是各个步骤的详细说明,

比如有一个使用 tag 方式引用的组件,把它的 tag 号后面加上 -zhihu-static 作为它在私有 Specs 仓库中的版本号,它在 podfile 中的 external_source 作为 summary 字段,同时确保唯一性。这里的映射关系只要能一一对应起来,随便怎么建立都好。

pod 'ABC', git: 'git@git.xxx.com:xxx/ABC.git', tag: '4.24.0.9'
所以它被改完版本号后 poddpec 会长成这个样子,

{
  "name": "ABC",
  "version": "4.24.0.9-zhihu-static",
  "summary": "{:git=>\"git@git.xxx.com:xxx/ABC.git\", :tag=>\"4.24.0.9\"}"
  ...
}
h) Specs 仓库目录结构如下所示,目录均为手动创建,没有使用 CocoaPods 提供的方式更新。
├── A
│   └── 4.22.0.8-zhihu-static
│       └── A.podspec.json
├── B
│   ├── 0.2.21-zhihu-static
│   │   └── B.podspec.json
│   └── 0.2.9-zhihu-static
│       └── B.podspec.json
├── C
│   └── 1.4.0-zhihu-static
│       └── C.podspec
└── D
    └── 2.5.0-zhihu-static
        └── D.podspec

使用二进制

在触发 pod install 过程之前,需要在本地把私有 Specs 仓库更新到最新,pod repo update xxx。

接下来就是 patch pod install 替换依赖的过程了。在不更改 podfile 的情况下,只能模仿 pod install 的过程,自己创建一个脚本来替代这个操作了。整个过程不复杂,可以参考下面这一段带注释的代码,

# 参考 `CocoaPods` 的源码,模拟 `pod install` 执行的过程
argv = CLAide::ARGV.new([])
cmd = Pod::Command.new(argv)
cmd.send :verify_podfile_exists!
installer = cmd.send :installer_for_config
installer.repo_update = false
installer.update = false

podfile = installer.podfile

# 获取此次 install 的配置,是全部使用二进制还是全部使用源码
# 全部使用二进制时,哪些 pod 依旧使用源码引入
use_all_binary, source_pod_list = ZHPodInstallHelper.read_binary_pods_pref
use_all_binary = false if ENV['ALL_SOURCE'] == 'true'
unless use_all_binary
  puts '🐢 pod install with all source'
  installer.install!
  exit(0)
end

# 为 podfile 添加二进制 Specs 仓库的 source
podfile.send(:get_hash_value, 'sources')
hash_sources = podfile.send(:get_hash_value, 'sources') || []
hash_sources << 'git@git.xxx.com:xxx/xxx.git'
podfile.send(:set_hash_value, 'sources', hash_sources.uniq)

# 遍历 podfile 中的所有 dependencies
podfile.root_target_definitions.each do |root_target_definition|
  children_definitions = root_target_definition.recursive_children
  children_definitions.each do |children_definition|
    dependencies_hash_array = children_definition.send(:get_hash_value, 'dependencies')
    next if dependencies_hash_array.count.zero?
    dependencies_hash_array.each do |dependencie_hash_item|
      next if dependencie_hash_item.class.name != 'Hash'
      dependencie_hash_item.each do |name, value|
        next if value[0].is_a?(Hash) && value[0][:path]
        search_name = name
        search_name = name.split('/')[0] if name.include?('/')

        # 对于想要以源码依赖的 pod,不作修改
        next if source_pod_list.include?(search_name)

        # 根据 podfile 中引用的源码版本,在私有 Specs 仓库中查找相应二进制的版本
        version = ZHPodInstallHelper.get_binary_version(search_name, value[0].to_s)
        # 存在对应的二进制版本,就替换掉
        dependencie_hash_item[name] = [version] if version
      end
    end
    # 替换 podfile 的 dependencies 为修改后的 dependencies
    children_definition.send(:set_hash_value, 'dependencies', dependencies_hash_array)
  end
end

installer.install!

限制

说完了整个方案的流程,我们再来谈谈这个方案存在的一些问题,

总结

以上就是知乎 iOS 客户端二进制预编译的方案,有任何问题都欢迎大家留言,写下你的看法
另外,知乎移动平台团队也在招人中,欢迎 iOS、Android、前端 各类工程师的加入

关于作者

X140Yu,16年加入知乎,目前为 iOS 基础架构工程师,负责知乎 iOS App 组件化及工程效率等相关工作。

上一篇下一篇

猜你喜欢

热点阅读