Class: KZ::KZRepairModuleImport
- Inherits:
-
Object
- Object
- KZ::KZRepairModuleImport
- Defined in:
- lib/cocoapods-kz/helpers/repair_module_import.rb
Instance Method Summary collapse
- #find_other_module_name(header_name, current_module_headers, sub_pods) ⇒ Object
-
#initialize(main_project) ⇒ KZRepairModuleImport
constructor
A new instance of KZRepairModuleImport.
- #repair ⇒ Object
- #repair_file(file_path, current_module_headers, sub_pods = nil) ⇒ Object
- #write_swift_file(file_path, contexts) ⇒ Object
Constructor Details
#initialize(main_project) ⇒ KZRepairModuleImport
Returns a new instance of KZRepairModuleImport.
5 6 7 8 9 10 |
# File 'lib/cocoapods-kz/helpers/repair_module_import.rb', line 5 def initialize(main_project) @main_project = main_project @all_kz_pod_targets = KZ::KZGlobalHelper.instance.kz_analyzer.all_kz_pod_targets @specify_pod_names = KZ::KZGlobalHelper.instance.specify_pod_names @header_white_list = %w[ifaddrs.h netdb.h resolv.h math.h Availability.h fcntl.h netdb.h unistd.h TargetConditionals.h] end |
Instance Method Details
#find_other_module_name(header_name, current_module_headers, sub_pods) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/cocoapods-kz/helpers/repair_module_import.rb', line 100 def find_other_module_name(header_name, current_module_headers, sub_pods) current_module_headers.each do |header_path| if header_path.basename.to_s == header_name return "" end end if sub_pods == nil sub_pods = @all_kz_pod_targets.values end sub_pods.each do |sub_kz_pod_target| sub_headers = sub_kz_pod_target.all_headers sub_headers.each do |header_path| if header_path.basename.to_s == header_name return sub_kz_pod_target.kz_module_name end end end return nil end |
#repair ⇒ Object
12 13 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 |
# File 'lib/cocoapods-kz/helpers/repair_module_import.rb', line 12 def repair if @all_kz_pod_targets.empty? KZLog.log("项目pod分析失败,请检测配置后重试", :error) return end if @specify_pod_names.count == 0 || @specify_pod_names.include?("Main") main_project_file_folder = @main_project.project_dir + @main_project.root_object.display_name main_project_files = [] main_project_headers = [] Dir.glob(File.join(main_project_file_folder, '**', '*')).each do |file| if File.file?(file) and (file.end_with?(".h") or file.end_with?(".m")) if file.end_with?(".h") main_project_headers << Pathname.new(file) main_project_files << Pathname.new(file) elsif file.end_with?(".m") main_project_files << Pathname.new(file) end end end KZLog.log("Start reair main project...", :info) main_project_files.each do |file_path| repair_file(file_path, main_project_headers) end end @all_kz_pod_targets.values.each do |kz_pod_target| next unless kz_pod_target.is_dev_pod next unless @specify_pod_names.count == 0 || (@specify_pod_names.count > 0 && @specify_pod_names.include?(kz_pod_target.name)) need_repair_files = [] kz_pod_target.native_pod_target.file_accessors.each do |file_accessor| next if file_accessor.spec.test_specification file_accessor.source_files.each do |source_file| if %w[.h .m .mm].include?(source_file.extname) need_repair_files << source_file end end end KZLog.log("Start reair '#{kz_pod_target.name}' files...", :info) need_repair_files.each do |file_path| repair_file(file_path, kz_pod_target.all_headers, kz_pod_target.recursive_dependent_targets) end end end |
#repair_file(file_path, current_module_headers, sub_pods = nil) ⇒ Object
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 |
# File 'lib/cocoapods-kz/helpers/repair_module_import.rb', line 60 def repair_file(file_path, current_module_headers, sub_pods = nil) new_header_content = [] file = File.open(file_path) contents = file.readlines file.close previous_line = "" contents.each do |line| if !line.start_with?("//") && line =~ /#import\s*["<]([a-zA-Z\+]+\.h)[">]/ heaer_name = $1 other_module_name = find_other_module_name(heaer_name, current_module_headers, sub_pods) if other_module_name if other_module_name == "" new_header_content << line else new_header_content << "#import <#{other_module_name}/#{heaer_name}>\n" end else unless previous_line.end_with?("is not included in the current pod or its subpods\n") || @header_white_list.include?(heaer_name) new_header_content << "#warning '#{heaer_name}' is not included in the current pod or its subpods\n" end new_header_content << line end else new_header_content << line end previous_line = line end write_swift_file(file_path, new_header_content) end |
#write_swift_file(file_path, contexts) ⇒ Object
92 93 94 95 96 97 98 |
# File 'lib/cocoapods-kz/helpers/repair_module_import.rb', line 92 def write_swift_file(file_path, contexts) swift_file = File.open(file_path, 'w') contexts.each do |new_line| swift_file.write(new_line) end swift_file.close end |