lhj-tools 0.2.66 → 0.2.68

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b76b134d11c853bb88b3c9e3de8b5fd6d43b2f740299403bca0d0509bd25a8ef
4
- data.tar.gz: 36149c214675272e19a908b12567cd136babcc63ede1585385434984402b3767
3
+ metadata.gz: c23165c469fd6d71c90bdd7084b586791467fa476c5b7900bf6a6390c75c5a77
4
+ data.tar.gz: 0d2c4a52513393230ed54e001639e6bc86cc0eabcbd015ef753429785cfc5af8
5
5
  SHA512:
6
- metadata.gz: f6e5ea95f2549966fbb171d19ec366b14dc69a5f5ed69104b1e953d1ffec86e9a45a6f9f381656698d206591d92a600a240a3852b834b080046d4d0b93d93d53
7
- data.tar.gz: 7c2b0fd4686e0881cdb94005cbf5fbeca0661767e61592b9ac1e848ad61f0987f8c82de370b5118b2cf31bd380f41796557f34eb7be5e0a50933245ce229979d
6
+ metadata.gz: c7cb08beec96cd9b1ab1c92c55c99aa364531ccae929a87a0dacff18d35e10b08af4616de77034641728ab208452e2253d71fe301f50fc8eda76c1d56035c9a4
7
+ data.tar.gz: 6bbbe3a17f63e7b8431a15d9e6e2e2345ab3993e41080aa8c03c7c024708133ae733336cce2a69ef51c9f4d8c64121aac091e2400b13504602b55518ab5ccfe4
@@ -1,30 +1,33 @@
1
1
  # frozen_string_literal: true
2
2
  require 'csv'
3
+ require 'highline'
3
4
 
4
5
  module Lhj
5
6
  class Command
6
7
  class Fetch < Command
7
8
  self.summary = '提取源码的中文字符串,并生成中英文对照csv文件'
8
9
 
9
- def self.options
10
- [
11
- %w[--file-type 从文件扩展名中查找中文字符串,默认为m,h],
12
- %w[--file-name 生成csv文件名,默认为gen_cn_key.csv]
13
- ]
14
- end
15
-
16
10
  def initialize(argv)
17
11
  @current_path = argv.shift_argument || Dir.pwd
18
- @file_type = argv.option('file-type', 'm,h')
19
- @file_name = argv.option('file-name', 'gen_cn_key.csv')
20
12
  @cn_keys = []
21
13
  @key_map = {}
14
+ @cli = HighLine.new
22
15
  super
23
16
  end
24
17
 
25
18
  def handle
19
+ @file_name = @cli.ask(HighLine.color('生成csv文件名:', :green), String) do |q|
20
+ time = Time.now.strftime('%Y%m%d_%H%M')
21
+ q.default = "gen_cn_key_#{time}.csv"
22
+ end
23
+ @file_type = @cli.ask(HighLine.color('从哪些源码中查找中文字符串, 默认为m,h:', :green), String) do |q|
24
+ q.default = 'm,h'
25
+ end
26
+ @remove_duplicate = @cli.agree('是否要过滤重复中文yes?:'.green)
27
+
26
28
  handle_files
27
29
  gen_csv
30
+ gen_csv_unique if @remove_duplicate
28
31
  # update_source_header
29
32
  end
30
33
 
@@ -34,18 +37,34 @@ module Lhj
34
37
  file_name
35
38
  end
36
39
 
40
+ def unique_csv_file_name
41
+ "#{csv_file_name.split('.')[0]}_unique.csv"
42
+ end
43
+
37
44
  def gen_csv
38
45
  file = File.join(@current_path, csv_file_name)
39
46
  FileUtils.rm_rf(file) if File.exist?(file)
40
47
  CSV.open(file, 'wb:utf-8') do |csv|
41
- csv << %w[国际化key 中文 英文 所在文件 文件路径]
48
+ csv << %w[国际化key 中文 英文 所在文件 文件路径 行号]
42
49
  @cn_keys.each do |k|
43
- csv << [k[:key], k[:cn], k[:en], k[:fname], k[:dirname]]
50
+ csv << [k[:key], k[:cn], k[:en], k[:fname], k[:dirname], k[:idx]]
44
51
  end
45
52
  end
46
53
  puts "生成csv文件完成.\n文件路径:#{File.absolute_path(file)}"
47
54
  end
48
55
 
56
+ def gen_csv_unique
57
+ file = File.join(@current_path, unique_csv_file_name)
58
+ FileUtils.rm_rf(file) if File.exist?(file)
59
+ CSV.open(file, 'wb:utf-8') do |csv|
60
+ csv << %w[国际化key 中文 英文 所在文件 文件路径 行号]
61
+ @cn_keys.uniq { |k| k[:cn] }.each do |k|
62
+ csv << [k[:key], k[:cn], k[:en], k[:fname], k[:dirname], k[:idx]]
63
+ end
64
+ end
65
+ puts "生成唯一键csv文件完成.\n文件路径:#{File.absolute_path(file)}"
66
+ end
67
+
49
68
  def handle_files
50
69
  Dir.glob("#{@current_path}/**/*.{#{@file_type}}").each do |f|
51
70
  dir_name = File.dirname(f)
@@ -64,13 +83,28 @@ module Lhj
64
83
 
65
84
  def handle_file(file)
66
85
  File.open(file, 'r') do |f|
67
- f.each_line do |line|
68
- handle_line(file, line) if zh_ch_reg =~ line && !((/DDLog/ =~ line) || (/NSLog/ =~ line))
86
+ multi_comment = false
87
+ f.readlines.each_with_index do |line, idx|
88
+ multi_comment = true if line =~ %r{/\*} && line !~ %r{\*/} && !multi_comment
89
+ if line !~ %r{/\*} && line =~ %r{\*/} && multi_comment
90
+ multi_comment = false
91
+ next
92
+ end
93
+
94
+ next if multi_comment
95
+ next if line =~ %r{/\*.*\*/}
96
+ next unless zh_ch_reg =~ line
97
+ next if line =~ /#pragma/
98
+ next if line =~ /log|Log|LOG|NSCAssert/
99
+ next unless line =~ /[\u4e00-\u9fa5]/
100
+ next if line =~ %r{//.*ignore}
101
+
102
+ handle_line(file, line, idx)
69
103
  end
70
104
  end
71
105
  end
72
106
 
73
- def handle_line(file, line)
107
+ def handle_line(file, line, idx)
74
108
  line.scan(zh_ch_reg) do |str|
75
109
  fname = File.basename(file)
76
110
  dir_name = File.dirname(file)
@@ -79,7 +113,7 @@ module Lhj
79
113
  key = "#{mod_name}.#{File.basename(file, '.*')}.#{rand(36 ** 8).to_s(36)}"
80
114
  cn_str = str[2, str.length - 3]
81
115
  en_str = cn_str.gsub(/[\u4e00-\u9fa5]/, 'x')
82
- @cn_keys << { key: key, cn: cn_str, en: en_str, fname: fname, dirname: dir_name }
116
+ @cn_keys << { key: key, cn: cn_str, en: en_str, fname: fname, dirname: dir_name, idx: idx + 1 }
83
117
  end
84
118
  end
85
119
 
@@ -3,44 +3,59 @@
3
3
  require 'csv'
4
4
  require 'lhj/helper/trans_helper'
5
5
  require 'lhj/helper/oss_helper'
6
- require 'lhj/helper/local_config'
6
+ require 'highline'
7
7
 
8
8
  module Lhj
9
9
  class Command
10
10
  class Local < Command
11
- self.summary = '根据中英文对照csv文件,生成国际化配置, 及批量更新源码(使用国际化写法)'
12
-
13
- def self.options
14
- [
15
- %w[--key-col 国际化key在csv中第几列,默认为0],
16
- %w[--cn-col 中文在csv中第几列,默认为1],
17
- %w[--en-col 英文在csv中第几列,默认为2],
18
- %w[--download-csv 云端下载cvs的文件名],
19
- %w[--read-csv-file 读取csv的文件名,默认为当前目录下所有csv文件],
20
- %w[--gen-file 生成配置文件名,默认名为:Localizable.strings],
21
- %w[--modify-source 修改源码,使用国际化key代替中文字符串],
22
- %w[--modify-file-type 需要修改源码的文件类型,默认为m,h],
23
- %w[--modify-format-string 修改为国际化后的字符格式,默认为NSLocalizedString(%s,@"")]
24
- ]
25
- end
11
+ self.summary = '根据中英文对照csv文件,生成国际化配置(.strings文件)、修复源码(.m.h等)'
26
12
 
27
13
  def initialize(argv)
28
14
  @current_path = argv.shift_argument || Dir.pwd
29
- @key_col = argv.option('key-col', Lhj::LocalConfig.instance.config['csv_key_col']).to_i
30
- @cn_col = argv.option('cn-col', Lhj::LocalConfig.instance.config['csv_cn_col']).to_i
31
- @en_col = argv.option('en-col', Lhj::LocalConfig.instance.config['csv_en_col']).to_i
32
- @download_csv_files = argv.option('download-csv')
33
- @read_csv_file = argv.option('read-csv-file', Lhj::LocalConfig.instance.config['read_csv_file'])
34
- @gen_file_name = argv.option('gen-file', Lhj::LocalConfig.instance.config['gen_file_name'])
35
- @modify_source_flag = argv.flag?('modify-source', false)
36
- @modify_file_type = argv.option('modify-file-type', 'm,h')
37
- @modify_format_string = argv.option('modify-format-string', Lhj::LocalConfig.instance.config['source_format_string'])
15
+ @modify_file_type = 'm,h'
16
+ @modify_format_string = 'MLLocalizedString(%s)'
17
+ @cli = HighLine.new
38
18
  @key_map = {}
39
19
  super
40
20
  end
41
21
 
42
22
  def handle
43
- down_load_csv_file if need_download
23
+ @cli.say(HighLine.color('选择中英对应照csv文件:', :green))
24
+ path = File.join(@current_path, '**', '*.csv')
25
+ @csv_file = @cli.choose(*Dir.glob(path))
26
+ puts "选择对照文档>>>>>>>>> #{@csv_file}"
27
+
28
+ @gen_file_name = @cli.ask('生成.strings文件的名称: '.green, String) do |q|
29
+ q.default = 'Localizable.strings'
30
+ end
31
+ @key_col = @cli.ask('中英对应照csv中key所在的列, 默认为0: '.yellow, Integer) do |q|
32
+ q.default = 0
33
+ end
34
+ @cn_col = @cli.ask('中英对应照csv<<中文>>所在的列, 默认为1: '.yellow, Integer) do |q|
35
+ q.default = 1
36
+ end
37
+ @en_col = @cli.ask('中英对应照csv<<英文>>所在的列, 默认为2: '.yellow, Integer) do |q|
38
+ q.default = 2
39
+ end
40
+ @en_dir_name = @cli.ask('生成英文.strings文件对应路径: '.yellow, String) do |q|
41
+ q.default = 'MacauLife/en.lproj'
42
+ end
43
+ @zh_hk_dir_name = @cli.ask('生成繁体.strings文件对应路径: '.yellow, String) do |q|
44
+ q.default = 'MacauLife/zh-Hant.lproj'
45
+ end
46
+ @zh_cn_dir_name = @cli.ask('生成简体.strings文件对应路径: '.yellow, String) do |q|
47
+ q.default = 'MacauLife/zh-Hans.lproj'
48
+ end
49
+ @modify_source_flag = @cli.agree(HighLine.color('是否要修改源码yes?:', :red))
50
+ if @modify_source_flag
51
+ @modify_file_type = @cli.ask('需要修改源码的文件类型: '.yellow, String) do |q|
52
+ q.default = 'm,h'
53
+ end
54
+ @modify_format_string = @cli.ask('修改为国际化后的字符格式: '.yellow, String) do |q|
55
+ q.default = 'MLLocalizedString(%s)'
56
+ end
57
+ end
58
+
44
59
  read_csv
45
60
  if @key_map.keys.length.positive?
46
61
  write_en_strings
@@ -52,95 +67,32 @@ module Lhj
52
67
  end
53
68
  end
54
69
 
55
- def en_dir_name
56
- Lhj::LocalConfig.instance.config['gen_en_dir']
57
- end
58
-
59
- def zh_hk_dir_name
60
- Lhj::LocalConfig.instance.config['gen_zh_hk_dir']
61
- end
62
-
63
- def zh_cn_dir_name
64
- Lhj::LocalConfig.instance.config['gen_zh_cn_dir']
65
- end
66
-
67
70
  def generate_file_name
68
71
  @gen_file_name
69
72
  end
70
73
 
71
- def need_download
72
- @download_csv_files || Lhj::LocalConfig.instance.config['download']
73
- end
74
-
75
- def download_cvs_str
76
- @download_csv_files || Lhj::LocalConfig.instance.config['download_csv']
77
- end
78
-
79
- def read_csv_file_name
80
- file_name = @read_csv_file
81
- file_name = "#{@read_csv_file}.csv" unless /.csv$/ =~ @read_csv_file
82
- file_name
83
- end
84
-
85
- def down_load_csv_file
86
- ary = get_download_keys
87
- ary.each do |key|
88
- file_name = File.basename(key)
89
- file = File.join(@current_path, file_name)
90
- backup_csv_file file if File.exist?(file)
91
- puts "下载csv文件:#{Lhj::OSS::Helper.instance.object_url(key)} 到目录#{file}\n"
92
- Lhj::OSS::Helper.instance.down_load(key, file)
93
- end
94
- UI.puts "下载云端csv文件完成 \n".green
95
- end
96
-
97
- def backup_csv_file(file)
98
- dest_file = bak_file(file)
99
- FileUtils.mkdir_p(File.dirname(dest_file)) unless File.exist?(File.dirname(dest_file))
100
- UI.puts "备份csv文件:#{file} 到目录#{dest_file}".green
101
- FileUtils.cp file, dest_file
102
- FileUtils.rm_rf file
103
- end
104
-
105
74
  def bak_file(file)
106
75
  dest_file = File.join(File.dirname(file), 'csv_bak', File.basename(file))
107
76
  File.exist?(dest_file) ? bak_file(dest_file) : dest_file
108
77
  end
109
78
 
110
- def get_download_keys
111
- download_keys = []
112
- csv_files = download_cvs_str.split(/,/).map(&:strip)
113
- all_keys = Lhj::OSS::Helper.instance.list.map(&:key)
114
- csv_files.each do |f|
115
- arr = all_keys.select { |k| %r{^csv/} =~ k && /#{f}/ =~ k }
116
- if arr.count.positive?
117
- arr.sort! { |a, b| b.split(%r{/})[1].to_i <=> a.split(%r{/})[1].to_i }
118
- download_keys << arr[0]
119
- end
120
- end
121
- download_keys
122
- end
123
-
124
79
  def read_csv
125
- path = File.join(@current_path, read_csv_file_name)
126
- Dir.glob(path).each do |f|
127
- read_csv_file f
128
- end
80
+ read_csv_file @csv_file
129
81
  end
130
82
 
131
83
  def read_csv_file(file)
132
- key_c = Lhj::LocalConfig.instance.get_col_by_name(file, 'csv_key_col')
133
- cn_c = Lhj::LocalConfig.instance.get_col_by_name(file, 'csv_cn_col')
134
- en_c = Lhj::LocalConfig.instance.get_col_by_name(file, 'csv_en_col')
135
- trans_hk = Lhj::LocalConfig.instance.get_col_by_name(file, 'trans_zh_hk')
136
- trans_cn = Lhj::LocalConfig.instance.get_col_by_name(file, 'trans_zh_cn')
84
+ key_c = @key_col
85
+ cn_c = @cn_col
86
+ en_c = @en_col
87
+ # trans_hk = false
88
+ # trans_cn = true
137
89
  CSV.foreach(file) do |row|
138
90
  if row.length > 2
139
91
  key = row[key_c]
140
92
  cn_str = row[cn_c]
141
93
  hk_str = row[cn_c]
142
- cn_str = Lhj::Trans::Helper.instance.trans_zh_cn_str(cn_str) if trans_cn
143
- hk_str = Lhj::Trans::Helper.instance.trans_zh_hk_str(hk_str) if trans_hk
94
+ # cn_str = Lhj::Trans::Helper.instance.trans_zh_cn_str(cn_str) if trans_cn
95
+ # hk_str = Lhj::Trans::Helper.instance.trans_zh_hk_str(hk_str) if trans_hk
144
96
  @key_map[key] = { key: key, cn: cn_str, hk: hk_str, en: row[en_c] } unless key =~ /[\u4e00-\u9fa5]/
145
97
  end
146
98
  end
@@ -171,8 +123,41 @@ module Lhj
171
123
  def modify_file_string(file)
172
124
  str = ''
173
125
  File.open(file, 'r') do |f|
174
- f.each_line do |line|
175
- str += modify_format_string(f, line)
126
+ multi_comment = false
127
+ f.readlines.each_with_index do |line, idx|
128
+ multi_comment = true if line =~ %r{/\*} && line !~ %r{\*/} && !multi_comment
129
+ if line !~ %r{/\*} && line =~ %r{\*/} && multi_comment
130
+ multi_comment = false
131
+ str += line
132
+ next
133
+ end
134
+
135
+ if multi_comment
136
+ str += line
137
+ next
138
+ end
139
+ if line =~ %r{/\*.*\*/}
140
+ str += line
141
+ next
142
+ end
143
+ unless zh_ch_reg =~ line
144
+ str += line
145
+ next
146
+ end
147
+ if line =~ /#pragma/
148
+ str += line
149
+ next
150
+ end
151
+ if line =~ /log|Log|LOG|NSCAssert/
152
+ str += line
153
+ next
154
+ end
155
+ if line =~ %r{//.*ignore}
156
+ str += line
157
+ next
158
+ end
159
+
160
+ str += handle_modify_line(f, line)
176
161
  end
177
162
  end
178
163
  str
@@ -182,12 +167,6 @@ module Lhj
182
167
  /@"[^"]*[\u4e00-\u9fa5]+[^"]*"/
183
168
  end
184
169
 
185
- def modify_format_string(file, line)
186
- result = line
187
- result = handle_modify_line(file, line) if zh_ch_reg =~ line && !((/DDLog/ =~ line) || (/NSLog/ =~ line))
188
- result
189
- end
190
-
191
170
  def handle_modify_line(file, line)
192
171
  result = line
193
172
  line.scan(zh_ch_reg) do |m|
@@ -203,8 +182,8 @@ module Lhj
203
182
  def find_key_by_cn_val(file, val)
204
183
  file_name = File.basename(file, '.*')
205
184
  cn_key = val[2, val.length - 3]
206
- index = @key_map.values.find_index { |obj| cn_key.eql?(obj[:zh]) && /#{file_name}/ =~ obj[:key] }
207
- index ||= @key_map.values.find_index { |obj| cn_key.eql?(obj[:zh]) }
185
+ index = @key_map.values.find_index { |obj| cn_key.eql?(obj[:hk]) && /#{file_name}/ =~ obj[:key] }
186
+ index ||= @key_map.values.find_index { |obj| cn_key.eql?(obj[:hk]) }
208
187
  @key_map.values[index][:key] if index
209
188
  end
210
189
 
@@ -230,7 +209,7 @@ module Lhj
230
209
  end
231
210
 
232
211
  def write_en_strings
233
- file = File.join(@current_path, en_dir_name, generate_file_name)
212
+ file = File.join(@current_path, @en_dir_name, generate_file_name)
234
213
  generate_file(file, :en)
235
214
  puts "生成英文配置完成.文件路径:#{File.absolute_path(file)}\n"
236
215
  end
@@ -240,21 +219,21 @@ module Lhj
240
219
  end
241
220
 
242
221
  def gen_zh_cn_strings_file
243
- file = File.join(@current_path, zh_cn_dir_name, generate_file_name)
222
+ file = File.join(@current_path, @zh_cn_dir_name, generate_file_name)
244
223
  content = format_str(:cn)
245
224
  write_to_file(file, content)
246
225
  puts "生成简体中文配置完成.文件路径:#{File.absolute_path(file)}\n"
247
226
  end
248
227
 
249
228
  def copy_hk_to_cn_file
250
- source_file = File.join(@current_path, zh_hk_dir_name, generate_file_name)
251
- dest_file = File.join(@current_path, zh_cn_dir_name, generate_file_name)
229
+ source_file = File.join(@current_path, @zh_hk_dir_name, generate_file_name)
230
+ dest_file = File.join(@current_path, @zh_cn_dir_name, generate_file_name)
252
231
  FileUtils.cp source_file, dest_file
253
232
  puts "繁体中文配置覆盖简体中文配置\n"
254
233
  end
255
234
 
256
235
  def write_zh_hk_strings
257
- file = File.join(@current_path, zh_hk_dir_name, generate_file_name)
236
+ file = File.join(@current_path, @zh_hk_dir_name, generate_file_name)
258
237
  content = format_str(:hk)
259
238
  write_to_file(file, content)
260
239
  puts "生成繁体中文配置完成.文件路径:#{File.absolute_path(file)}\n"
data/lib/lhj/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Lhj
4
- VERSION = '0.2.66'
4
+ VERSION = '0.2.68'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lhj-tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.66
4
+ version: 0.2.68
5
5
  platform: ruby
6
6
  authors:
7
7
  - lihaijian
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-12-20 00:00:00.000000000 Z
11
+ date: 2023-12-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: xcodeproj