Class: Lhj::Command::Fetch
- Inherits:
-
Lhj::Command
- Object
- CLAide::Command
- Lhj::Command
- Lhj::Command::Fetch
- Defined in:
- lib/lhj/command/local/fetch.rb
Class Method Summary collapse
Instance Method Summary collapse
- #csv_file_name ⇒ Object
- #file_string(file) ⇒ Object
- #format_string(file, line) ⇒ Object
- #framework_name(path) ⇒ Object
- #gen_csv ⇒ Object
- #handle ⇒ Object
- #handle_file(file) ⇒ Object
- #handle_files ⇒ Object
- #handle_line(file, line) ⇒ Object
- #handle_static_line(file, line) ⇒ Object
- #handler_file(file) ⇒ Object
-
#initialize(argv) ⇒ Fetch
constructor
A new instance of Fetch.
- #update_source_header ⇒ Object
- #zh_ch_reg ⇒ Object
Methods inherited from Lhj::Command
Constructor Details
#initialize(argv) ⇒ Fetch
Returns a new instance of Fetch.
16 17 18 19 20 21 22 23 |
# File 'lib/lhj/command/local/fetch.rb', line 16 def initialize(argv) @current_path = argv.shift_argument || Dir.pwd @file_type = argv.option('file-type', 'm,h') @file_name = argv.option('file-name', 'gen_cn_key.csv') @cn_keys = [] @key_map = {} super end |
Class Method Details
.options ⇒ Object
9 10 11 12 13 14 |
# File 'lib/lhj/command/local/fetch.rb', line 9 def self. [ %w[--file-type 从文件扩展名中查找中文字符串,默认为m,h], %w[--file-name 生成csv文件名,默认为gen_cn_key.csv] ] end |
Instance Method Details
#csv_file_name ⇒ Object
31 32 33 34 35 |
# File 'lib/lhj/command/local/fetch.rb', line 31 def csv_file_name file_name = @file_name file_name = "#{@file_name}.csv" unless /.csv$/ =~ @file_name file_name end |
#file_string(file) ⇒ Object
129 130 131 132 133 134 135 136 137 |
# File 'lib/lhj/command/local/fetch.rb', line 129 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 |
#format_string(file, line) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/lhj/command/local/fetch.rb', line 139 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
86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/lhj/command/local/fetch.rb', line 86 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
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/lhj/command/local/fetch.rb', line 37 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
25 26 27 28 29 |
# File 'lib/lhj/command/local/fetch.rb', line 25 def handle handle_files gen_csv # update_source_header end |
#handle_file(file) ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/lhj/command/local/fetch.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 && !((/DDLog/ =~ line) || (/NSLog/ =~ line)) end end end |
#handle_files ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/lhj/command/local/fetch.rb', line 49 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) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/lhj/command/local/fetch.rb', line 73 def handle_line(file, line) line.scan(zh_ch_reg) do |str| fname = File.basename(file) dir_name = File.dirname(file) mod_name = framework_name(dir_name) # ('a'..'z').to_a.shuffle[0..12].join 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 } end end |
#handle_static_line(file, line) ⇒ Object
99 100 101 102 103 104 105 |
# File 'lib/lhj/command/local/fetch.rb', line 99 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
119 120 121 122 123 124 125 126 127 |
# File 'lib/lhj/command/local/fetch.rb', line 119 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 |
#update_source_header ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/lhj/command/local/fetch.rb', line 107 def update_source_header 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
61 62 63 |
# File 'lib/lhj/command/local/fetch.rb', line 61 def zh_ch_reg /@"[^"]*[\u4e00-\u9fa5]+[^"]*"/ end |