Class: CBin::BuildAll::BinHelper

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Pod

match_version?

Constructor Details

#initializeBinHelper

Returns a new instance of BinHelper.



8
9
10
11
# File 'lib/cocoapods-meitu-bin/helpers/buildAll/bin_helper.rb', line 8

def initialize
  super
  @specs_str_md5_hash = Hash.new
end

Class Method Details

.xcode_versionObject



170
171
172
173
# File 'lib/cocoapods-meitu-bin/helpers/buildAll/bin_helper.rb', line 170

def self.xcode_version
  xcode_version = `xcodebuild -version`.split(' ').join('')
  xcode_version
end

Instance Method Details

#binary_version_pods_str(pod_name, specifications) ⇒ Object

获取参与二进制版本号生成的组件的tag/commit信息



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/cocoapods-meitu-bin/helpers/buildAll/bin_helper.rb', line 69

def binary_version_pods_str(pod_name,specifications)
  # 获取所有直接和间接依赖
  all_dependencies = get_all_dependencies(pod_name, specifications)
  return '' if all_dependencies.nil? || all_dependencies.empty?
  result = []
  all_dependencies.each do |pod_name|
    worktree_identity = PodUpdateConfig.get_worktree_identity(pod_name)
    if worktree_identity
      # local :path 组件优先采用 worktree identity,避免仅靠 commit/tag 导致命中旧缓存。
      result << "#{pod_name}(#{worktree_identity})"
      next
    end

    # 优先使用 get_checkout_option 中的 commit 或 tag
    checkout_option = PodUpdateConfig.get_checkout_option(pod_name)
    if checkout_option
      result << "#{pod_name}(#{checkout_option})"
      next
    end

    # 如果没有 commit 或 tag,则使用版本号
    specifications.each do |spec|
      if spec.root.name == pod_name
        version = spec.root.version.to_s
        # 如果是二进制版本,去掉二进制版本号后缀
        version_arr = version.split('.')
        if version_arr.last.include?('bin')
          version_arr.delete_at(version_arr.size - 1)
          version = version_arr.join('.')
        end
        commit = PodUpdateConfig.get_with_commit(pod_name)
        if commit
          result << "#{pod_name}(#{commit})"
          break
        end
        result << "#{pod_name}(#{version})"
        break
      end
    end
  end
  
  if ENV['p_bin_v'] == '1'
    UI.puts "参与二进制版本号生成的组件:#{result.join(',')}".yellow
  end
  
  result.join(',')
end

#bundle_identifier_strObject

捆绑标识符,参与二进制版本生成



156
157
158
# File 'lib/cocoapods-meitu-bin/helpers/buildAll/bin_helper.rb', line 156

def bundle_identifier_str
  CBin.config.bundle_identifier
end

#dep_version(dep, specifications) ⇒ Object

获取依赖库版本号



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/cocoapods-meitu-bin/helpers/buildAll/bin_helper.rb', line 243

def dep_version(dep, specifications)
  version = ''
  specifications.map do |spec|
    if spec.root.name == dep.root_name
      version = spec.root.version.to_s
      # 如果是二进制版本,去掉二进制版本号后缀
      version_arr = version.split('.')
      if version_arr.last.include?('bin')
        version_arr.delete_at(version_arr.size - 1)
        version = version_arr.join('.')
      end
      break
    end
  end
  version
end

#dependencies_commit_str(pod_name, specifications) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/cocoapods-meitu-bin/helpers/buildAll/bin_helper.rb', line 214

def dependencies_commit_str(pod_name, specifications)
  deps = []
  specifications.map do |spec|
    if spec.root.name == pod_name
      deps.concat(spec.dependencies)
    end
  end

  if deps.empty?
    UI.puts "`#{pod_name}`无依赖库".red if ENV['p_bin_v'] == '1'
    return ''
  end
  result = []
  deps.uniq.map do |dep|
    if dep.root_name == pod_name
      next
    end
    commit = find_dependency_by_name(podfile, dep.name)
    if commit
      result << "#{dep.name}(#{commit})"
    end
    # if PodUpdateConfig.external_source_commit[dep.name]
    #   result << "#{dep.name}(#{PodUpdateConfig.external_source_commit[dep.name]})"
    # end
  end
  result.join(',')
end

#dependencies_str(pod_name, specifications) ⇒ Object

将当前 Pod 库的依赖库拼接成字符串(格式:pod1_name(pod1_version),pod2_name(pod2_version),…)



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/cocoapods-meitu-bin/helpers/buildAll/bin_helper.rb', line 176

def dependencies_str(pod_name, specifications)
  deps = []

  specifications.map do |spec|
    if spec.root.name == pod_name
      deps.concat(spec.dependencies)
    end
  end

  if deps.empty?
    UI.puts "`#{pod_name}`无依赖库".red if ENV['p_bin_v'] == '1'
    return ''
  end
  result = []
  deps.uniq.map do |dep|
    if dep.root_name == pod_name
      next
    end
    version = dep_version(dep, specifications)
    # if !ENV['MEITU_USE_OPEN_COMMIT']
    #   if PodUpdateConfig.external_source_commit[dep.name]
    #     version = PodUpdateConfig.external_source_commit[dep.name]
    #   end
    # end
    commit = find_dependency_by_name(podfile, dep.name)
    version = commit unless commit.nil?
    result << "#{dep.name}(#{version})"
  end
  if ENV['p_bin_v'] == '1'
    if result.empty?
      UI.puts "`#{pod_name}`无依赖库".red
    else
      UI.puts "`#{pod_name}`依赖的库如下:".yellow
      result.map { |pod| puts "- #{pod}" }
    end
  end
  result.join(',')
end

#get_all_dependencies(pod_name, specifications) ⇒ Object

递归获取所有依赖(包括直接依赖和间接依赖)



118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/cocoapods-meitu-bin/helpers/buildAll/bin_helper.rb', line 118

def get_all_dependencies(pod_name, specifications)
  result = Set.new([pod_name])  # 添加当前 pod 到结果集
  visited = Set.new
  get_dependencies_recursively(pod_name, specifications, result, visited)
  
  if ENV['p_bin_v'] == '1'
    UI.puts "\n#{pod_name} 及其依赖:".yellow
    result.each do |dep|
      UI.puts "- #{dep}"
    end
  end
  
  result.to_a
end

#get_dependencies_recursively(pod_name, specifications, result, visited) ⇒ Object

递归获取单个组件的所有依赖



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/cocoapods-meitu-bin/helpers/buildAll/bin_helper.rb', line 134

def get_dependencies_recursively(pod_name, specifications, result, visited)
  return if visited.include?(pod_name)
  visited.add(pod_name)

  specifications.each do |spec|
    if spec.root.name == pod_name
      spec.dependencies.each do |dep|
        next if dep.root_name == pod_name # 跳过自依赖
        result.add(dep.root_name)
        get_dependencies_recursively(dep.root_name, specifications, result, visited)
      end
      break
    end
  end
end

#minimum_deployment_target_strObject

最低部署目标,参与二进制版本生成



151
152
153
# File 'lib/cocoapods-meitu-bin/helpers/buildAll/bin_helper.rb', line 151

def minimum_deployment_target_str
  CBin.config.minimum_deployment_target
end

#random_value_strObject

随机值,参与二进制版本生成



160
161
162
# File 'lib/cocoapods-meitu-bin/helpers/buildAll/bin_helper.rb', line 160

def random_value_str
  CBin.config.random_value
end

#version(pod_name, original_version, specifications, configuration = 'Debug', include_dependencies = false) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/cocoapods-meitu-bin/helpers/buildAll/bin_helper.rb', line 14

def version(pod_name, original_version, specifications, configuration = 'Debug', include_dependencies = false)
  # 有缓存从缓存中取,没有则新建
  if @specs_str_md5_hash[pod_name].nil?
    specs = specifications.map(&:name).select { |spec|
      spec.include?(pod_name) && !spec.include?('/Binary')
    }.sort!

    # 将当前 pod 自身的 commit/tag 加入版本号计算字符串
    # 修复:Podfile 中通过 :commit/:tag 指定的 pod,其自身 commit 信息必须参与 MD5 计算
    # 否则更新 Podfile 的 commit 后版本号不变,会错误命中旧的二进制缓存
    self_checkout = PodUpdateConfig.get_worktree_identity(pod_name)
    if ENV['MEITU_BIN_WORKTREE_DEBUG'] == '1' && self_checkout
      UI.puts "#{pod_name} worktree_identity:#{self_checkout}".yellow
    end

    self_checkout ||= PodUpdateConfig.get_checkout_option(pod_name)
    self_checkout ||= PodUpdateConfig.get_with_commit(pod_name)
    specs << "#{pod_name}(#{self_checkout})" if self_checkout

    version_name =  PodUpdateConfig.dependency_relationships[pod_name]
    if  version_name && version_name != ""
      specs << version_name
    else
      specs <<  binary_version_pods_str(pod_name,specifications)
    end
    specs <<  minimum_deployment_target_str
    specs <<  bundle_identifier_str
    specs <<  random_value_str
    if  ENV['xcode_version']
      specs <<  ENV['xcode_version']
    else
      specs << xcode_version
    end

    specs << (configuration.nil? ? 'Debug' : configuration)

    specs_str = specs.join('')
    if ENV['p_bin_v'] == '1'
      UI.puts "`#{pod_name}`:#{specs_str}".red
    end

    specs_str_md5 = Digest::MD5.hexdigest(specs_str)[0,6]
    # if pod_name == "MTImageEditor"
    #   puts "MTImageEditor 参与二进制版本号生成的字符串:#{specs_str}".yellow
    #   puts "MTImageEditor 参与二进制版本号生成的MD5字符串:#{ Digest::MD5.hexdigest(specs_str)}".yellow
    # end
    @specs_str_md5_hash[pod_name] = specs_str_md5
  else
    specs_str_md5 = @specs_str_md5_hash[pod_name]
  end
  "#{original_version}.bin#{specs_str_md5}"
end

#xcode_versionObject



164
165
166
167
168
# File 'lib/cocoapods-meitu-bin/helpers/buildAll/bin_helper.rb', line 164

def xcode_version
  @xcode_version ||= begin
                       `xcodebuild -version`.split(' ').join('')
                     end
end