Class: Pod::PrebuildCache::Cache
- Inherits:
-
Object
- Object
- Pod::PrebuildCache::Cache
- Defined in:
- lib/cocoapods-binary-matchup/Integration_cache.rb
Class Method Summary collapse
-
.cache_dir_for_pod(pod_name, version) ⇒ Object
获取特定pod的缓存目录.
-
.cache_exists?(pod_name, version) ⇒ Boolean
检查缓存是否存在.
-
.cache_root_dir ⇒ Object
获取缓存根目录.
-
.cache_stats ⇒ Object
获取缓存统计信息.
-
.cache_valid?(pod_name, version, source_dir = nil) ⇒ Boolean
检查缓存是否存在且有效(用于验证完整性).
-
.calculate_dir_hash(dir_path) ⇒ Object
计算目录内容的哈希值(用于验证缓存完整性).
-
.clean_expired_cache(days = 30) ⇒ Object
清理过期缓存(可选功能).
-
.copy_from_cache_if_exists(pod_name, version, target_dir) ⇒ Object
尝试从缓存拷贝到目标目录(如果缓存存在).
-
.get_pod_version(sandbox, pod_name) ⇒ Object
获取pod的版本信息,优先使用commit hash.
-
.print_cache_stats ⇒ Object
打印缓存统计信息.
-
.restore_from_cache(pod_name, version, target_dir) ⇒ Object
从缓存恢复pod.
-
.restore_from_pod_cache(pod_name, prebuild_sandbox) ⇒ Object
从 ~/.PodCache 恢复缓存到 _prebuild 目录.
-
.save_to_cache(pod_name, version, source_dir) ⇒ Object
保存到缓存(如果缓存不存在的话).
Class Method Details
.cache_dir_for_pod(pod_name, version) ⇒ Object
获取特定pod的缓存目录
16 17 18 |
# File 'lib/cocoapods-binary-matchup/Integration_cache.rb', line 16 def self.cache_dir_for_pod(pod_name, version) File.join(cache_root_dir, pod_name, version) end |
.cache_exists?(pod_name, version) ⇒ Boolean
检查缓存是否存在
201 202 203 204 |
# File 'lib/cocoapods-binary-matchup/Integration_cache.rb', line 201 def self.cache_exists?(pod_name, version) cache_dir = cache_dir_for_pod(pod_name, version) Dir.exist?(cache_dir) && !Dir.empty?(cache_dir) end |
.cache_root_dir ⇒ Object
获取缓存根目录
9 10 11 12 13 |
# File 'lib/cocoapods-binary-matchup/Integration_cache.rb', line 9 def self.cache_root_dir cache_dir = File.("~/.PodCache") FileUtils.mkdir_p(cache_dir) unless Dir.exist?(cache_dir) cache_dir end |
.cache_stats ⇒ Object
获取缓存统计信息
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 |
# File 'lib/cocoapods-binary-matchup/Integration_cache.rb', line 339 def self.cache_stats return {} unless Dir.exist?(cache_root_dir) stats = { total_pods: 0, total_versions: 0, total_size: 0 } Dir.glob("#{cache_root_dir}/*").each do |pod_dir| next unless Dir.exist?(pod_dir) stats[:total_pods] += 1 Dir.glob("#{pod_dir}/*").each do |version_dir| next unless Dir.exist?(version_dir) stats[:total_versions] += 1 # 计算目录大小 size = `du -sk "#{version_dir}" 2>/dev/null`.split.first.to_i * 1024 stats[:total_size] += size end end stats end |
.cache_valid?(pod_name, version, source_dir = nil) ⇒ Boolean
检查缓存是否存在且有效(用于验证完整性)
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/cocoapods-binary-matchup/Integration_cache.rb', line 207 def self.cache_valid?(pod_name, version, source_dir = nil) cache_dir = cache_dir_for_pod(pod_name, version) return false unless Dir.exist?(cache_dir) # 如果没有提供源目录,只检查缓存是否存在 return true if source_dir.nil? # 检查缓存的哈希文件 hash_file = File.join(cache_dir, '.cache_hash') return false unless File.exist?(hash_file) begin cached_hash = File.read(hash_file).strip current_hash = calculate_dir_hash(source_dir) return cached_hash == current_hash rescue => e Pod::UI.puts "⚠️ Warning: Cannot validate cache for #{pod_name}: #{e.message}" return false end end |
.calculate_dir_hash(dir_path) ⇒ Object
计算目录内容的哈希值(用于验证缓存完整性)
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/cocoapods-binary-matchup/Integration_cache.rb', line 183 def self.calculate_dir_hash(dir_path) return nil unless Dir.exist?(dir_path) files = Dir.glob("#{dir_path}/**/*", File::FNM_DOTMATCH).select { |f| File.file?(f) } files.sort! hasher = Digest::SHA256.new files.each do |file| # 添加文件路径和修改时间到哈希计算中 relative_path = file.sub("#{dir_path}/", "") hasher.update(relative_path) hasher.update(File.mtime(file).to_s) end hasher.hexdigest end |
.clean_expired_cache(days = 30) ⇒ Object
清理过期缓存(可选功能)
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
# File 'lib/cocoapods-binary-matchup/Integration_cache.rb', line 315 def self.clean_expired_cache(days = 30) return unless Dir.exist?(cache_root_dir) cutoff_time = Time.now - (days * 24 * 60 * 60) cleaned_count = 0 Dir.glob("#{cache_root_dir}/*/*").each do |cache_dir| next unless Dir.exist?(cache_dir) if File.mtime(cache_dir) < cutoff_time begin FileUtils.rm_rf(cache_dir) cleaned_count += 1 Pod::UI.puts "🗑️ Cleaned expired cache: #{File.basename(File.dirname(cache_dir))}/#{File.basename(cache_dir)}" rescue => e Pod::UI.puts "❌ Failed to clean cache #{cache_dir}: #{e.message}" end end end Pod::UI.puts "✅ Cleaned #{cleaned_count} expired cache entries" if cleaned_count > 0 end |
.copy_from_cache_if_exists(pod_name, version, target_dir) ⇒ Object
尝试从缓存拷贝到目标目录(如果缓存存在)
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
# File 'lib/cocoapods-binary-matchup/Integration_cache.rb', line 290 def self.copy_from_cache_if_exists(pod_name, version, target_dir) return false unless cache_exists?(pod_name, version) cache_dir = cache_dir_for_pod(pod_name, version) begin # 删除目标目录 FileUtils.rm_rf(target_dir) if Dir.exist?(target_dir) # 从缓存拷贝到目标目录 FileUtils.cp_r(cache_dir, target_dir, :remove_destination => true) # 删除缓存标记文件 hash_file = File.join(target_dir, '.cache_hash') File.delete(hash_file) if File.exist?(hash_file) Pod::UI.puts "📦 Copied #{pod_name} (#{version}) from cache" return true rescue => e Pod::UI.puts "❌ Failed to copy #{pod_name} from cache: #{e.message}" return false end end |
.get_pod_version(sandbox, pod_name) ⇒ Object
获取pod的版本信息,优先使用commit hash
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 66 67 68 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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/cocoapods-binary-matchup/Integration_cache.rb', line 21 def self.get_pod_version(sandbox, pod_name) begin Pod::UI.puts "🔍 Looking for version of: #{pod_name}" # 优先级:commit > tag > version > timestamp # 1. 首先尝试从Manifest.lock获取commit信息 manifest_lock = sandbox.root + 'Manifest.lock' if manifest_lock.exist? manifest_content = YAML.load_file(manifest_lock) # 优先检查 CHECKOUT OPTIONS 中的commit信息 = manifest_content['CHECKOUT OPTIONS'] || {} if [pod_name].is_a?(Hash) checkout_info = [pod_name] # 优先使用commit,然后是tag if checkout_info[:commit] || checkout_info['commit'] commit = checkout_info[:commit] || checkout_info['commit'] Pod::UI.puts "📋 Using commit for #{pod_name}: #{commit[0..7]}..." return commit elsif checkout_info[:tag] || checkout_info['tag'] tag = checkout_info[:tag] || checkout_info['tag'] Pod::UI.puts "📋 Using tag for #{pod_name}: #{tag}" return tag end end # 如果没有commit信息,查找版本信息 pods = manifest_content['PODS'] || [] Pod::UI.puts "🔍 Searching in #{pods.length} pod entries from Manifest.lock" pods.each do |pod_entry| if pod_entry.is_a?(Hash) # Hash 格式:{"PodName (1.0.0)" => [...]} pod_entry.each do |key, _| key_str = key.to_s Pod::UI.puts " 📦 Checking hash key: #{key_str}" # 使用更灵活的匹配逻辑,支持subspec match = key_str.match(/^#{Regexp.escape(pod_name)}\s*\(([^)]+)\)/) if match Pod::UI.puts "📋 Found exact match! Using version for #{pod_name}: #{match[1]}" return match[1] end # 如果精确匹配失败,尝试检查是否是subspec的情况 # 例如:pod_name是"GoogleUtilities/AppDelegateSwizzler",但key是"GoogleUtilities/AppDelegateSwizzler (8.1.0)" if key_str.include?(pod_name) && key_str.include?('(') version_match = key_str.match(/\(([^)]+)\)/) if version_match Pod::UI.puts "📋 Found partial match! Using version for #{pod_name}: #{version_match[1]}" return version_match[1] end end end elsif pod_entry.is_a?(String) # String 格式:"PodName (1.0.0)" Pod::UI.puts " 📦 Checking string: #{pod_entry}" match = pod_entry.match(/^#{Regexp.escape(pod_name)}\s*\(([^)]+)\)/) if match Pod::UI.puts "📋 Found exact match! Using version for #{pod_name}: #{match[1]}" return match[1] end # 如果精确匹配失败,尝试部分匹配 if pod_entry.include?(pod_name) && pod_entry.include?('(') version_match = pod_entry.match(/\(([^)]+)\)/) if version_match Pod::UI.puts "📋 Found partial match! Using version for #{pod_name}: #{version_match[1]}" return version_match[1] end end end end Pod::UI.puts "⚠️ No version found for #{pod_name} in Manifest.lock" end # 2. 如果Manifest.lock不存在或没找到,尝试从Podfile.lock获取 podfile_lock = sandbox.root.parent + 'Podfile.lock' if podfile_lock.exist? podfile_content = YAML.load_file(podfile_lock) # 同样优先检查checkout信息 = podfile_content['CHECKOUT OPTIONS'] || {} if [pod_name].is_a?(Hash) checkout_info = [pod_name] if checkout_info[:commit] || checkout_info['commit'] commit = checkout_info[:commit] || checkout_info['commit'] Pod::UI.puts "📋 Using commit for #{pod_name}: #{commit[0..7]}..." return commit elsif checkout_info[:tag] || checkout_info['tag'] tag = checkout_info[:tag] || checkout_info['tag'] Pod::UI.puts "📋 Using tag for #{pod_name}: #{tag}" return tag end end # 查找版本信息 pods = podfile_content['PODS'] || [] Pod::UI.puts "🔍 Searching in #{pods.length} pod entries from Podfile.lock" pods.each do |pod_entry| if pod_entry.is_a?(Hash) pod_entry.each do |key, _| key_str = key.to_s Pod::UI.puts " 📦 Checking hash key: #{key_str}" # 使用更灵活的匹配逻辑,支持subspec match = key_str.match(/^#{Regexp.escape(pod_name)}\s*\(([^)]+)\)/) if match Pod::UI.puts "📋 Found exact match! Using version for #{pod_name}: #{match[1]}" return match[1] end # 如果精确匹配失败,尝试部分匹配 if key_str.include?(pod_name) && key_str.include?('(') version_match = key_str.match(/\(([^)]+)\)/) if version_match Pod::UI.puts "📋 Found partial match! Using version for #{pod_name}: #{version_match[1]}" return version_match[1] end end end elsif pod_entry.is_a?(String) # String 格式:"PodName (1.0.0)" Pod::UI.puts " 📦 Checking string: #{pod_entry}" match = pod_entry.match(/^#{Regexp.escape(pod_name)}\s*\(([^)]+)\)/) if match Pod::UI.puts "📋 Found exact match! Using version for #{pod_name}: #{match[1]}" return match[1] end # 如果精确匹配失败,尝试部分匹配 if pod_entry.include?(pod_name) && pod_entry.include?('(') version_match = pod_entry.match(/\(([^)]+)\)/) if version_match Pod::UI.puts "📋 Found partial match! Using version for #{pod_name}: #{version_match[1]}" return version_match[1] end end end end Pod::UI.puts "⚠️ No version found for #{pod_name} in Podfile.lock" end # 3. 如果无法从任何lockfile获取,使用时间戳作为版本 = Time.now.strftime("%Y%m%d_%H%M%S") Pod::UI.puts "⚠️ Warning: Cannot find version/commit for #{pod_name} in lock files, using timestamp: #{timestamp}" return rescue => e = Time.now.strftime("%Y%m%d_%H%M%S") Pod::UI.puts "⚠️ Warning: Error getting version for #{pod_name}: #{e.message}, using timestamp: #{timestamp}" return end end |
.print_cache_stats ⇒ Object
打印缓存统计信息
368 369 370 371 372 373 374 375 376 |
# File 'lib/cocoapods-binary-matchup/Integration_cache.rb', line 368 def self.print_cache_stats stats = cache_stats if stats[:total_pods] > 0 size_mb = (stats[:total_size] / 1024.0 / 1024.0).round(2) Pod::UI.puts "📊 Cache Stats: #{stats[:total_pods]} pods, #{stats[:total_versions]} versions, #{size_mb} MB" else Pod::UI.puts "📊 Cache is empty" end end |
.restore_from_cache(pod_name, version, target_dir) ⇒ Object
从缓存恢复pod
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/cocoapods-binary-matchup/Integration_cache.rb', line 230 def self.restore_from_cache(pod_name, version, target_dir) cache_dir = cache_dir_for_pod(pod_name, version) return false unless Dir.exist?(cache_dir) begin # 删除目标目录 FileUtils.rm_rf(target_dir) if Dir.exist?(target_dir) # 从缓存拷贝到目标目录 FileUtils.cp_r(cache_dir, target_dir, :remove_destination => true) # 删除缓存标记文件 hash_file = File.join(target_dir, '.cache_hash') File.delete(hash_file) if File.exist?(hash_file) Pod::UI.puts "📦 Restored #{pod_name} (#{version}) from cache" return true rescue => e Pod::UI.puts "❌ Failed to restore #{pod_name} from cache: #{e.message}" return false end end |
.restore_from_pod_cache(pod_name, prebuild_sandbox) ⇒ Object
从 ~/.PodCache 恢复缓存到 _prebuild 目录
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 |
# File 'lib/cocoapods-binary-matchup/Integration_cache.rb', line 379 def self.restore_from_pod_cache(pod_name, prebuild_sandbox) begin Pod::UI.puts "🔍 Checking cache for #{pod_name} #{prebuild_sandbox}" restored_count = 0 missing_pods = [] # 获取pod版本信息 pod_version = get_pod_version(prebuild_sandbox, pod_name) # 检查缓存是否存在 if cache_exists?(pod_name, pod_version) # 从缓存复制到_prebuild目录 cache_dir = cache_dir_for_pod(pod_name, pod_version) target_dir = prebuild_sandbox.framework_folder_path_for_pod_name(pod_name) # 确保目标目录存在 target_dir.parent.mkpath unless target_dir.parent.exist? # 复制缓存到_prebuild FileUtils.cp_r(cache_dir, target_dir, :remove_destination => true) # 删除缓存标记文件 hash_file = target_dir + '.cache_hash' hash_file.delete if hash_file.exist? Pod::UI.puts "📦 Restored #{pod_name} (#{pod_version}) from cache" restored_count += 1 else missing_pods << pod_name end if missing_pods.empty? Pod::UI.puts "✅ Successfully restored all #{restored_count} pods from cache" return true else Pod::UI.puts "⚠️ Missing cache for #{missing_pods.count} pods: #{missing_pods.join(', ')}" return false end rescue => e Pod::UI.puts "❌ Error restoring from cache: #{e.message}" return false end end |
.save_to_cache(pod_name, version, source_dir) ⇒ Object
保存到缓存(如果缓存不存在的话)
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/cocoapods-binary-matchup/Integration_cache.rb', line 255 def self.save_to_cache(pod_name, version, source_dir) return unless Dir.exist?(source_dir) # 检查缓存是否已经存在,如果存在则不覆盖 if cache_exists?(pod_name, version) Pod::UI.puts "📦 Cache already exists for #{pod_name} (#{version}), skipping save" return true end cache_dir = cache_dir_for_pod(pod_name, version) begin # 创建缓存目录 FileUtils.mkdir_p(File.dirname(cache_dir)) # 删除旧缓存(如果存在) FileUtils.rm_rf(cache_dir) if Dir.exist?(cache_dir) # 拷贝到缓存 FileUtils.cp_r(source_dir, cache_dir, :remove_destination => true) # 保存哈希值 source_hash = calculate_dir_hash(source_dir) hash_file = File.join(cache_dir, '.cache_hash') File.write(hash_file, source_hash) Pod::UI.puts "💾 Saved #{pod_name} (#{version}) to cache" return true rescue => e Pod::UI.puts "❌ Failed to save #{pod_name} to cache: #{e.message}" return false end end |