Class: Pod::Command::Bin::HeaderFilesSpecifications

Inherits:
Pod::Command::Bin show all
Defined in:
lib/cocoapods-meitu-bin/command/bin/header_files_specifications.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CBin::SpecFilesHelper

#binary_spec, #binary_spec_files, #binary_template_spec, #binary_template_spec_file, #binary_template_spec_files, #clear_binary_spec_file_if_needed, #code_spec, #code_spec_files, #create_binary_spec_file, #find_spec_file, #spec_files

Methods included from CBin::SourcesHelper

#binary_source, #code_source_list, #sources_manager, #sources_option, #valid_sources

Constructor Details

#initialize(argv) ⇒ HeaderFilesSpecifications

Returns a new instance of HeaderFilesSpecifications.



28
29
30
31
32
33
34
# File 'lib/cocoapods-meitu-bin/command/bin/header_files_specifications.rb', line 28

def initialize(argv)
  @xcodeproj_path = argv.option('xcodeproj_path', "")
  @classes_path = argv.option('classes_path', "")
  @error_del = argv.flag?('error-del', false)
  @ignore_header = argv.option('ignore-header', '')
  super
end

Class Method Details

.optionsObject



19
20
21
22
23
24
25
26
# File 'lib/cocoapods-meitu-bin/command/bin/header_files_specifications.rb', line 19

def self.options
  [
    %w[--xcodeproj-path xcodeproj路径,默认会查找podfile同级目录下的xcodeproj_path],
    %w[--classes-path 壳工程默认的代码文件路径,默认会查找podfile同级目录下的Classes目录],
    %w[--error-del 提示不规范的组件头文件引入,并删除壳工程不参与编译的文件],
    %w[--ignore-header header白名单设置,也可以在BinConfig.yml里配置,比如壳工程中的协议.h,或者纯.h文件,多个文件用;分割,例如CanvasProtocol.h;PainterProtocol.h]
  ].concat(super).uniq
end

Instance Method Details

#runObject



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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/cocoapods-meitu-bin/command/bin/header_files_specifications.rb', line 36

def run
  # 开始时间
  @start_time = Time.now.to_i
  # 读取配置文件
  read_config
  #xcodeproj_path为空字符串,默认获取当前目录下的xxx.xcodeproj
  # 获取xcodeproj_path
  if @xcodeproj_path.empty?
    files = `ls`
    file_list = files.split("\n")
    xcodeproj = file_list.find_all { |n| n.include? ".xcodeproj" }[0]
    scheme = xcodeproj.split(".")[0]
    @xcodeproj_path = File.join(Pod::Config.instance.project_root, xcodeproj)
  end

  if !@xcodeproj_path.include? ".xcodeproj"
    UI.info ".xcodeproj 路径不存在".red
    return
  end
  # 获取xcodeproj 中的 Compile Sources 实际参与编译的文件
  project = Xcodeproj::Project.open(@xcodeproj_path)
  target = project.targets.select { |a_target| a_target.name.eql?(scheme) }[0]
  files = target.source_build_phase.files
  #获取 Compile Sources 中的所有文件名称
  names = Array.new
  files.each do |reference|
    if reference.respond_to? 'file_ref' and reference.file_ref.respond_to? 'path'
      names << reference.file_ref.path
    else
      puts reference
    end
  end
  # puts names
  #过滤出 .m 和 .mm 文件名
  find_names = names.find_all { |n| (n.respond_to? 'include?' and (n.include? ".m" or n.include? ".mm")) }
  find_swift_names = names.find_all { |n| (n.respond_to? 'include?' and n.include? ".swift") }
  if @classes_path.empty?
    @classes_path = File.join(Pod::Config.instance.project_root, "Classes")
  end

  if File.exist?(@classes_path)
    files_h = `find #{@classes_path}  -name "*.h"`
    files_m = `find #{@classes_path}  -name "*.m"`
    files_swift = `find #{@classes_path}  -name "*.swift"`
    file_h_list = files_h.split("\n")
    file_m_list = files_m.split("\n")
    file_s_list = files_swift.split("\n")
    del_h_path_list = Array.new
    save_h_path_list = Array.new
    del_m_path_list = Array.new
    save_m_path_list = Array.new
    del_s_path_list = Array.new
    #获取在Compile Sources 的.h 和 本地路径下不在Compile Sources 的.h(需要删除的)
    file_h_list.each do |file_h_path|
      is_save = false
      find_names.each { |name|
        name_to = ''
        if name.include? '.mm'
          name_to = name.sub(".mm", ".h")
        elsif name.include? '.m'
          name_to = name.sub(".m", ".h")
        else
          puts "error:------#{name}"
        end

        if file_h_path.include? name_to
          is_save = true
          break
        end
      }
      if is_save
        save_h_path_list << file_h_path
      else
        del_h_path_list << file_h_path
      end
    end
    #获取在Compile Sources 的.m .mm 和 本地路径下不在Compile Sources 的.m .mm(需要删除的)
    file_m_list.each do |file_m_path|
      is_save = false
      find_names.each { |name|
        if file_m_path.include? name
          is_save = true
          break
        end
      }
      if is_save
        save_m_path_list << file_m_path
      else
        del_m_path_list << file_m_path
      end
    end
    #获取在Compile Sources 的.swift 和 本地路径下不在Compile Sources 的.swift(需要删除的)
    file_s_list.each do |file_s_path|
      is_save = false
      find_swift_names.each { |name|
        if file_s_path.include? name
          is_save = true
          break
        end
      }
      if !is_save
        del_s_path_list << file_s_path
      end
    end

    # puts "del_h_path_list"
    # puts del_h_path_list
    # puts "save_h_path_list"
    # puts save_h_path_list
    # puts "del_m_path_list"
    UI.title "提示: 壳工程不参与编译的文件,由于之前下层组件,删除引用并未删除代码文件或者其他分支又合入的不在使用或者已经下沉到组件的代码文件".green
    del_m_path_list.each { |name| UI.info "- #{name}".red }
    del_s_path_list.each { |name| UI.info "- #{name}".red }
    # puts "save_m_path_list"
    # puts save_m_path_list
    if @error_del
      del_m_path_list.each { |path|
        h_path = path.gsub(".m", ".h")
        if File.exist?(h_path)
          `rm  -rf #{h_path}`
        end
        `rm  -rf #{path}`
      }

      del_s_path_list.each { |path|
        `rm  -rf #{path}`
      }
    end

    header_path = save_h_path_list.join(sep = "#",)
    all_header_list = Array.new
    all_header_annotation_list = Array.new
    save_h_path_list.each { |h_path|
      list = `cat #{h_path} | grep '#import "' | awk -F ' ' '{print $2}'`
      list_annotation = `cat #{h_path} | grep '//#import' | awk -F ' ' '{print $2}'`
      #注释的头文件
      list_annotation.split("\n").each { |name|
        if !name.include? "-Swift.h\"" and name.include? ".h\""
          all_header_annotation_list << name.gsub("\"", "")
        end
      }
      list.split("\n").each { |name|
        if !name.include? "-Swift.h\"" and name.include? ".h\""
          all_header_list << name.gsub("\"", "")
        end
      }
    }
    save_m_path_list.each { |m_path|
      list = `cat #{m_path} | grep '#import "' | awk -F ' ' '{print $2}'`
      list_annotation = `cat #{m_path} | grep '//#import' | awk -F ' ' '{print $2}'`
      list_annotation.split("\n").each { |name|
        if !name.include? "-Swift.h\"" and name.include? ".h\""
          all_header_annotation_list << name.gsub("\"", "")
        end
      }
      list.split("\n").each { |name|
        if !name.include? "-Swift.h\"" and name.include? ".h\""
          all_header_list << name.gsub("\"", "")
        end
      }
    }

    modified_header_file_list = Array.new
    all_header_annotation_list_to = all_header_annotation_list.uniq
    all_header_list_to = all_header_list.uniq

    all_header_del_list = Array.new

    all_header_list_to.each { |name|
      is_unsave = true
      all_header_annotation_list_to.each { |name_to|
        if name == name_to
          is_unsave = false
          break
        end
      }
      if is_unsave
        all_header_del_list << name
      end
    }

    UI.title "提示: 使用方式需要调整为<>方式的头文件,请在壳工程代码搜索并修改".green
    if !@ignore_header.empty?
      @ignore_header.split(";").each { |name|
        @ignore_header_list << name
      }
    end
    @ignore_header_list.uniq
    all_header_del_list.each { |name|
      if !header_path.include? name and !@ignore_header_list.include? name
        modified_header_file_list << name
        UI.info "- #{name} 头文件不能在壳工程使用 #import \"#{name}\" 需要修改成#import <xxx/#{name}.h>".red
      end
    }

  end

  # 计算耗时
  show_cost_time
end