-
介绍
-
lg_pod_plugin 是一个 ruby gem, 基于 cocoapods 开发, 实现了 pod 预下载功能.
-
lg_pod_plugin 使用 gitlab api 来下载 gitlab 仓库代码, 只将 gitlab 账号信息保存在本地, 并不会泄露你的 gitlab 账户信息.
-
lg_pod_plugin 利用Swift Asnyc 多线程技术进行并发下载, 多任务下载能够有效利用网络资源, 从而减少下载 Pod 花费的时间.
-
lg_pod_plugin 使用 Github 加速功能, 下载单个 commit 对应的压缩文件包, 减少 git clone 速度慢的问题.
-
只提供了预下载功能, 没有修改 cocoapods 源码, 不影响 pod install | update 执行流程, 无侵入式设计, 不需要对现有项目进行改动.
原理
项目整体流程
项目下载流程
缓存路径计算方法: 根据下载参数进行 hash 运算, 生产一个文件路径
开发版本 external pod 文件路径示意图
Release pod 文件路径示意图
文件路径生成算法
def slug(name: self.name, params: self.params, spec: self.spec)
checksum = spec && spec.checksum && '-' << spec.checksum[0, 5]
if released_pod?
"Release/#{name}/#{spec.version}#{checksum}"
else
opts = params.to_a.sort_by(&:first).map { |k, v| "#{k}=#{v}" }.join('-')
digest = Digest::MD5.hexdigest(opts)
"External/#{name}/#{digest}#{checksum}"
end
end
git & branch
pod "AFNetworking", :git => 'https://github.com/AFNetworking/AFNetworking.git', :branch => 'master'
下载前参数:
{"commit": "c29d3d09e5f1ba5ec7265bbdf097eac977071d39", "git": "https://github.com/AFNetworking/AFNetworking.git"}
下载后参数
{"commit": "c29d3d09e5f1ba5ec7265bbdf097eac977071d39", "git": "https://github.com/AFNetworking/AFNetworking.git"}
git & tag
pod "AFNetworking", :git => ''https://github.com/AFNetworking/AFNetworking.git', :tag => '0.2.0'
下载前参数:
{ "git" : "'https://github.com/AFNetworking/AFNetworking.git", "tag" "0.2.0"}
下载后参数
{ "git" : "'https://github.com/AFNetworking/AFNetworking.git", "tag" "0.2.0"}
git & commit
pod "AFNetworking", :git => 'https://github.com/AFNetworking/AFNetworking.git', :commit => 'c29d3d09e5f1ba5ec7265bbdf097eac977071d39'
下载前参数:
{"commit": "c29d3d09e5f1ba5ec7265bbdf097eac977071d39", "git": "https://github.com/AFNetworking/AFNetworking.git"}
下载后参数
{"commit": "c29d3d09e5f1ba5ec7265bbdf097eac977071d39", "git": "https://github.com/AFNetworking/AFNetworking.git"}
http
pod 'AlicloudPush', '1.9.9.7'
下载前参数:
{"http": "https://ios-repo.oss-cn-shanghai.aliyuncs.com/push/1.9.9.7/push.zip"}
下载后参数:
{"http": "https://ios-repo.oss-cn-shanghai.aliyuncs.com/push/1.9.9.7/push.zip"}
判断Pod 缓存是否存在
public
def pod_cache_exist(name, options, spec = nil, released_pod = false)
destination, cache_pod_spec = self.find_pod_cache name, options, spec, released_pod
if (File.exist?(destination) && !destination.children.empty?) && cache_pod_spec.exist?
return [true, destination, cache_pod_spec]
else
return [false, destination, cache_pod_spec]
end
end
#判断缓存是否存在且有效命中缓存
public
def find_pod_cache(name, options, spec = nil, released_pod = false)
hash_map = Hash.new.merge!(options)
if hash_map.has_key?(:version)
hash_map.delete(:version)
end
request = LCache.download_request(name, hash_map, spec, released_pod)
destination = LCache.path_for_pod(request, {})
cache_pod_spec = LCache.path_for_spec(request, {})
[destination, cache_pod_spec]
end
#MARK - 判断 Pod 缓存是否存在
def self.path_for_pod(request, slug_opts = {})
root = self.root_path
root + request.slug(**slug_opts)
end
#MARK - 判断 PodSpec 文件是否存在
def self.path_for_spec(request, slug_opts = {})
root = self.root_path
path = root + 'Specs' + request.slug(**slug_opts)
Pathname.new(path.to_path + '.podspec.json')
end
lg_pod_plugin整体架构
command 提供了命令行工具, 负责输入和解析命令和参数
lg_pod_plugin 核心库, 提供了数据库, 网络请求, 下载, 解析 podfile, podlock 文件内容, 拷贝清理缓存的功能.
sqlite3-1.5.3: 提供了 sqlite 数据库驱动的功能, 兼容 x86, arm 平台.
博客地址: juejin.cn/post/716983…
github地址: github.com/BestiOSDev/… 持续更新欢迎 star
文档地址: www.rubydoc.info/gems/lg_pod…
参考文档:
GitLab API: docs.gitlab.com/ee/api/rest…
抖音研发效能建设 - CocoaPods 优化实践: blog.csdn.net/ByteDanceTe…
Cocoapods 历险记: juejin.cn/column/6966…
CocoaPods Cache问题分析、修复和优化: juejin.cn/post/705892…
记录一次 Cocoapods Plugins 插件开发过程: juejin.cn/post/689370…