Class: Lhj::Command::HeaderImport
Overview
Instance Method Summary
collapse
#auto_spin, #begin_title, #run, #stop
Constructor Details
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
70
71
72
73
74
|
# File 'lib/lhj/command/head_import.rb', line 70
def (folder)
= Dir.glob("#{pod_folder_name}/#{folder}/**/*.h")
.map! { |f| f.split('/').last }
end
|
#child_dir ⇒ Object
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, )
folder_name = @header_folder_map[.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_folder ⇒ Object
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)
= /"\D*.h"/
line.match()
end
|
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])
end
end
result
end
|
#framework_black_list ⇒ Object
48
49
50
|
# File 'lib/lhj/command/head_import.rb', line 48
def framework_black_list
%w[mob_sharesdk]
end
|
#framework_name_map ⇒ Object
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
|
30
31
32
33
34
35
36
37
38
39
40
41
42
|
# File 'lib/lhj/command/head_import.rb', line 30
def
= {}
folders = child_dir
folders.each { |name| [name] = (name) }
.each do |key, value|
value.each do ||
= "\"#{}\""
framework_name = framework_name_map[key.to_sym] || key
@header_map[.to_sym] = "<#{framework_name}/#{}>"
@header_folder_map[.to_sym] = key
end
end
end
|
#handle ⇒ Object
21
22
23
24
25
26
27
|
# File 'lib/lhj/command/head_import.rb', line 21
def handle
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_name ⇒ Object
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
|
#print_modify_info ⇒ Object
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
|
52
53
54
55
56
|
# File 'lib/lhj/command/head_import.rb', line 52
def
@header_map.each do |key, value|
puts "#{key}===> #{value}"
end
end
|
82
83
84
85
86
87
88
89
90
|
# File 'lib/lhj/command/head_import.rb', line 82
def
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
|