Class: Lhj::Command::HeaderImport

Inherits:
Lhj::Command
  • Object
show all
Defined in:
lib/lhj/command/head_import.rb

Overview

modify header

Instance Method Summary collapse

Methods inherited from Lhj::Command

#auto_spin, #begin_title, #run, #stop

Constructor Details

#initialize(argv) ⇒ HeaderImport

Returns a new instance of HeaderImport.



12
13
14
15
16
17
18
19
# File 'lib/lhj/command/head_import.rb', line 12

def initialize(argv)
  @current_path = argv.shift_argument || Dir.pwd
  @framework = argv.option('framework')
  @header_map = {}
  @header_folder_map = {}
  @modify_files = []
  super
end

Instance Method Details

#all_header_map(folder) ⇒ Object



70
71
72
73
74
# File 'lib/lhj/command/head_import.rb', line 70

def all_header_map(folder)
  headers = Dir.glob("#{pod_folder_name}/#{folder}/**/*.h")
  headers.map! { |f| f.split('/').last }
  headers
end

#child_dirObject



62
63
64
65
66
67
68
# File 'lib/lhj/command/head_import.rb', line 62

def child_dir
  dirs = Dir.entries(pod_folder_name)
  dirs.reject! do |d|
    File.directory?(d) || /\./ =~ d || /_Prebuild/ =~ d || /Target/ =~ d || /Headers/ =~ d || /Podspecs/ =~ d || framework_black_list.any? { |f| f == d }
  end
  dirs
end

#exist_in_file(file, header) ⇒ Object



123
124
125
126
# File 'lib/lhj/command/head_import.rb', line 123

def exist_in_file(file, header)
  folder_name = @header_folder_map[header.to_sym]
  Regexp.new("/Pods/#{folder_name}") =~ File.path(file) if folder_name
end

#file_string(file) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'lib/lhj/command/head_import.rb', line 101

def file_string(file)
  str = ''
  File.open(file, 'r+') do |f|
    f.each_line do |line|
      str += format_string(f, line)
    end
  end
  str
end

#find_all_sub_folderObject



76
77
78
79
80
# File 'lib/lhj/command/head_import.rb', line 76

def find_all_sub_folder
  Find.find(@current_path).each do |f|
    handler_file f if f =~ /APPDelegate/
  end
end

#find_head_key(line) ⇒ Object



128
129
130
131
# File 'lib/lhj/command/head_import.rb', line 128

def find_head_key(line)
  header_reg = /"\D*.h"/
  line.match(header_reg)
end

#format_string(file, line) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/lhj/command/head_import.rb', line 111

def format_string(file, line)
  result = line
  if line =~ /#import/
    ma = find_head_key(line)
    if ma && !exist_in_file(file, ma[0])
      result = line.gsub(ma[0], @header_map[ma[0].to_sym] || ma[0])
      # @modify_files << file unless @modify_files.include?(file)
    end
  end
  result
end

#framework_black_listObject



48
49
50
# File 'lib/lhj/command/head_import.rb', line 48

def framework_black_list
  %w[mob_sharesdk]
end

#framework_name_mapObject



44
45
46
# File 'lib/lhj/command/head_import.rb', line 44

def framework_name_map
  { 'lottie-ios': 'Lottie', 'UITableView+FDTemplateLayoutCell': 'UITableView_FDTemplateLayoutCell', 'mob_sharesdk': 'ShareSDK' }
end

#generate_header_map@header_map

Returns:

  • (@header_map)


30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/lhj/command/head_import.rb', line 30

def generate_header_map
  framework_headers = {}
  folders = child_dir
  folders.each { |name| framework_headers[name] = all_header_map(name) }
  framework_headers.each do |key, value|
    value.each do |header|
      header_key = "\"#{header}\""
      framework_name = framework_name_map[key.to_sym] || key
      @header_map[header_key.to_sym] = "<#{framework_name}/#{header}>"
      @header_folder_map[header_key.to_sym] = key
    end
  end
end

#handleObject



21
22
23
24
25
26
27
# File 'lib/lhj/command/head_import.rb', line 21

def handle
  generate_header_map
  # say_header_map
  # find_all_sub_folder
  update_source_header
  # print_modify_info
end

#handler_file(file) ⇒ Object



92
93
94
95
96
97
98
99
# File 'lib/lhj/command/head_import.rb', line 92

def handler_file(file)
  File.chmod(0o644, file)
  str = file_string(file)
  File.open(file, 'w+') do |f|
    f.write(str)
  end
  File.chmod(0o444, file) if file =~ /Pods/
end

#pod_folder_nameObject



58
59
60
# File 'lib/lhj/command/head_import.rb', line 58

def pod_folder_name
  File.directory?("#{@current_path}/Pods") ? "#{@current_path}/Pods" : "#{@current_path}/Example/Pods"
end


133
134
135
136
137
138
139
140
141
# File 'lib/lhj/command/head_import.rb', line 133

def print_modify_info
  rows = []
  @modify_files.each do |file|
    rows << [File.basename(file), File.absolute_path(file)]
  end
  title = "修改了#{rows.count}个文件"
  table = Terminal::Table.new title: title, headings: %w[文件 路径], rows: rows
  puts table
end

#say_header_mapObject



52
53
54
55
56
# File 'lib/lhj/command/head_import.rb', line 52

def say_header_map
  @header_map.each do |key, value|
    puts "#{key}===> #{value}"
  end
end

#update_source_headerObject



82
83
84
85
86
87
88
89
90
# File 'lib/lhj/command/head_import.rb', line 82

def update_source_header
  Dir.glob("#{@current_path}/**/*.{m,h,pch}").each do |f|
    if f =~ /Pods/
      handler_file(f) if f =~ %r{Pods/ML}
    else
      handler_file(f)
    end
  end
end