前言
本文描述 Cocoapods 的工作原理和源碼分析下硕,閱讀完本文后可以更好的理解在使用 Cocoapods 中遇到的問題并找到解決方法玉工,并且有新的開發(fā)語言出現時也可以開發(fā)一款自己的包管理工具羽资。
Cocoapods 的安裝
Cocoapods 的安裝非常簡單,只需一行命令即可(需對系統(tǒng)的網絡請求進行翻墻)
$ sudo gem install cocoapods
安裝完后可在終端輸入 pod 瓮栗,會有如下輸出:
顯示了 pod 的所有可用的命令和命令選項削罩。
Cocoapods 的使用
打開終端,切換到你的工程目錄费奸,輸入下面的命令
pod init
終端輸入 ls 可以看到已經多了個 Podfile 文件弥激,使用 vi 打開,內容如下
# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'
target 'SwiftDemo' do
# Comment the next line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!
# Pods for SwiftDemo
target 'SwiftDemoTests' do
inherit! :search_paths
# Pods for testing
end
target 'SwiftDemoUITests' do
inherit! :search_paths
# Pods for testing
end
end
Podfile 的注釋已經很清楚的告訴我們應該如何編寫 Podfile 文件愿阐。Podfile 的具體編寫規(guī)則請參考官方文檔微服。
編寫好 Podfile 后執(zhí)行以下命令:
pod install
終端會輸出 Setting up CocoaPods Master repo 并會停頓很久。因為第一次使用 pod 會從 GitHub 拉取包含所有可用三方庫信息的 repo缨历,該代碼倉庫會隨著可用三方庫的增多逐漸變大以蕴,2014 年 2 月該 repo 僅有 129MB糙麦,2016 年 8 月該 repo 已經增加到 841 MB,2016 年 11 月該 repo 已達到 1.07GB(具體請參考鏈接)丛肮。后面會在使用小技巧中介紹如何避免在這一步"卡死"赡磅。
Cocoapods 的工作流程
pod install 執(zhí)行流程可分為如下五個步驟
- 查看 ~/.cocoapods/repo/master/Specs 是否存在
- 存在,從這個本地三方庫信息庫中獲取 Podfile 中對應三方庫的 git 地址
- 不存在宝与,輸出 Setting up CocoaPods Master repo焚廊,并拉取三方庫信息庫到 ~/.cocoapods/repo/中
- 使用 git 命令從 GitHub 上拉取 Podfile 中對應的三方庫源碼
在終端中輸入如下命令
cd ~/.cocoapods/repo/master/Specs
ls
可以看到很多文件夾,其中就包含我們平常使用的三方庫的文件夾习劫。
隨便選進入一個文件夾咆瘟,這里我 cd 進了 Alamofire 文件夾。
這里全是 Alamofire 的 release 版本號诽里,當我們使用 pod search Alamofire 命令時會將這里的所有版本號輸出出來袒餐。
cd 進最新版本的文件夾中,里面是一個 Alamofire.podspec.json 文件谤狡,使用 vi 打開灸眼,內容如下
{
"name": "Alamofire",
"version": "4.0.0",
"license": "MIT",
"summary": "Elegant HTTP Networking in Swift",
"homepage": "https://github.com/Alamofire/Alamofire",
"social_media_url": "http://twitter.com/AlamofireSF",
"authors": {
"Alamofire Software Foundation": "info@alamofire.org"
},
"source": {
"git": "https://github.com/Alamofire/Alamofire.git",
"tag": "4.0.0"
},
"platforms": {
"ios": "8.0",
"osx": "10.9",
"tvos": "9.0",
"watchos": "2.0"
},
"source_files": "Source/*.swift"
}
可以看到這里包含了所有的三方庫的相關信息,包括名字墓懂,協(xié)議幢炸,描述,Github 地址拒贱,支持平臺等。
這些信息由三方庫作者提交到該信息倉庫中佛嬉。比如你要發(fā)布自己的三方庫到 Cocoapods 上逻澳,你需要 fork 該repo,然后按照 Cocoapods 規(guī)定的格式把自己三方庫的信息填寫上,再 push 到主分支上暖呕。
不過這個過程 Cocoapods 也提供的相應的命令來完成斜做,具體請參考官方文檔
在查找到對應文件夾后,進行 json 數據解析湾揽,獲得三方庫的 repo 地址瓤逼,調用本地 git 命令拉取源碼,拉取完成后調用本地 xcodebuild 命令把三方庫編譯為 Framework库物。
源碼分析
我們使用的 cocoapods 命令的 GitHub 路徑
https://github.com/CocoaPods/CocoaPods/tree/master/lib/cocoapods/command霸旗。
其中 setup 命令用于第一次使用 cocoapods 時對使用環(huán)境進行設置,下面對 setup 命令的源碼進行分析戚揭。
git 的詳細使用教程請參考文檔
require 'fileutils'
module Pod
class Command
class Setup < Command
self.summary = 'Setup the CocoaPods environment'
self.description = <<-DESC
Creates a directory at `~/.cocoapods/repos` which will hold your spec-repos.
This is where it will create a clone of the public `master` spec-repo from:
https://github.com/CocoaPods/Specs
If the clone already exists, it will ensure that it is up-to-date.
DESC
extend Executable
executable :git
def run
UI.section 'Setting up CocoaPods master repo' do
if master_repo_dir.exist?
set_master_repo_url
set_master_repo_branch
update_master_repo
else
add_master_repo
end
end
UI.puts 'Setup completed'.green
end
#--------------------------------------#
# @!group Setup steps
# Sets the url of the master repo according to whether it is push.
#
# @return [void]
#
def set_master_repo_url
Dir.chdir(master_repo_dir) do
git('remote', 'set-url', 'origin', url)
end
end
# Adds the master repo from the remote.
#
# @return [void]
#
def add_master_repo
cmd = ['master', url, 'master']
Repo::Add.parse(cmd).run
end
# Updates the master repo against the remote.
#
# @return [void]
#
def update_master_repo
show_output = !config.silent?
config.sources_manager.update('master', show_output)
end
# Sets the repo to the master branch.
#
# @note This is not needed anymore as it was used for CocoaPods 0.6
# release candidates.
#
# @return [void]
#
def set_master_repo_branch
Dir.chdir(master_repo_dir) do
git %w(checkout master)
end
end
#--------------------------------------#
# @!group Private helpers
# @return [String] the url to use according to whether push mode should
# be enabled.
#
def url
self.class.read_only_url
end
# @return [String] the read only url of the master repo.
#
def self.read_only_url
'https://github.com/CocoaPods/Specs.git'
end
# @return [Pathname] the directory of the master repo.
#
def master_repo_dir
config.sources_manager.master_repo_dir
end
end
end
end
其執(zhí)行流程可分為如下步驟
- 判斷 ~/.cocoapods/repo目錄是否存在
- 存在诱告,依次調用 set_master_repo_url,set_master_repo_branch民晒,update_master_repo 三個函數分別設置 repo 主分支地址精居,git checkout 到主分支锄禽,拉取主分支代碼,更新repo
- 不存在靴姿,添加主分支 repo
使用 Cocoapods 的小技巧
大家在使用 pod install 命令時一般會加上 --no-repo-update 選項沃但。這使得 pod install 不進行本地三方庫信息庫 git pull 的更新操作。若 Podfile 中指定 Alamofire 的版本號為 4.2.0,但本地 ~/cocoapods/repo/master/Specs/Alamofire 中并沒有此版本號佛吓,此時使用 pod install --no-repo-update 會出現如下錯誤宵晚,
若直接使用 pod install 便會先執(zhí)行 pod repo update,由于 github 需要翻墻辈毯,并且更新的內容過大就會等待很久坝疼。
既然知道了 Cocoapods 的原理,我們便可以手動在 ~./cocoapods/repo/master/Specs 中添加我們需要的三方庫版本信息谆沃,避免了把所有的并沒有使用到的三方庫信息更新到本地钝凶。
下面以添加 Alamofire 4.2.0 版本信息到本地為例子。
在終端輸入如下命令
cd ~/.cocoapods/repo/master/Specs/Alamofire/
mkdir 4.2.0
cp ~/.cocoapods/repo/master/Specs/Alamofire/1.1.3/Alamofire.podspec.json ./4.2.0
上面的命令創(chuàng)建了名為 4.2.0 的文件夾唁影,并復制了一個庫信息的 json 文件到新創(chuàng)建的文件夾中耕陷,用 vi 打開該文件,內容如下
{
"name": "Alamofire",
"version": "1.1.3",
"license": "MIT",
"summary": "Elegant HTTP Networking in Swift",
"homepage": "https://github.com/Alamofire/Alamofire",
"social_media_url": "http://twitter.com/mattt",
"authors": {
"Mattt Thompson": "m@mattt.me"
},
"source": {
"git": "https://github.com/Alamofire/Alamofire.git",
"tag": "1.1.3"
},
"platforms": {
"ios": "8.0"
},
"source_files": "Source/*.swift",
"requires_arc": true
}
我們需要修改版本號据沈,source 的 tag哟沫。那 source 的 tag 怎么知道是多少呢,這個可以從 GitHub 上找到锌介。
可以看到所有 release 版本信息嗜诀。
總結
到此,你已經知道了 Cocoapods 是調用了本地的 git 命令來實現拉取源碼孔祸。當出現一門新的語言隆敢,可以按照類似邏輯開發(fā)自己的三方庫管理工具。
歡迎關注我的簡書崔慧,我會定期做一些技術分享:)