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

Instance Method Summary collapse

Methods inherited from Lhj::Command

#auto_spin, #begin_title, #run, #stop

Constructor Details

#initialize(argv) ⇒ Local

Returns a new instance of Local.



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

def initialize(argv)
  @current_path = argv.shift_argument || Dir.pwd
  @modify_file_type = 'm,h'
  @modify_format_string = 'MLLocalizedString(%s)'
  @cli = HighLine.new
  @key_map = {}
  super
end

Instance Method Details

#bak_file(file) ⇒ Object



92
93
94
95
# File 'lib/lhj/command/local/local.rb', line 92

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



252
253
254
255
256
257
# File 'lib/lhj/command/local/local.rb', line 252

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

#find_key_by_cn_val(file, val) ⇒ Object



206
207
208
209
210
211
212
# File 'lib/lhj/command/local/local.rb', line 206

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[:hk]) && /#{file_name}/ =~ obj[:key] }
  index ||= @key_map.values.find_index { |obj| cn_key.eql?(obj[:hk]) }
  @key_map.values[index][:key] if index
end

#format_str(type) ⇒ Object



214
215
216
217
218
219
220
# File 'lib/lhj/command/local/local.rb', line 214

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

#gen_zh_cn_strings_fileObject



245
246
247
248
249
250
# File 'lib/lhj/command/local/local.rb', line 245

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



230
231
232
233
# File 'lib/lhj/command/local/local.rb', line 230

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

#generate_file_nameObject



88
89
90
# File 'lib/lhj/command/local/local.rb', line 88

def generate_file_name
  @gen_file_name
end

#handleObject



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/lhj/command/local/local.rb', line 22

def handle
  setting_handle
  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 "获取中英文映射文件失败\n"
  end
end

#handle_modify_file(file) ⇒ Object



132
133
134
135
136
137
138
139
# File 'lib/lhj/command/local/local.rb', line 132

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



194
195
196
197
198
199
200
201
202
203
204
# File 'lib/lhj/command/local/local.rb', line 194

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



119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/lhj/command/local/local.rb', line 119

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



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/lhj/command/local/local.rb', line 141

def modify_file_string(file)
  str = ''
  File.open(file, 'r') do |f|
    multi_comment = false
    f.readlines.each_with_index do |line, idx|
      multi_comment = true if line =~ %r{/\*} && line !~ %r{\*/} && !multi_comment
      if line !~ %r{/\*} && line =~ %r{\*/} && multi_comment
        multi_comment = false
        str += line
        next
      end

      if multi_comment
        str += line
        next
      end
      if line =~ %r{/\*.*\*/}
        str += line
        next
      end
      if line =~ %r{^\s*//}
        str += line
        next
      end
      unless zh_ch_reg =~ line
        str += line
        next
      end
      if line =~ /#pragma/
        str += line
        next
      end
      if line =~ /log|Log|LOG|NSCAssert/
        str += line
        next
      end
      if line =~ %r{//.*ignore}
        str += line
        next
      end

      modify_line = handle_modify_line(f, line)
      str += "//#{line}" unless line.eql?(modify_line)
      str += modify_line
    end
  end
  str
end

#read_csv_file(file, quote_char, key_c, cn_c, en_c) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/lhj/command/local/local.rb', line 97

def read_csv_file(file, quote_char, key_c, cn_c, en_c)
  begin
    current_row = nil
    CSV.foreach(file, quote_char: quote_char) do |row|
      current_row = row
      if row.length > 2
        key = row[key_c]
        cn_str = row[cn_c]
        hk_str = row[cn_c]
        cn_str ||= ''
        hk_str ||= ''
        # 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 hk_str&.length&.positive?
        @key_map[key] = { key: key, cn: cn_str, hk: hk_str, en: row[en_c] } unless key =~ /[\u4e00-\u9fa5]/
      end
    end
  rescue CSV::MalformedCSVError => e
    error = "读取csv异常,文件中的第#{e.line_number}行格式有误!! \n 上一行信息#{current_row}"
    raise ArgumentError, error
  end
end

#setting_csv_handleObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/lhj/command/local/local.rb', line 34

def setting_csv_handle
  @cli.say(HighLine.color('选择中英对应照csv文件:', :green))
  path = File.join(@current_path, '**', '*.csv')
  csv_file = @cli.choose(*Dir.glob(path))
  puts "选择对照文档>>>>>>>>> #{csv_file}"
  key_col = @cli.ask('中英对应照csv中<<key>>所在的列, 默认为0: '.yellow, Integer) do |q|
    q.default = 0
  end
  cn_col = @cli.ask('中英对应照csv<<中文>>所在的列, 默认为1: '.yellow, Integer) do |q|
    q.default = 1
  end
  en_col = @cli.ask('中英对应照csv<<英文>>所在的列, 默认为2: '.yellow, Integer) do |q|
    q.default = 2
  end
  quote_char = @cli.ask('引号字符, 默认为": '.yellow, String) do |q|
    q.default = '"'
  end
  begin
    read_csv_file(csv_file, quote_char, key_col, cn_col, en_col)
  rescue ArgumentError => e
    puts e.message.red
  end
  need_add_other = @cli.ask('是否继续添加csv文件yes?: '.yellow, ->(yn) { yn.downcase[0] == 'y' }) do |q|
    q.default = false
  end
  setting_csv_handle if need_add_other
end

#setting_handleObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/lhj/command/local/local.rb', line 62

def setting_handle
  setting_csv_handle
  @gen_file_name = @cli.ask('生成.strings文件的名称: '.green, String) do |q|
    time = Time.now.strftime('%Y%m%d_%H%M')
    q.default = "Localizable_#{time}.strings"
  end
  @en_dir_name = @cli.ask('存放英文的文件夹: '.yellow, String) do |q|
    q.default = 'MacauLife/en.lproj'
  end
  @zh_hk_dir_name = @cli.ask('存放繁体的文件夹: '.yellow, String) do |q|
    q.default = 'MacauLife/zh-Hant.lproj'
  end
  @zh_cn_dir_name = @cli.ask('存放简体的文件夹: '.yellow, String) do |q|
    q.default = 'MacauLife/zh-Hans.lproj'
  end
  @modify_source_flag = @cli.agree(HighLine.color('是否要修改源码yes?:', :red))
  return unless @modify_source_flag

  @modify_file_type = @cli.ask('源码的文件类型: '.yellow, String) do |q|
    q.default = 'm,h'
  end
  @modify_format_string = @cli.ask('国际化字符格式: '.yellow, String) do |q|
    q.default = 'MLLocalizedString(%s)'
  end
end

#write_en_stringsObject



235
236
237
238
239
# File 'lib/lhj/command/local/local.rb', line 235

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



222
223
224
225
226
227
228
# File 'lib/lhj/command/local/local.rb', line 222

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



241
242
243
# File 'lib/lhj/command/local/local.rb', line 241

def write_zh_cn_strings
  gen_zh_cn_strings_file
end

#write_zh_hk_stringsObject



259
260
261
262
263
264
# File 'lib/lhj/command/local/local.rb', line 259

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



190
191
192
# File 'lib/lhj/command/local/local.rb', line 190

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