Class: CBin::PodSize

Inherits:
Object
  • Object
show all
Includes:
Pod
Defined in:
lib/cocoapods-meitu-bin/helpers/pod_size_helper.rb

Constant Summary collapse

@@lock =

多线程锁

Mutex.new
@@size_threshold =

阈值,单位MB

300
@@tmp_file_path =

存放过大Pod信息的临时文件

File.join(Dir.pwd, '.mtxx_big_pods.log')
@@archive_sizes =

存放实际下载压缩包大小的哈希,键为 target_path.to_s

{}
@@archive_lock =
Mutex.new

Class Method Summary collapse

Methods included from Pod

match_version?

Class Method Details

.add_pod(pod) ⇒ Object

添加超过阈值的pod



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/cocoapods-meitu-bin/helpers/pod_size_helper.rb', line 31

def self.add_pod(pod)
  if pod[:size].to_i < @@size_threshold * 1024
    return
  end
  @@lock.synchronize do
    size = pod[:size].to_i
    size = ('%.0f' % (size / 1024)).to_f
    PodUpdateConfig.add_pod_hash(pod[:name],size)
    File.open(@@tmp_file_path, "a") do |f|
      f.write(format_pod_size(pod))
    end
  end
end

.format_pod_size(pod) ⇒ Object

格式化pod



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/cocoapods-meitu-bin/helpers/pod_size_helper.rb', line 46

def self.format_pod_size(pod)
  unit = 'KB'
  size = pod[:size].to_i
  if size >= 1024 * 1024
    unit = 'GB'
    size = ('%.1f' % (size / 1024.0 / 1024.0)).to_f
  elsif size >= 1024
    unit = 'MB'
    size = ('%.1f' % (size / 1024.0)).to_f
  end
  "#{pod[:name]}:#{size}#{unit}\n"
end

.get_archive_size(target_path) ⇒ Object

获取并移除对应 target_path 的压缩包大小



24
25
26
27
28
# File 'lib/cocoapods-meitu-bin/helpers/pod_size_helper.rb', line 24

def self.get_archive_size(target_path)
  @@archive_lock.synchronize do
    @@archive_sizes.delete(target_path.to_s)
  end
end

打印超过阈值的Pod库



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/cocoapods-meitu-bin/helpers/pod_size_helper.rb', line 60

def self.print_pods
  unless File.exist?(@@tmp_file_path)
    return
  end
  UI.puts "\n"
  UI.puts "以下Pod库下载大小大于阈值`#{@@size_threshold}MB`:".green
  File.open(@@tmp_file_path, "r") do |f|
    f.readlines.map do |line|
      UI.puts " - #{line.strip}".green
    end
  end
  # 打印完成后,删除临时文件
  FileUtils.rm_f(@@tmp_file_path) if File.exist?(@@tmp_file_path)
end

.record_archive_size(target_path, size_kb) ⇒ Object

记录压缩包实际大小



17
18
19
20
21
# File 'lib/cocoapods-meitu-bin/helpers/pod_size_helper.rb', line 17

def self.record_archive_size(target_path, size_kb)
  @@archive_lock.synchronize do
    @@archive_sizes[target_path.to_s] = size_kb
  end
end