iOS - CocoaPods原理

2021-05-05  本文已影响0人  码代码的小马

本文已同步至掘金iOS - CocoaPods原理

CocoaPods将所有的依赖库都放到Pods项目中,让主项目依赖Pods项目,这样源码管理工作都从主项目迁移到了Pods项目中

核心组件

CocoaPods是用 Ruby 写的,并由若干个 Ruby 包 (gems) 构成的。在解析整合过程中,最重要的几个 gems 分别是: CocoaPods/CocoaPods, CocoaPods/Core, 和 CocoaPods/Xcodeproj (是的,CocoaPods 是一个依赖管理工具 -- 利用依赖管理进行构建的!)。CocoaPods 是一个 objc 的依赖管理工具,而其本身是利用 ruby 的依赖管理 gem 进行构建的.

首先我们cd到~/.cocoapods/repos/master/Specs路径下,选择其中一个第三方库,查看其AFNetwork.podspec.json文件(以AFNetwork为例):

{
  "name": "AFNetwork",
  "version": "0.1.0",
  "summary": "Simple request manager about AFNetworking",
  "description": "TODO: Add long description of the pod here.",
  "homepage": "https://github.com/lingyfh/AFNetwork",
  "license": {
    "type": "MIT",
    "file": "LICENSE"
  },
  "authors": {
    "lingyfh": "LingYFH@gmail.com"
  },
  "source": {
    "git": "https://github.com/lingyfh/AFNetwork.git",
    "tag": "0.1.0"
  },
  "platforms": {
    "ios": "8.0"
  },
  "public_header_files": [
    "AFNetwork/*.h",
    "AFNetwork/**/*.h"
  ],
  "source_files": "AFNetwork/Classes/**/*",
  "dependencies": {
    "AFNetworking": [
      "~> 3.0"
    ]
  }
}

可以看到这里包含了所有三方看的信息,包括名字、协议、描述、Github地址、支持平台等

pod install的过程

通常我们在Podfile文件中新增引入第三方库的代码后,执行pod install, 对应的第三方库就安装在Pods项目中了,pod install 这一过程发生了什么呢,我们这个命令后边加上 --verbose, 可以看到如下内容:

xxx@xxxdeMacBook-Pro CocoaPodsDemo % pod install --verbose
  Preparing

Analyzing dependencies

Inspecting targets to integrate
  Using `ARCHS` setting to build architectures of target
  `Pods-defaults-CocoaPodsDemo `: (``)

Finding Podfile changes
  - AFNetworking

Resolving dependencies of `Podfile`
  CDN: trunk Relative path: CocoaPods-version.yml exists! Returning local
  because checking is only perfomed in repo update

Comparing resolved specification to the sandbox manifest
  - AFNetworking

Downloading dependencies

-> Using AFNetworking (3.2.1)
  - Running pre install hooks

Generating Pods project
  - Creating Pods project
  - Installing files into Pods project
    - Adding source files
    - Adding frameworks
    - Adding libraries
    - Adding resources
    - Linking headers
  - Installing Pod Targets
    - Installing target `AFNetworking` iOS 7.0
      - Generating dummy source at `Pods/Target Support
      Files/AFNetworking/AFNetworking-dummy.m`
  - Generating deterministic UUIDs
  - Stabilizing target UUIDs
  - Running post install hooks
    - Podfile
  - Writing Xcode project file to `Pods/Pods.xcodeproj`
  Cleaning up sandbox directory

Integrating client project

Integrating target `Pods-defaults-CocoaPodsDemo ` (`CocoaPodsDemo.xcodeproj` project)
  - Running post integrate hooks
  - Writing Lockfile in `Podfile.lock`
  - Writing Manifest in `Pods/Manifest.lock`
  CDN: trunk Relative path: CocoaPods-version.yml exists! Returning local
  because checking is only perfomed in repo update

-> Pod installation complete! There are 1 dependencies from the Podfile and 1 total pods installed.

分析过程:

以AFNetworking为例:


一旦每个pod的target完成了上面的内容,整个Pod target就会被创建,这增加了相同文件的同时,还增加了另外几个文件,如果源码中包含有资源bundle,将这个bundle添加至target的指令将被添加到Pod-Resources.sh文件中
。还有一个名为Pods-environment.h的文件,文件中包含了一些宏,这些宏可以用来检查某个组件是否来自pod,最后生成两个认可文件,一个是plist, 另一个是markdown,这两个文件用于给最终用户查阅相关许可信息

参考:
CocoaPods 都做了什么?
cocoapods系列教程---原理篇

上一篇 下一篇

猜你喜欢

热点阅读