Class: Lhj::Command::Filter
Instance Method Summary
collapse
#auto_spin, #begin_title, #run, #stop
Constructor Details
#initialize(argv) ⇒ Filter
Returns a new instance of Filter.
9
10
11
12
13
14
15
16
17
|
# File 'lib/lhj/command/local/filter.rb', line 9
def initialize(argv)
@current_path = argv.shift_argument || Dir.pwd
@file_type = argv.option('file-type', 'm,h')
@file_name = argv.option('file-name', 'zh_en.csv')
@cn_keys = []
@key_map = {}
@used_keys = []
super
end
|
Instance Method Details
#csv_file_name ⇒ Object
25
26
27
28
29
|
# File 'lib/lhj/command/local/filter.rb', line 25
def csv_file_name
file_name = @file_name
file_name = "#{@file_name}.csv" unless /.csv$/ =~ @file_name
file_name
end
|
#fetch_keys ⇒ Object
55
56
57
58
59
|
# File 'lib/lhj/command/local/filter.rb', line 55
def fetch_keys
Dir.glob("#{@current_path}/**/*.{#{@file_type}}").each do |f|
handle_file f
end
end
|
#gen_csv ⇒ Object
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/lhj/command/local/filter.rb', line 43
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]]
end
end
puts "生成csv文件完成.\n文件路径:#{File.absolute_path(file)}"
end
|
#handle ⇒ Object
19
20
21
22
23
|
# File 'lib/lhj/command/local/filter.rb', line 19
def handle
fetch_keys
read_csv
gen_csv
end
|
#handle_file(file) ⇒ Object
65
66
67
68
69
70
71
|
# File 'lib/lhj/command/local/filter.rb', line 65
def handle_file(file)
File.open(file, 'r') do |f|
f.each_line do |line|
handle_line(file, line) if zh_ch_reg =~ line
end
end
end
|
#handle_line(file, line) ⇒ Object
73
74
75
76
77
78
|
# File 'lib/lhj/command/local/filter.rb', line 73
def handle_line(file, line)
line.scan(zh_ch_reg) do |str|
str[20, str.length - 22]
@used_keys << str[20, str.length - 22]
end
end
|
#read_csv ⇒ Object
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/lhj/command/local/filter.rb', line 31
def read_csv
path = File.join(@current_path, csv_file_name)
Dir.glob(path).each do |p|
CSV.foreach(p) do |row|
key = row[0]
if @used_keys.any? { |k| k.eql?(key) }
@cn_keys << { key: key, cn: row[1], en: row[2], fname: row[3], dirname: row[4] }
end
end
end
end
|
#zh_ch_reg ⇒ Object
61
62
63
|
# File 'lib/lhj/command/local/filter.rb', line 61
def zh_ch_reg
/MLLocalizedString\([^)]+\)/
end
|