提速pod update

74 阅读1分钟

提升pod update的速度

在使用pod引入私有库的时候,pod会默认clone整个仓库,而我们其实只需要指定的tag或者branch即可。

本文将使用ruby语言在Podfile文件里写一个函数,将pod的clone逻辑替换为我们自己的。


首先,pod引入私有库的传统写法:

# Podfile
pod 'Libxxx', :git => 'git@code.xxx.com:xxx/Libxxx.git', :branch => '2.2.0'

接下来,我们打破传统,定义自己的include函数:

require 'digest'

# 只clone当前分支,并引用
def very_fast_include(name, url, branch, submodels = [])
  puts ""
  puts "very_fast_include: #{name} #{url} #{branch}"
  
  # 根文件夹
  root_path = "./PodsLocal"
  # 不存在则创建
  Dir.mkdir(root_path) unless Dir.exist?(root_path)
  
  # 是否需要更新
  need_update = 1
  
  # 库名文件夹
  lib_path = "#{root_path}/#{name}_#{Digest::MD5.hexdigest(url)}"
  # 版本文件夹
  ver_path = "#{lib_path}/#{branch}"
  if Pathname.new(lib_path).directory?
    # 库存在,看看版本是否存在
    if Pathname.new(ver_path).directory?
      puts "    --No update required"
      need_update = 0
    end
  end
  
  if need_update == 1
    Dir.mkdir(lib_path) unless Dir.exist?(lib_path)
    Dir.mkdir(ver_path) unless Dir.exist?(ver_path)
    puts `git clone --single-branch --branch #{branch} #{url} '#{ver_path}'`
  end
  
  if submodels.length == 0
    pod name, :path => ver_path
  else
    pod name, :path => ver_path, :subspecs => submodels
  end
  puts ""
end

核心逻辑就是我们使用pod name, :path => path,而不是pod name, :git => git,path由very_fast_include函数定义。

在函数中我们只clone自己想要的branch到path中,然后让pod去path中引入库。

这段代码放在Podfile文件开头即可。

使用方法如下:

# Podfile
# pod 'Libxxx', :git => 'git@code.xxx.com:xxx/Libxxx.git', :branch => '2.2.0'
very_fast_include('Libxxx', 'git@code.xxx.com:xxx/Libxxx.git', '2.2.0')
pod update