CocoaPods 本身是相當優秀的 iOS 開發套件管理工具,收錄了七千多個開源元件,但庫主體託管在 GitHub。由於眾所周知的原因,它的速度逐年下滑,pod install 時不時就會冒出這種錯誤:
$ pod install Cloning into '/path/to/ios/project/Pods/xxx' error: RPC failed; result=52, HTTP code = 0 fatal: The remote end hung up unexpectedly
流傳已久的加速手段多半是換 RubyGems 源、關閉不必要的更新檢查、改用國內的 Spec 鏡像。本文要談的,是在這些做法之上再往前一步——把鏡像與私有倉庫結合,做出真正屬於團隊的加速方案。
若你想更深入理解 CocoaPods 的運作,推薦閱讀 objc.io 的《CocoaPods under the Hood》(有中文版)。
最近社群流傳的《CocoaPods 最佳實踐探討》建議把 Pods 目錄納入版本控制,這其實是無奈之舉。筆者團隊實行過一段時間,除了每次更新都產生大量無意義的 diff 洗版,偶爾還會遇到版本衝突需要人工收拾。相較之下,我更傾向精簡原則,遵循官方建議。
說到底,這些問題並不是「GitHub 掛了就沒救了」。
CocoaPods 本身就支援私有倉庫。而 0.34.0 版正好加入了在 Podfile 中宣告多個來源倉庫的能力:
source 'https://github.com/artsy/Specs.git' source 'https://github.com/CocoaPods/Specs.git' pod 'AFNetworking' pod 'Mantle'
這個特性原本設計給「擴充官方 Spec 的同時管理自家元件」使用,而我們正好可以拿它來做鏡像分流。
前提是你已經架好 GitLab 服務器(官方安裝教學或社群的 CentOS 版本都可以參考)。
先建立自己的 Spec 倉庫,目錄結構如下:
. ├── CocoaPods-version.yml ├── Specs/ ├── README.md ├── Rakefile └── Gemfile
設定細節這裡不展開,只補充比官方多了 Rakefile 與 Gemfile 兩個檔案,都是 rake 所需。接著配置 gitlab-mirrors,官方文件寫得很清楚。
利用私有 Spec 倉庫的特性,把官方 Spec 目錄下的套件按需或全部鏡像過來,再把裡面的 GitHub 位址換成 GitLab 位址。
這才是核心。少了這一步,你的私有倉庫和國內鏡像沒有任何差別——因為下載來源仍然是 GitHub。
# Rakefile
require 'uri'
require 'fileutils'
require 'multi_json'
require 'net/ssh'
desc '鏡像一個 github 包至 gitlab 倉庫'
task :clone, [:name] do |t, p|
name = p[:name]
current_path = Dir.pwd
specs = Dir[File.join(File.expand_path('~'), '.cocoapods/repos/master/Specs/*')]
repo = specs.select { |s| File.basename(s) == name }.first
if repo
puts " * found repo, copy it here"
repo_store_path = File.join(current_path, 'Specs')
FileUtils.cp_r repo, repo_store_path
puts " * updating repo url"
Dir["#{repo_store_path}/#{name}/*"].each do |f|
pod_file = File.join(f, "#{name}.podspec.json")
json = File.read(pod_file)
data = MultiJson.load json
if data['source']['git']
puts " -> #{data['version']}: git"
orginal_repo_url = data['source']['git']
coverted_repo_name = URI.parse(orginal_repo_url).path[1..-1].gsub('/', '-').downcase
data['source']['git'] = "https://gitlab.dev/mirrors/#{coverted_repo_name}"
File.write(pod_file, JSON.pretty_generate(data))
elsif data['source']['http']
puts " -> #{data['version']}: http url, do you want speed up?"
elsif data['source']['svn']
puts " -> #{data['version']}: svn repo, do you want speed up?"
end
end
else
puts "Not find spec named: #{name}"
end
end
desc 'gitlab 服務器鏡像 Cocoapod Spec'
task :mirror, [:repo] do |t, p|
host = '172.16.0.1'
user = 'icyleaf'
options = {:keys => '~/.ssh/keys/id_rsa.pub'}
puts "Connect gitlab server and mirror"
Net::SSH.start(host, user, options) do |ssh|
gitmirror_path = '/home/gitmirror/gitlab-mirrors'
cmd = "sudo -u gitmirror -H rake \"add[#{p[:repo]}]\""
stdout = ssh.exec!("echo 'cd #{gitmirror_path} && #{cmd}'")
puts stdout
ssh.loop
end
end
# Gemfile source "https://ruby.taobao.org" gem 'rest_client' gem 'multi_json' gem 'rake' gem 'net-ssh'
兩個 task 分工明確:
mirror:把 iOS 開源元件鏡像進 GitLab。clone:把官方 Spec 中指定的套件抓下來,改寫其中的 git 位址,再放進私有倉庫。執行 rake clone[AFNetworking] 之後,該套件所有版本的 podspec.json 裡的 source 都會指向你的 GitLab。接著在 Podfile 中把私有源放在官方源之前,CocoaPods 就會優先從內網拉取。
Q:這套做法靠譜嗎?
A:團隊已經實際運行很長一段時間,沒有出過問題。最大優勢是與官方倉庫完全相容——即使私有服務器暫時連不上,切換回官方源或國內鏡像都是瞬間的事。
Q:沒有伺服器能做嗎?
A:光是鏡像官方 Spec 就需要一台伺服器跑定期同步腳本,這一步省不掉。
Q:國內的 Git 託管服務支援嗎?
A:據了解,國內多數 Git 託管方案都是基於 GitLab 二次開發,理論上可行。gitlab-mirrors 依賴 GitLab 的 API 在鏡像時自動建倉庫,如果實測成功歡迎回饋。
Q:你的程式碼裡伺服器端還呼叫了一個 rake 腳本,沒開源。
A:眼睛很利。這裡補上:在 gitlab-mirrors 專案根目錄新增一個 Rakefile 即可。
require 'uri'
desc "Adding repo to gitmirror"
task :add, [:repo] do |t, p|
repo = p[:repo]
begin
name = URI.parse(repo).path[1..-1].gsub('/', '-').gsub('.git', '')
if name
`./add_mirror.sh -f --git --project-name #{name} --mirror #{repo}`
end
rescue Error => e
puts 'not url'
end
end
只要有工具化的思路,效率問題與環境限制通常都有解。希望這套方案能給你一些靈感,也歡迎分享你的實作結果。
(本文作者:icyleaf)