Class: Lhj::Command::Fetch
Instance Method Summary
collapse
#auto_spin, #begin_title, #run, #stop
Constructor Details
#initialize(argv) ⇒ Fetch
Returns a new instance of Fetch.
10
11
12
13
14
15
16
|
# File 'lib/lhj/command/local/fetch.rb', line 10
def initialize(argv)
@current_path = argv.shift_argument || Dir.pwd
@cn_keys = []
@key_map = {}
@cli = HighLine.new
super
end
|
Instance Method Details
#csv_file_name ⇒ Object
34
35
36
37
38
|
# File 'lib/lhj/command/local/fetch.rb', line 34
def csv_file_name
file_name = @file_name
file_name = "#{@file_name}.csv" unless /.csv$/ =~ @file_name
file_name
end
|
#file_string(file) ⇒ Object
164
165
166
167
168
169
170
171
172
|
# File 'lib/lhj/command/local/fetch.rb', line 164
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
|
174
175
176
177
178
179
180
181
182
183
184
|
# File 'lib/lhj/command/local/fetch.rb', line 174
def format_string(file, line)
result = line
unless /static/ =~ line
@key_map.each_key do |key|
n_key = /#{key.to_s}\s/
n_val = "#{@key_map[key]}\s"
result = result.gsub(n_key, n_val)
end
end
result
end
|
#framework_name(path) ⇒ Object
121
122
123
124
125
126
127
128
129
130
131
132
|
# File 'lib/lhj/command/local/fetch.rb', line 121
def framework_name(path)
mod_name = 'Main'
if /pods/i =~ path
ary = path.split('/')
index = ary.find_index { |p| p.eql?('Pods') }
if index
i = index + 1
mod_name = ary[i]
end
end
mod_name
end
|
#gen_csv ⇒ Object
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/lhj/command/local/fetch.rb', line 44
def gen_csv
file = File.join(@current_path, csv_file_name)
FileUtils.rm_rf(file) if File.exist?(file)
CSV.open(file, 'wb:utf-8') do |csv|
csv << %w[国际化key 中文 英文 所在文件 文件路径 行号]
@cn_keys.each do |k|
csv << [k[:key], k[:cn], k[:en], k[:fname], k[:dirname], k[:idx]]
end
end
puts "生成csv文件完成.\n文件路径:#{File.absolute_path(file)}"
end
|
#gen_csv_unique ⇒ Object
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/lhj/command/local/fetch.rb', line 56
def gen_csv_unique
file = File.join(@current_path, unique_csv_file_name)
FileUtils.rm_rf(file) if File.exist?(file)
CSV.open(file, 'wb:utf-8') do |csv|
csv << %w[国际化key 中文 英文 所在文件 文件路径 行号]
@cn_keys.uniq { |k| k[:cn] }.each do |k|
csv << [k[:key], k[:cn], k[:en], k[:fname], k[:dirname], k[:idx]]
end
end
puts "生成唯一键csv文件完成.\n文件路径:#{File.absolute_path(file)}"
end
|
#handle ⇒ Object
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/lhj/command/local/fetch.rb', line 18
def handle
@file_name = @cli.ask(HighLine.color('生成csv文件名:', :green), String) do |q|
time = Time.now.strftime('%Y%m%d_%H%M')
q.default = "gen_ch_en_#{time}.csv"
end
@file_type = @cli.ask(HighLine.color('从哪些源码中查找中文字符串, 默认为m,h:', :green), String) do |q|
q.default = 'm,h'
end
@remove_duplicate = @cli.agree('是否要过滤重复中文yes?:'.green)
handle_files
gen_csv
gen_csv_unique if @remove_duplicate
end
|
#handle_file(file) ⇒ Object
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
# File 'lib/lhj/command/local/fetch.rb', line 84
def handle_file(file)
File.open(file, 'r') do |f|
= false
f.readlines.each_with_index do |line, idx|
= true if line =~ %r{/\*} && line !~ %r{\*/} && !
if line !~ %r{/\*} && line =~ %r{\*/} &&
= false
next
end
next if
next if line =~ %r{/\*.*\*/}
next if line =~ %r{^\s*//}
next unless zh_ch_reg =~ line
next if line =~ /#pragma/
next if line =~ /log|Log|LOG|NSCAssert/
next unless line =~ /[\u4e00-\u9fa5]/
next if line =~ %r{//.*ignore}
handle_line(file, line, idx)
end
end
end
|
#handle_files ⇒ Object
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/lhj/command/local/fetch.rb', line 68
def handle_files
Dir.glob("#{@current_path}/**/*.{#{@file_type}}").each do |f|
dir_name = File.dirname(f)
if /Pods/ =~ f
mod_name = framework_name(dir_name)
handle_file f if /^ML/ =~ mod_name
else
handle_file f
end
end
end
|
#handle_line(file, line, idx) ⇒ Object
108
109
110
111
112
113
114
115
116
117
118
119
|
# File 'lib/lhj/command/local/fetch.rb', line 108
def handle_line(file, line, idx)
line.scan(zh_ch_reg) do |str|
fname = File.basename(file)
dir_name = File.dirname(file)
mod_name = framework_name(dir_name)
key = "#{mod_name}.#{File.basename(file, '.*')}.#{rand(36 ** 8).to_s(36)}"
cn_str = str[2, str.length - 3]
en_str = cn_str.gsub(/[\u4e00-\u9fa5]/, 'x')
@cn_keys << { key: key, cn: cn_str, en: en_str, fname: fname, dirname: dir_name, idx: idx + 1 }
end
end
|
#handle_static_line(file, line) ⇒ Object
134
135
136
137
138
139
140
|
# File 'lib/lhj/command/local/fetch.rb', line 134
def handle_static_line(file, line)
line.scan(zh_ch_reg) do |str|
ma = line.match(/\*.*=/)
key = ma[0][1, ma[0].length - 2].strip
@key_map[key.to_sym] = str
end
end
|
#handler_file(file) ⇒ Object
154
155
156
157
158
159
160
161
162
|
# File 'lib/lhj/command/local/fetch.rb', line 154
def handler_file(file)
puts "#{File.absolute_path(file)} \n"
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
|
#unique_csv_file_name ⇒ Object
40
41
42
|
# File 'lib/lhj/command/local/fetch.rb', line 40
def unique_csv_file_name
"#{csv_file_name.split('.')[0]}_unique.csv"
end
|
142
143
144
145
146
147
148
149
150
151
152
|
# File 'lib/lhj/command/local/fetch.rb', line 142
def
Dir.glob("#{@current_path}/**/*.{m,h}").each do |f|
if f =~ /Pods/
dir_name = File.dirname(f)
mod_name = framework_name(dir_name)
handle_file f if /^ML/ =~ mod_name
else
handler_file f
end
end
end
|
#zh_ch_reg ⇒ Object
80
81
82
|
# File 'lib/lhj/command/local/fetch.rb', line 80
def zh_ch_reg
/@"[^"]*[\u4e00-\u9fa5]+[^"]*"/
end
|