Class: Lhj::Command::Local

Inherits:
Lhj::Command show all
Defined in:
lib/lhj/command/local/local.rb,
lib/lhj/command/local/local_upload.rb

Direct Known Subclasses

Upload

Defined Under Namespace

Classes: Upload

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Lhj::Command

#auto_spin, #run, #stop

Constructor Details

#initialize(argv) ⇒ Local

Returns a new instance of Local.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/lhj/command/local/local.rb', line 27

def initialize(argv)
  @current_path = argv.shift_argument || Dir.pwd
  @key_col = argv.option('key-col', Lhj::LocalConfig.instance.config['csv_key_col']).to_i
  @cn_col = argv.option('cn-col', Lhj::LocalConfig.instance.config['csv_cn_col']).to_i
  @en_col = argv.option('en-col', Lhj::LocalConfig.instance.config['csv_en_col']).to_i
  @download_csv_files = argv.option('download-csv')
  @read_csv_file = argv.option('read-csv-file', Lhj::LocalConfig.instance.config['read_csv_file'])
  @gen_file_name = argv.option('gen-file', Lhj::LocalConfig.instance.config['gen_file_name'])
  @modify_source_flag = argv.flag?('modify-source', false)
  @modify_file_type = argv.option('modify-file-type', 'm,h')
  @modify_format_string = argv.option('modify-format-string', Lhj::LocalConfig.instance.config['source_format_string'])
  @key_map = {}
  super
end

Class Method Details

.optionsObject



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/lhj/command/local/local.rb', line 13

def self.options
  [
    %w[--key-col 国际化key在csv中第几列,默认为0],
    %w[--cn-col 中文在csv中第几列,默认为1],
    %w[--en-col 英文在csv中第几列,默认为2],
    %w[--download-csv 云端下载cvs的文件名],
    %w[--read-csv-file 读取csv的文件名,默认为当前目录下所有csv文件],
    %w[--gen-file 生成配置文件名,默认名为:Localizable.strings],
    %w[--modify-source 修改源码,使用国际化key代替中文字符串],
    %w[--modify-file-type 需要修改源码的文件类型,默认为m,h],
    %w[--modify-format-string 修改为国际化后的字符格式,默认为NSLocalizedString(%s,@"")]
  ]
end

Instance Method Details

#backup_csv_file(file) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/lhj/command/local/local.rb', line 97

def backup_csv_file(file)
  dest_file = bak_file(file)
  FileUtils.mkdir_p(File.dirname(dest_file)) unless File.exist?(File.dirname(dest_file))
  UI.puts "备份csv文件:#{file} 到目录#{dest_file}".green
  FileUtils.cp file, dest_file
  FileUtils.rm_rf file
end

#bak_file(file) ⇒ Object



105
106
107
108
# File 'lib/lhj/command/local/local.rb', line 105

def bak_file(file)
  dest_file = File.join(File.dirname(file), 'csv_bak', File.basename(file))
  File.exist?(dest_file) ? bak_file(dest_file) : dest_file
end

#copy_hk_to_cn_fileObject



249
250
251
252
253
254
# File 'lib/lhj/command/local/local.rb', line 249

def copy_hk_to_cn_file
  source_file = File.join(@current_path, zh_hk_dir_name, generate_file_name)
  dest_file = File.join(@current_path, zh_cn_dir_name, generate_file_name)
  FileUtils.cp source_file, dest_file
  puts "繁体中文配置覆盖简体中文配置\n"
end

#down_load_csv_fileObject



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/lhj/command/local/local.rb', line 85

def down_load_csv_file
  ary = get_download_keys
  ary.each do |key|
    file_name = File.basename(key)
    file = File.join(@current_path, file_name)
    backup_csv_file file if File.exist?(file)
    puts "下载csv文件:#{Lhj::OSS::Helper.instance.object_url(key)} 到目录#{file}\n"
    Lhj::OSS::Helper.instance.down_load(key, file)
  end
  UI.puts "下载云端csv文件完成 \n".green
end

#download_cvs_strObject



75
76
77
# File 'lib/lhj/command/local/local.rb', line 75

def download_cvs_str
  @download_csv_files || Lhj::LocalConfig.instance.config['download_csv']
end

#en_dir_nameObject



55
56
57
# File 'lib/lhj/command/local/local.rb', line 55

def en_dir_name
  Lhj::LocalConfig.instance.config['gen_en_dir']
end

#find_key_by_cn_val(file, val) ⇒ Object



203
204
205
206
207
208
209
# File 'lib/lhj/command/local/local.rb', line 203

def find_key_by_cn_val(file, val)
  file_name = File.basename(file, '.*')
  cn_key = val[2, val.length - 3]
  index = @key_map.values.find_index { |obj| cn_key.eql?(obj[:zh]) && /#{file_name}/ =~ obj[:key] }
  index ||= @key_map.values.find_index { |obj| cn_key.eql?(obj[:zh]) }
  @key_map.values[index][:key] if index
end

#format_str(type) ⇒ Object



211
212
213
214
215
216
217
# File 'lib/lhj/command/local/local.rb', line 211

def format_str(type)
  str = ''
  @key_map.each do |k, v|
    str += "\"#{k}\" = \"#{v[type]}\";\n"
  end
  str
end

#gen_zh_cn_strings_fileObject



242
243
244
245
246
247
# File 'lib/lhj/command/local/local.rb', line 242

def gen_zh_cn_strings_file
  file = File.join(@current_path, zh_cn_dir_name, generate_file_name)
  content = format_str(:cn)
  write_to_file(file, content)
  puts "生成简体中文配置完成.文件路径:#{File.absolute_path(file)}\n"
end

#generate_file(file, type) ⇒ Object



227
228
229
230
# File 'lib/lhj/command/local/local.rb', line 227

def generate_file(file, type)
  content = format_str(type)
  write_to_file(file, content)
end

#generate_file_nameObject



67
68
69
# File 'lib/lhj/command/local/local.rb', line 67

def generate_file_name
  @gen_file_name
end

#get_download_keysObject



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

def get_download_keys
  download_keys = []
  csv_files = download_cvs_str.split(/,/).map(&:strip)
  all_keys = Lhj::OSS::Helper.instance.list.map(&:key)
  csv_files.each do |f|
    arr = all_keys.select { |k| %r{^csv/} =~ k && /#{f}/ =~ k }
    if arr.count.positive?
      arr.sort! { |a, b| b.split(%r{/})[1].to_i <=> a.split(%r{/})[1].to_i }
      download_keys << arr[0]
    end
  end
  download_keys
end

#handleObject



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/lhj/command/local/local.rb', line 42

def handle
  down_load_csv_file if need_download
  read_csv
  if @key_map.keys.length.positive?
    write_en_strings
    write_zh_hk_strings
    write_zh_cn_strings
    handle_modify_source if @modify_source_flag
  else
    puts "获取中英文映射文件失败, 检查参数--read-csv-file=xx是否正常\n"
  end
end

#handle_modify_file(file) ⇒ Object



162
163
164
165
166
167
168
169
# File 'lib/lhj/command/local/local.rb', line 162

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

#handle_modify_line(file, line) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
# File 'lib/lhj/command/local/local.rb', line 191

def handle_modify_line(file, line)
  result = line
  line.scan(zh_ch_reg) do |m|
    key = find_key_by_cn_val(file, m)
    if key
      val = format(@modify_format_string, "@\"#{key}\"")
      result = result.gsub(m, val)
    end
  end
  result
end

#handle_modify_sourceObject



149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/lhj/command/local/local.rb', line 149

def handle_modify_source
  puts '修改源码开始'
  Dir.glob("#{@current_path}/**/*.{#{@modify_file_type}}").each do |f|
    # handle_modify_file f if File.stat(f).writable?
    if f =~ /Pods/
      handle_modify_file(f) if f =~ %r{Pods/ML}
    else
      handle_modify_file(f)
    end
  end
  puts '修改源码结束'
end

#modify_file_string(file) ⇒ Object



171
172
173
174
175
176
177
178
179
# File 'lib/lhj/command/local/local.rb', line 171

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

#modify_format_string(file, line) ⇒ Object



185
186
187
188
189
# File 'lib/lhj/command/local/local.rb', line 185

def modify_format_string(file, line)
  result = line
  result = handle_modify_line(file, line) if zh_ch_reg =~ line && !((/DDLog/ =~ line) || (/NSLog/ =~ line))
  result
end

#need_downloadObject



71
72
73
# File 'lib/lhj/command/local/local.rb', line 71

def need_download
  @download_csv_files || Lhj::LocalConfig.instance.config['download']
end

#read_csvObject



124
125
126
127
128
129
# File 'lib/lhj/command/local/local.rb', line 124

def read_csv
  path = File.join(@current_path, read_csv_file_name)
  Dir.glob(path).each do |f|
    read_csv_file f
  end
end

#read_csv_file(file) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/lhj/command/local/local.rb', line 131

def read_csv_file(file)
  key_c = Lhj::LocalConfig.instance.get_col_by_name(file, 'csv_key_col')
  cn_c = Lhj::LocalConfig.instance.get_col_by_name(file, 'csv_cn_col')
  en_c = Lhj::LocalConfig.instance.get_col_by_name(file, 'csv_en_col')
  trans_hk = Lhj::LocalConfig.instance.get_col_by_name(file, 'trans_zh_hk')
  trans_cn = Lhj::LocalConfig.instance.get_col_by_name(file, 'trans_zh_cn')
  CSV.foreach(file) do |row|
    if row.length > 2
      key = row[key_c]
      cn_str = row[cn_c]
      hk_str = row[cn_c]
      cn_str = Lhj::Trans::Helper.instance.trans_zh_cn_str(cn_str) if trans_cn
      hk_str = Lhj::Trans::Helper.instance.trans_zh_hk_str(hk_str) if trans_hk
      @key_map[key] = { key: key, cn: cn_str, hk: hk_str, en: row[en_c] } unless key =~ /[\u4e00-\u9fa5]/
    end
  end
end

#read_csv_file_nameObject



79
80
81
82
83
# File 'lib/lhj/command/local/local.rb', line 79

def read_csv_file_name
  file_name = @read_csv_file
  file_name = "#{@read_csv_file}.csv" unless /.csv$/ =~ @read_csv_file
  file_name
end

#write_en_stringsObject



232
233
234
235
236
# File 'lib/lhj/command/local/local.rb', line 232

def write_en_strings
  file = File.join(@current_path, en_dir_name, generate_file_name)
  generate_file(file, :en)
  puts "生成英文配置完成.文件路径:#{File.absolute_path(file)}\n"
end

#write_to_file(file, contents) ⇒ Object



219
220
221
222
223
224
225
# File 'lib/lhj/command/local/local.rb', line 219

def write_to_file(file, contents)
  FileUtils.rm_rf(file) if File.exist?(file)
  FileUtils.mkdir_p(File.dirname(file)) unless File.exist?(File.dirname(file))
  File.open(file, 'w+') do |f|
    f.write(contents)
  end
end

#write_zh_cn_stringsObject



238
239
240
# File 'lib/lhj/command/local/local.rb', line 238

def write_zh_cn_strings
  gen_zh_cn_strings_file
end

#write_zh_hk_stringsObject



256
257
258
259
260
261
# File 'lib/lhj/command/local/local.rb', line 256

def write_zh_hk_strings
  file = File.join(@current_path, zh_hk_dir_name, generate_file_name)
  content = format_str(:hk)
  write_to_file(file, content)
  puts "生成繁体中文配置完成.文件路径:#{File.absolute_path(file)}\n"
end

#zh_ch_regObject



181
182
183
# File 'lib/lhj/command/local/local.rb', line 181

def zh_ch_reg
  /@"[^"]*[\u4e00-\u9fa5]+[^"]*"/
end

#zh_cn_dir_nameObject



63
64
65
# File 'lib/lhj/command/local/local.rb', line 63

def zh_cn_dir_name
  Lhj::LocalConfig.instance.config['gen_zh_cn_dir']
end

#zh_hk_dir_nameObject



59
60
61
# File 'lib/lhj/command/local/local.rb', line 59

def zh_hk_dir_name
  Lhj::LocalConfig.instance.config['gen_zh_hk_dir']
end