Class: CacheManager

Inherits:
Object
  • Object
show all
Defined in:
lib/kcache.rb

Instance Method Summary collapse

Instance Method Details

#add_cache(target, target_info) ⇒ Object



407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
# File 'lib/kcache.rb', line 407

def add_cache(target, target_info)
    target_md5 = target_info[:target_md5]
    product_dir = target_info[CONFIGURATION_BUILD_DIR]
    full_product_name = target_info[FULL_PRODUCT_NAME]

    Dir.glob("#{product_dir}/**/*.modulemap").each do | modulemap |
    modulemap_content = File.read(modulemap)
        if modulemap_content.include? File.dirname(modulemap) + "/"
            modulemap_content = modulemap_content.gsub(File.dirname(modulemap) + "/", "")
            File.write(modulemap, modulemap_content)
        end
    end

    unless full_product_name and full_product_name.size > 0 and File.exist? "#{product_dir}/#{full_product_name}"
        puts "<ERROR> #{target.name} #{product_dir}/#{full_product_name} should exist"
        return false
    end

    zip_start_time = Time.now

    command = "cd \"#{File.dirname(product_dir)}\" && tar -L -c -f #{target.name}.#{FILE_NAME_PRODUCT} #{File.basename(product_dir)}/#{full_product_name}"
    if target.product_type == "com.apple.product-type.library.static"
        command = "cd \"#{File.dirname(product_dir)}\" && tar --exclude=*.bundle --exclude=*.framework -L -c -f #{target.name}.#{FILE_NAME_PRODUCT} #{File.basename(product_dir)}"
    end
    
    unless system command
        puts "<INFO> #{command} should succeed"
        return false
    end

    already_target_cache_dirs = Dir.glob(get_cache_root + "/" + target.name + "-" + target_md5 + "-*")
    already_target_cache_dirs.each do |path|
        if File.exist? path
            raise unless system "rm -rf \"#{path}\""
        end
    end
    
    target_cache_dir = get_cache_root + "/" + target.name + "-" + target_md5 + "-" + (Time.now.to_f * 1000).to_i.to_s
    if File.exist? target_cache_dir
        puts "<INFO> #{target_cache_dir} should not exist"
        raise unless system "rm -rf \"#{target_cache_dir}\""
        return false
    end

    command = "mkdir -p \"#{target_cache_dir}\""
    unless system command
        puts "<INFO> #{command} should succeed"
        return false
    end

    cache_product_path = target_cache_dir + "/#{FILE_NAME_PRODUCT}"
    command = "mv \"#{File.dirname(product_dir)}/#{target.name}.#{FILE_NAME_PRODUCT}\" \"#{cache_product_path}\""
    unless system command
        puts "<INFO> #{command} should succeed"
        return false
    end
    unless File.exist? cache_product_path
         puts "<INFO> #{cache_product_path} should exist after mv"
        return false
    end

    target_info[:product_md5] = get_file_md5(cache_product_path)
    target_info[:build_product_dir] = target_info[CONFIGURATION_BUILD_DIR].gsub(target_info[SYMROOT] + "/", "")
    target_info[:build_intermediate_dir] = target_info[TARGET_TEMP_DIR].gsub(target_info[OBJROOT] + "/", "")
    if target_info[MODULEMAP_FILE]
        target_info[MODULEMAP_FILE] = get_content_without_pwd(target_info[MODULEMAP_FILE])
    end

    target_info = target_info.clone
    target_info.delete(:dependency_files)
    target_info.delete(:target_status)
    target_info.delete(:hit_target_cache_dir)
    target_info.delete(:target_md5_content)
    [SYMROOT, CONFIGURATION_BUILD_DIR, CONFIGURATION_TEMP_DIR, OBJROOT, TARGET_TEMP_DIR, TARGET_BUILD_DIR, PODS_XCFRAMEWORKS_BUILD_DIR, SRCROOT].each do | key |
        target_info.delete(key)
    end

    File.write(target_cache_dir + "/" + FILE_NAME_CONTEXT, target_info.to_yaml)
    
    return true

end

#backup_project(project) ⇒ Object



87
88
89
90
# File 'lib/kcache.rb', line 87

def backup_project(project)
    command = "cp \"#{project.path}/project.pbxproj\" \"#{project.path}/ysTest_backup.pbxproj\""
    raise unless system command
end

#can_cache_target(target) ⇒ Object



108
109
110
111
112
113
114
115
# File 'lib/kcache.rb', line 108

def can_cache_target(target)
    if target.product_type == "com.apple.product-type.bundle" or
        target.product_type == "com.apple.product-type.library.static" or
        target.product_type == "com.apple.product-type.framework"
        return true
    end
    return false
end

#clean_temp_filesObject



92
93
94
95
96
97
98
# File 'lib/kcache.rb', line 92

def clean_temp_files
    # command = "rm -rf Pods/*.xcodeproj/ysTest_backup.pbxproj"
    # raise unless system command

    command = "rm -rf Pods/*.xcodeproj/*.#{FILE_NAME_TARGET_CONTEXT}"
    raise unless system command
end

#copy_cacheObject



370
371
372
373
374
375
376
377
378
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
# File 'lib/kcache.rb', line 370

def copy_cache

    puts "<INFO> #{Time.now.to_f.to_s}"
    start_time = Time.now
    target_cache_dir = ARGV[1]
    cache_product_path = target_cache_dir + "/#{FILE_NAME_PRODUCT}"

    [SYMROOT, CONFIGURATION_BUILD_DIR, CONFIGURATION_TEMP_DIR, OBJROOT, TARGET_BUILD_DIR, TARGET_TEMP_DIR, SRCROOT, FULL_PRODUCT_NAME].sort.each do | key |
        unless ENV.has_key? key and ENV[key] and ENV[key].size > 0
            raise "<INFO> #{$0} should have #{key}"
            break
        end
    end

    command = "mkdir -p \"#{ENV[CONFIGURATION_BUILD_DIR]}\" && cd \"#{File.dirname(ENV[CONFIGURATION_BUILD_DIR])}/\" && tar -xf \"#{cache_product_path}\""
    raise unless system command

    if ENV[CONFIGURATION_BUILD_DIR] != ENV[TARGET_BUILD_DIR]

        command = "rm -rf \"#{ENV[TARGET_BUILD_DIR]+"/"+ENV[FULL_PRODUCT_NAME]}\""
        raise unless system command

        command = "mkdir -p \"#{File.dirname(ENV[TARGET_BUILD_DIR]+"/"+ENV[FULL_PRODUCT_NAME])}\""
        raise unless system command

        command = "mv \"#{ENV[CONFIGURATION_BUILD_DIR]+"/"+ENV[FULL_PRODUCT_NAME]}\" \"#{ENV[TARGET_BUILD_DIR]+"/"+ENV[FULL_PRODUCT_NAME]}\""
        raise unless system command

        command = "/bin/ln -sfh \"#{ENV[TARGET_BUILD_DIR]+"/"+ENV[FULL_PRODUCT_NAME]}\" \"#{ENV[CONFIGURATION_BUILD_DIR]+"/"+ENV[FULL_PRODUCT_NAME]}\""
        raise unless system command
    end 

    puts "<INFO> duration = #{((Time.now - start_time)*1000).to_i} ms in copy cache action"
    puts "<INFO> #{Time.now.to_f.to_s}"
end

#generate_target_all_infomation(project, target, source_files) ⇒ Object



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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/kcache.rb', line 184

def generate_target_all_infomation(project, target, source_files)

    if $podfile_spec_checksums == nil and File.exist? "Podfile.lock"
        podfile_lock = YAML.load(File.read("Podfile.lock"))
        $podfile_spec_checksums = podfile_lock["SPEC CHECKSUMS"]
    end

    project_configuration = project.build_configurations.detect { | config | config.name == Target_Build_Configuration}
    project_configuration_content = project_configuration.pretty_print.to_yaml

    project_xcconfig = ""
    if project_configuration.base_configuration_reference
        config_file_path = project_configuration.base_configuration_reference.real_path
        if File.exist? config_file_path
            project_xcconfig = File.read(config_file_path).lines.reject{|line|line.include? "_SEARCH_PATHS"}.sort.join("")
        end
    end

    target_configuration = target.build_configurations.detect { | config | config.name == Target_Build_Configuration}
    target_configuration_content = target_configuration.pretty_print.to_yaml

    target_xcconfig = ""
    if target_configuration.base_configuration_reference
        config_file_path = target_configuration.base_configuration_reference.real_path
        if File.exist? config_file_path
            target_xcconfig = File.read(config_file_path).lines.reject{|line|line.include? "_SEARCH_PATHS"}.sort.join("")
        end
    end


    files_configuration = []
    build_phases = []
    build_phases.push target.source_build_phase if target.methods.include? :source_build_phase
    build_phases.push target.resources_build_phase if target.methods.include? :resources_build_phase
    build_phases.each do | build_phase |
        target.source_build_phase.files_references.each do | files_reference |
            files_reference.build_files.each do |build_file|
                if build_file.settings and build_file.settings.class == Hash
                    files_configuration.push File.basename(build_file.file_ref.real_path.to_s) + "\n" + build_file.settings.to_yaml
                end
            end
        end
    end
    files_configuration = files_configuration.sort.uniq.join("\n")


    source_md5_list = []
    has_found_checksum = false
    split_parts = target.name.split("-")
    split_parts.each_with_index do | part, index |
        spec_name = split_parts[0..index].join("-")
        if $podfile_spec_checksums.has_key? spec_name
            source_md5_list.push "SPEC CHECKSUM : #{spec_name} #{$podfile_spec_checksums[spec_name]}"
            has_found_checksum = true
        end
    end

    if has_found_checksum
        if target.name.start_with? "AK" or target.name.start_with? "EUR"

            source_md5_list.push "Project : #{File.basename(project.path)}"
            source_md5_list.push "Project configuration : "
            source_md5_list.push project_configuration_content.strip
            source_md5_list.push "Project xcconfig : "
            source_md5_list.push project_xcconfig.strip

            source_md5_list.push "Target : #{target.name}, #{target.product_type}"
            source_md5_list.push "Target configuration : "
            source_md5_list.push target_configuration_content.strip
            source_md5_list.push "Target xcconfig : "
            source_md5_list.push target_xcconfig.strip
            source_md5_list.push "Files settings : "
            source_md5_list.push files_configuration.strip

            source_md5_list.push "Files MD5 : "
            source_files.uniq.sort.each do | file |
                path =  get_content_without_pwd(file)
                if target.name == "AKULocalizedStrings" or target.name == "EURLocalizedStrings"
                    if file.include? ".swift"
                        source_md5_list.push path + " : " + get_file_md5(path)
                    end
                else
                    source_md5_list.push path + " : " + get_file_md5(path)
                end
            end
        end
        source_md5_content = source_md5_list.join("\n")
        return source_md5_content

    end
end

#get_cache_rootObject



45
46
47
# File 'lib/kcache.rb', line 45

def get_cache_root
    return Dir.home + "/ysCache"
end

#get_content_without_pwd(content) ⇒ Object



49
50
51
52
# File 'lib/kcache.rb', line 49

def get_content_without_pwd(content)
    content = content.gsub("#{Dir.pwd}/", "").gsub(/#{Dir.pwd}(\W|$)/, '\1')
    return content
end

#get_custom_config_informationObject



490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
# File 'lib/kcache.rb', line 490

def get_custom_config_information
    if File.exist? "project_cache_config.yml"
        custom_config = YAML.load (File.read("project_cache_config.yml"))
        $exclude_target = custom_config[:exclude_targets]
    else
        source = {
            :description => nil,
            :dependency_check => true,
            :exclude_targets => ["target1", "target2"],
        }.to_yaml

        substitution_list = {
            /:description:/ => "# You can assign values to these parameters to achieve some custom functions\n",
            /:dependency_check:/ => "\n\n# Whether to check the dependencies of the target. For exzample: target A depends on target B. Under normal circumstances, target B is recompiled, then target A will be recompiled, but if the dependencies are not checked, target A will not be recompiled at this time\n:dependency_check:",
            /:exclude_targets:/ => "\n\n# the targets that will not participate in cache dependency detection\n:exclude_targets:",
        }

        substitution_list.each do |pattern, replacement|
          source.gsub!(pattern, replacement)
        end
        puts source
        File.write("project_cache_config.yml", source)
    end

end

#get_file_md5(file) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/kcache.rb', line 54

def get_file_md5(file)
    if $file_md5_hash.has_key? file
        return $file_md5_hash[file]
    end
    md5 = Digest::MD5.hexdigest(File.read(file))
    $file_md5_hash[file] = md5
    return md5
end

#get_projectsObject



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/kcache.rb', line 64

def get_projects
    pods_project = Xcodeproj::Project.open("Pods/Pods.xcodeproj")
    super_project_paths = get_super_project(pods_project)
    super_projects = []
    super_project_paths.each do | path |
        next if path.end_with? "Pods/Pods.xcodeproj"
        project = Xcodeproj::Project.open(path)
        super_projects.push project
    end
    return (super_projects + [pods_project])
end

#get_super_project(project) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/kcache.rb', line 76

def get_super_project(project)
    wrapper_projects = project.files.select{|file|file.last_known_file_type=="wrapper.pb-project"}
    wrapper_project_paths = []
    wrapper_projects.each do | wrapper_project_file |
        wrapper_project_file_path = wrapper_project_file.real_path.to_s
        wrapper_project_paths.push wrapper_project_file_path
    end
    return wrapper_project_paths.uniq
end

#get_target_cache(target, target_md5) ⇒ Object



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/kcache.rb', line 278

def get_target_cache(target, target_md5)

    dependency_start_time = Time.now

    target_cache_dirs = Dir.glob(get_cache_root + "/" + target.name + "-" + target_md5 + "-*")

    hit_results = []

    target_cache_dirs.each do |target_cache_dir|
        unless File.exist? target_cache_dir + "/" + FILE_NAME_PRODUCT
            puts "<ERROR> #{target.name} target cache dir missed files: #{target_cache_dir}"
            next
        end
        unless File.exist? target_cache_dir + "/" + FILE_NAME_CONTEXT
            puts "<ERROR> #{target.name} target cache dir missed files: #{target_cache_dir}"
            next
        end

        target_context = YAML.load(File.read(target_cache_dir + "/" + FILE_NAME_CONTEXT))

        if target_context[:target_md5] != target_md5 or target_context[:product_md5] != get_file_md5(target_cache_dir + "/" + FILE_NAME_PRODUCT)
            command = "rm -rf \"#{target_cache_dir}\""
            raise unless system command
            puts "<ERROR> #{target.name} target md5 does not match:  #{target_cache_dir}"
        end

        dependency_exit = true
        
        if target_context[:dependency_files_md5] 
            target_context[:dependency_files_md5].each do | item |
                dependency_file = item[0]
                dependency_md5 = item[1]
                unless File.exist? dependency_file
                    puts "<WARNING> #{target.name} dependency file miss: #{dependency_file}"
                    dependency_exit = false
                    break
                end
                unless get_file_md5(dependency_file) == dependency_md5
                    puts "<WARNING> #{target.name} dependency file md5 does not match: #{dependency_file}"
                    dependency_exit = false
                    break
                end
            end
        end

        if dependency_exit
            hit_results.push target_cache_dir
        end
    end

    return hit_results

end

#get_target_dependency_files(target, intermediate_dir, product_dir, xcframeworks_build_dir, src_path) ⇒ Object



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
# File 'lib/kcache.rb', line 117

def get_target_dependency_files(target, intermediate_dir, product_dir, xcframeworks_build_dir, src_path)
    dependency_files = []
    Dir.glob("#{intermediate_dir}/**/*.d").each do | dependency_file |
        content = File.read(dependency_file)
        files = content.scan(/(?:\S(?:\\ )*)+/).flatten.uniq
        files = files - ["dependencies:", "\\", ":"]
        files.each do | file |
            file = file.gsub("\\ ", " ")
            if file.start_with? target.name
                file = src_path + "/" + file
            end
            unless File.exist? file
                puts "<ERROR> #{target.name} #{file} should exist in dependency file #{dependency_file}"
                return []
            end

            if file.start_with? intermediate_dir + "/" or 
                file.start_with? product_dir + "/"
                next
            end

            if xcframeworks_build_dir and xcframeworks_build_dir.size > 0 and file.start_with? xcframeworks_build_dir + "/"
                next
            end
            dependency_files.push file
        end
    end
    
    return dependency_files.uniq
end

#get_target_source_files(target) ⇒ Object



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
181
# File 'lib/kcache.rb', line 149

def get_target_source_files(target)
    files = []
    #获取所有可执行文件(.m .swift .cpp .mm)
    target.source_build_phase.files.each do | file |
        file_path = file.file_ref.real_path.to_s
        files.push file_path
    end

    #获取所有头文件(.h)
    target.headers_build_phase.files.each do | file |
        file_path = file.file_ref.real_path.to_s
        files.push file_path
    end
    #获取所有资源文件(.png,.strings .json .html)
    target.resources_build_phase.files.each do | file |
        file_path = file.file_ref.real_path.to_s
        files.push file_path
    end
    expand_files = []
    files.uniq.each do | file |
        next unless File.exist? file
        if File.file? file
            expand_files.push file
        else
            Find.find(file).each do | file_in_dir |
                if File.file? file_in_dir
                    expand_files.push file_in_dir
                end
            end
        end
    end
    return expand_files.uniq
end

#inject_copy_action(project, target, target_context) ⇒ Object



355
356
357
358
359
360
361
362
363
364
365
366
367
368
# File 'lib/kcache.rb', line 355

def inject_copy_action(project, target, target_context)
    target_cache_dir = target_context[:hit_target_cache_dir]

    target.build_phases.delete_if { | build_phase | 
        build_phase.class == Xcodeproj::Project::Object::PBXHeadersBuildPhase or 
        build_phase.class == Xcodeproj::Project::Object::PBXSourcesBuildPhase or 
        build_phase.class == Xcodeproj::Project::Object::PBXResourcesBuildPhase
    }

    command_exec = "\"#{$0}\""        
    inject_phase = target.new_shell_script_build_phase("ys_copy_#{target.name}")
    inject_phase.shell_script = "#{command_exec} #{"copy"} \"#{target_cache_dir}\""
    inject_phase.show_env_vars_in_log = '1'
end

#inject_flag_action(project, target) ⇒ Object



348
349
350
351
352
353
# File 'lib/kcache.rb', line 348

def inject_flag_action(project, target)
    command_exec = "\"#{$0}\""        
    inject_phase = target.new_shell_script_build_phase("ys_inject_#{target.name}")
    inject_phase.shell_script = "#{command_exec} #{"inject"} #{target.name} \"#{project.path}\""
    inject_phase.show_env_vars_in_log = '1'
end

#project_task_beginObject



517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
# File 'lib/kcache.rb', line 517

def project_task_begin

    projects = get_projects
    total_count = 0
    hit_count = 0
    miss_count = 0
    error_count = 0
    hit_target_md5_cache_set = Set.new
    miss_target_cache_set = Set.new

    get_custom_config_information

    pre_targets_info = {}

    projects.each do |project|
        restore_project(project)
        backup_project(project)
    end

    clean_temp_files 
    
    projects = get_projects

    projects.each do |project|
        project.native_targets.each do |target|
            if can_cache_target(target)
                total_count = total_count + 1
                source_files = get_target_source_files(target)
                target_md5_content = generate_target_all_infomation(project, target, source_files)
                unless target_md5_content
                    puts "<ERROR> target md5 content can not generate: #{target.name}"
                    error_count = error_count + 1
                    next
                end

                target_md5 = Digest::MD5.hexdigest(target_md5_content)
                hit_target_cache_dirs = get_target_cache(target, target_md5)
                target_info = {}
                target_info[:target_md5] = target_md5

                if hit_target_cache_dirs.count == 0
                    puts "<INFO> #{target.name} #{target_md5} does not hit any cache"
                    target_info[:target_status] = CACHE_STATUS_MISS
                    inject_flag_action(project, target)
                    miss_count = miss_count + 1
                    File.write("#{project.path}/#{target.name}.#{FILE_NAME_TARGET_CONTEXT}", target_info.to_yaml)
                else
                    target_info[:hit_target_cache_dir] = hit_target_cache_dirs
                    hit_target_md5_cache_set.add "#{target.name}-#{target_info[:target_md5]}"
                end
                pre_targets_info[target] = target_info
            end
        end
    end
      
    while true
        projects.each do |project|
            project.native_targets.each do |target|
                target_info = pre_targets_info[target]
                next unless target_info and target_info[:target_status] != CACHE_STATUS_MISS
                next unless target_info and target_info[:target_status] != CACHE_STATUS_HIT
                hit_target_cache_dirs = target_info[:hit_target_cache_dir]
                next unless hit_target_cache_dirs and hit_target_cache_dirs.count > 0
                target_md5 = target_info[:target_md5]
                
                target_really_hit_dir = ""
                target_really_miss = false
                hit_target_cache_dirs.each do |hit_target_cache_dir|
                    all_dependency_target_exist = true
                    target_really_miss = false
                    hit_targets_info = YAML.load(File.read(hit_target_cache_dir + "/" + FILE_NAME_CONTEXT))
                    if hit_targets_info[:dependency_targets_md5]
                        hit_targets_info[:dependency_targets_md5].each do | item |
                            dependency_target = item[0]
                            dependency_target_md5 = item[1]
                            if $exclude_target.include? dependency_target 
                                puts "<INFO> #{target.name} #{target_md5} hit cache, dependency target #{dependency_target}-#{dependency_target_md5} skip check"
                                next
                            end
                            
                            if miss_target_cache_set.include? "#{dependency_target}"
                                puts "<INFO> #{target.name} #{target_md5} hit cache, but dependency target #{dependency_target}-#{dependency_target_md5} does not hit"
                                target_really_miss = true
                                all_dependency_target_exist = false
                                break
                            end

                            unless hit_target_md5_cache_set.include? "#{dependency_target}-#{dependency_target_md5}"
                                puts "<INFO> #{target.name} #{target_md5} hit cache, but dependency target #{dependency_target}-#{dependency_target_md5} does not hit"
                                all_dependency_target_exist = false
                                target_really_miss = true
                                break
                            end
                        end
                        
                        if all_dependency_target_exist
                            target_really_hit_dir = hit_target_cache_dir
                            break
                        end

                        if target_really_miss
                            break
                        end
                    end
                end
    
                if target_really_hit_dir.length > 0
                    puts "<INFO> #{target.name} #{target_md5} hit cache"
                    hit_count = hit_count + 1
                    hit_target_info = YAML.load(File.read(target_really_hit_dir + "/" + FILE_NAME_CONTEXT))
                    target_info = target_info.merge!(hit_target_info)
                    target_info[:target_status] = CACHE_STATUS_HIT
                    target_info[:hit_target_cache_dir] = target_really_hit_dir
                    inject_copy_action(project, target, target_info)
                end

                if target_really_miss 
                    miss_count = miss_count + 1
                    miss_target_cache_set.add "#{target.name}"
                    hit_target_md5_cache_set.delete "#{target.name}-#{target_info[:target_md5]}"
                    target_info[:target_status] = CACHE_STATUS_MISS
                    target_info.delete(:hit_target_cache_dir)
                    inject_flag_action(project, target)
                end

                File.write("#{project.path}/#{target.name}.#{FILE_NAME_TARGET_CONTEXT}", target_info.to_yaml)
            end
            project.save
        end
        if total_count == hit_count + miss_count + error_count
            break
        else
            puts "<INFO> continue to find hit cache"
        end
    end
    puts "<INFO> total count: #{total_count}, hit count: #{hit_count}, miss_count: #{miss_count}, error_count: #{error_count}"
end

#project_task_endObject



656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
# File 'lib/kcache.rb', line 656

def project_task_end
    
    projects = get_projects
    post_targets_context = {}
    total_add_count = 0

    projects.each do |project|
        project.native_targets.each do | target |
            target_context_file = "#{project.path}/#{target.name}.#{FILE_NAME_TARGET_CONTEXT}"
            next unless File.exist? target_context_file
            if can_cache_target(target)
                target_info = YAML.load(File.read(target_context_file))
                if target_info[:target_status] == CACHE_STATUS_READY
                    # 保证编译前后MD5不变
                    source_files = get_target_source_files(target)
                    target_md5_content = generate_target_all_infomation(project, target, source_files)
                    target_md5 = Digest::MD5.hexdigest(target_md5_content)
                    unless target_info[:target_md5] == target_md5
                        puts "<ERROR> #{target.name} md5 should not be changed after build"
                        next
                    end
    
                    target_info[:target_md5_content] = target_md5_content
    
                    product_dir = target_info[CONFIGURATION_BUILD_DIR]
                    intermediate_dir = target_info[TARGET_TEMP_DIR].to_s
                    xcframeworks_build_dir = target_info[PODS_XCFRAMEWORKS_BUILD_DIR]
                    src_path = target_info[SRCROOT]
    
                    unless intermediate_dir.length > 0
                        puts "<ERROR> #{target.name} should have intermediate_dir"
                        next
                    end
                    dependency_files = get_target_dependency_files(target, intermediate_dir, product_dir, xcframeworks_build_dir, src_path)
                    if source_files.size > 0 and dependency_files.size == 0 and target.product_type != "com.apple.product-type.bundle"
                        puts "<ERROR> #{target.name} should have dependency files"
                        next
                    end
                    target_info[:dependency_files] = dependency_files - source_files
    
                    module_file_path = target_info[MODULEMAP_FILE]
    
                    if src_path and src_path.size > 0 and module_file_path and module_file_path.size > 0
                        if File.exist? Dir.pwd + "/" + get_content_without_pwd("#{src_path}/#{module_file_path}")
                            target_info[MODULEMAP_FILE] = get_content_without_pwd("#{src_path}/#{module_file_path}")
                        else
                            puts "<ERROR> #{target.name} #{module_file_path} should be supported"
                            next
                        end
                    end
                elsif target_info[:target_status] == CACHE_STATUS_HIT
                    module_file_path = target_info[MODULEMAP_FILE]
                    if module_file_path and module_file_path.size > 0
                        if not File.exist? Dir.pwd + "/" + target_info[MODULEMAP_FILE]
                            puts "<ERROR> #{target.name} #{module_file_path} should be supported"
                            next
                        end
                    end
                end
                post_targets_context[target] = target_info
    
            end
        end
    end

    projects.each do |project|
        project.native_targets.each do | target |
            if post_targets_context.has_key? target
                target_info = post_targets_context[target]
                next unless target_info[:target_status] == CACHE_STATUS_READY
              
                dependency_targets_set = Set.new
                implicit_dependencies = []
                
                post_targets_context.each do | other_target, other_target_context |
                    next if target == other_target
                    next if target.product_type == "com.apple.product-type.bundle"
                    next if other_target.product_type == "com.apple.product-type.bundle"
    
                    configuration_build_dir = other_target_context[CONFIGURATION_BUILD_DIR]
                    target_temp_dir = other_target_context[TARGET_TEMP_DIR]
                    build_product_dir = other_target_context[:build_product_dir]
                    build_intermediate_dir = other_target_context[:build_intermediate_dir]
    
                    target_info[:dependency_files].each do | dependency |
                        if configuration_build_dir and configuration_build_dir.size > 0 and dependency.start_with? configuration_build_dir + "/"
                            dependency_targets_set.add other_target
                            implicit_dependencies.push dependency
                        end
    
                        if target_temp_dir and target_temp_dir.size > 0 and dependency.start_with? target_temp_dir + "/"
                            dependency_targets_set.add other_target
                            implicit_dependencies.push dependency
                        end
    
                        if build_product_dir and build_product_dir.size > 0 and dependency.start_with? target_info[SYMROOT] + "/" + build_product_dir + "/"
                            dependency_targets_set.add other_target
                            implicit_dependencies.push dependency
                        end
    
                        if build_intermediate_dir and build_intermediate_dir.size > 0 and dependency.start_with? target_info[OBJROOT] + "/" + build_intermediate_dir + "/"
                            dependency_targets_set.add other_target
                            implicit_dependencies.push dependency
                        end
    
                    end
                    target_info[:dependency_files] = target_info[:dependency_files] - implicit_dependencies
    
                end
                target_info[:dependency_files] = target_info[:dependency_files] - implicit_dependencies
    
                dependency_files_md5 = []
    
                should_cache = true
                if target_info and target_info[:dependency_files].size > 0
                    target_info[:dependency_files].each do | file |
                        if file.start_with? target_info[OBJROOT] + "/" or file.start_with? target_info[SYMROOT] + "/"
                            puts "#{target.name} #{file} dependecy should not include build path"
                            should_cache = false
                            break
                        end
                        dependency_files_md5.push [get_content_without_pwd(file), get_file_md5(file)]
                    end
                end
                next unless should_cache
    
                target_info[:dependency_files_md5] = dependency_files_md5.sort.uniq
                dependency_targets_md5 = dependency_targets_set.to_a.map { | target |  [target.name, post_targets_context[target][:target_md5]]}
                target_info[:dependency_targets_md5] = dependency_targets_md5
    
                if add_cache(target, target_info)
                    puts  "<INFO> #{target} is being added to cache dir"
                    total_add_count = total_add_count + 1
                    target.build_phases.delete_if { |phase|
                        phase.class == Xcodeproj::Project::Object::PBXShellScriptBuildPhase and phase.name.include? "ys_"
                    }
                end
    
            end
        end
        restore_project(project)
    end

    puts "<INFO> total add cache count: #{total_add_count}"
end

#restore_project(project) ⇒ Object



100
101
102
103
104
105
# File 'lib/kcache.rb', line 100

def restore_project(project)
    if File.exist? "#{project.path}/ysTest_backup.pbxproj"
        command = "mv \"#{project.path}/ysTest_backup.pbxproj\" \"#{project.path}/project.pbxproj\""
        raise unless system command
    end
end

#set_prama_to_yamlObject



332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/kcache.rb', line 332

def set_prama_to_yaml
    target_name = ARGV[1]
    project_path = ARGV[2]
    if File.exist? "#{project_path}/#{target_name}.#{FILE_NAME_TARGET_CONTEXT}" 
        target_context = YAML.load(File.read("#{project_path}/#{target_name}.#{FILE_NAME_TARGET_CONTEXT}"))

        [SYMROOT, OBJROOT, SRCROOT, CONFIGURATION_BUILD_DIR, CONFIGURATION_TEMP_DIR, TARGET_BUILD_DIR, TARGET_TEMP_DIR, PODS_XCFRAMEWORKS_BUILD_DIR, MODULEMAP_FILE, FULL_PRODUCT_NAME].sort.each do | key |
            if ENV[key]
                target_context[key] = ENV[key] 
            end
        end
        target_context[:target_status] = CACHE_STATUS_READY
        File.write("#{project_path}/#{target_name}.#{FILE_NAME_TARGET_CONTEXT}", target_context.to_yaml)
    end
end