Class: KZ::KZRepairModuleImport

Inherits:
Object
  • Object
show all
Defined in:
lib/cocoapods-kz/helpers/repair_module_import.rb

Instance Method Summary collapse

Constructor Details

#initialize(main_project) ⇒ KZRepairModuleImport

Returns a new instance of KZRepairModuleImport.



5
6
7
8
9
# 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
end

Instance Method Details

#find_other_module_name(header_name, current_module_headers, sub_pods) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/cocoapods-kz/helpers/repair_module_import.rb', line 90

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

#repairObject



11
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
# File 'lib/cocoapods-kz/helpers/repair_module_import.rb', line 11

def repair
  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
    puts "Start reair main project..."
    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.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

    puts "Start reair '#{kz_pod_target.name}' files..."
    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



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
# File 'lib/cocoapods-kz/helpers/repair_module_import.rb', line 54

def repair_file(file_path, current_module_headers, sub_pods = nil)
  new_header_content = []

  file = File.open(file_path)
  contents = file.readlines
  file.close

  contents.each do |line|
    if 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
        new_header_content << "#warning '#{heaer_name}' is not included in the current pod or its subpods\n"
        new_header_content << line
      end
    else
      new_header_content << line
    end
  end
  write_swift_file(file_path, new_header_content)
end

#write_swift_file(file_path, contexts) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/cocoapods-kz/helpers/repair_module_import.rb', line 82

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