lhj-tools 0.2.65 → 0.2.67

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: 34c0ddef5dee06417fec5c0ebee14a8b39fdd662d61f4e921a6c778ebef27c83
4
- data.tar.gz: c57667b7ab8c006bcc8bd454177892a94b1c47694f972a7340cbd81a4feb1fdd
3
+ metadata.gz: 8911a1d33976f5c886210a2fa61df8bacfe187a09b73be4e21f0442032579d80
4
+ data.tar.gz: d3db9c7d3937964ad60fe8b5234422b21343fe74222613e9941afc5a1a6cb7e2
5
5
  SHA512:
6
- metadata.gz: 6185476ee084a91d23e36f71ebfaf4f3996de0c35e97d27784fd963c9305d0026513cb610d07f6eb19076144fc2dfb3be11121f9486e06891274f2f4460083a9
7
- data.tar.gz: bbea6e7aee75a14b3ea4e1fef54b64a55f1b65338d21b59ac63eb1b8d400e711d26d473531c2ba6bacd33017cbd91a56ee78780449293a941d76f41124db2de0
6
+ metadata.gz: '05097fdb09ae5e3bede75cd76d0dc9aaf5d620ccede82384278a517530d331759fea3d722a0c6e83ac58ad4220ab3591907e6e0c47754eeb84b06062cb990d10'
7
+ data.tar.gz: 691733ddc2e757846c07b5a64be446e0ba5ef9efb6aee9075b64427d724d00dfb321c81ad8bb6e5095eb58570407a383a150beaff9632529a06d4354b52b9db6
@@ -1,28 +1,29 @@
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
+
26
27
  handle_files
27
28
  gen_csv
28
29
  # update_source_header
@@ -38,9 +39,9 @@ module Lhj
38
39
  file = File.join(@current_path, csv_file_name)
39
40
  FileUtils.rm_rf(file) if File.exist?(file)
40
41
  CSV.open(file, 'wb:utf-8') do |csv|
41
- csv << %w[国际化key 中文 英文 所在文件 文件路径]
42
+ csv << %w[国际化key 中文 英文 所在文件 文件路径 行号]
42
43
  @cn_keys.each do |k|
43
- csv << [k[:key], k[:cn], k[:en], k[:fname], k[:dirname]]
44
+ csv << [k[:key], k[:cn], k[:en], k[:fname], k[:dirname], k[:idx]]
44
45
  end
45
46
  end
46
47
  puts "生成csv文件完成.\n文件路径:#{File.absolute_path(file)}"
@@ -64,13 +65,28 @@ module Lhj
64
65
 
65
66
  def handle_file(file)
66
67
  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))
68
+ multi_comment = false
69
+ f.readlines.each_with_index do |line, idx|
70
+ multi_comment = true if line =~ %r{/\*} && line !~ %r{\*/} && !multi_comment
71
+ if line !~ %r{/\*} && line =~ %r{\*/} && multi_comment
72
+ multi_comment = false
73
+ next
74
+ end
75
+
76
+ next if multi_comment
77
+ next if line =~ %r{/\*.*\*/}
78
+ next unless zh_ch_reg =~ line
79
+ next if line =~ /#pragma/
80
+ next if line =~ /log|Log|LOG|NSCAssert/
81
+ next unless line =~ /[\u4e00-\u9fa5]/
82
+ next if line =~ %r{//.*ignore}
83
+
84
+ handle_line(file, line, idx)
69
85
  end
70
86
  end
71
87
  end
72
88
 
73
- def handle_line(file, line)
89
+ def handle_line(file, line, idx)
74
90
  line.scan(zh_ch_reg) do |str|
75
91
  fname = File.basename(file)
76
92
  dir_name = File.dirname(file)
@@ -79,7 +95,7 @@ module Lhj
79
95
  key = "#{mod_name}.#{File.basename(file, '.*')}.#{rand(36 ** 8).to_s(36)}"
80
96
  cn_str = str[2, str.length - 3]
81
97
  en_str = cn_str.gsub(/[\u4e00-\u9fa5]/, 'x')
82
- @cn_keys << { key: key, cn: cn_str, en: en_str, fname: fname, dirname: dir_name }
98
+ @cn_keys << { key: key, cn: cn_str, en: en_str, fname: fname, dirname: dir_name, idx: idx + 1 }
83
99
  end
84
100
  end
85
101
 
@@ -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文件的名称: ', String) do |q|
29
+ q.default = 'Localizable.strings'
30
+ end
31
+ @key_col = @cli.ask('中英对应照csv中key所在的列, 默认为0: ', Integer) do |q|
32
+ q.default = 0
33
+ end
34
+ @cn_col = @cli.ask('中英对应照csv<<中文>>所在的列, 默认为1: ', Integer) do |q|
35
+ q.default = 1
36
+ end
37
+ @en_col = @cli.ask('中英对应照csv<<英文>>所在的列, 默认为2: ', Integer) do |q|
38
+ q.default = 2
39
+ end
40
+ @en_dir_name = @cli.ask('生成英文.strings文件对应路径: ', String) do |q|
41
+ q.default = 'MacauLife/en.lproj'
42
+ end
43
+ @zh_hk_dir_name = @cli.ask('生成繁体.strings文件对应路径: ', String) do |q|
44
+ q.default = 'MacauLife/zh-Hant.lproj'
45
+ end
46
+ @zh_cn_dir_name = @cli.ask('生成简体.strings文件对应路径: ', 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('需要修改源码的文件类型: ', String) do |q|
52
+ q.default = 'm,h'
53
+ end
54
+ @modify_format_string = @cli.ask('修改为国际化后的字符格式: ', 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"
@@ -1,6 +1,8 @@
1
1
  require 'lhj/helper/oss_helper'
2
2
  require 'terminal-table'
3
3
 
4
+ # 添加oss后缀,参考此例子
5
+ # ?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,size_360,text_6YGl6YGl6aKG5YWI,image_bG9nbzEucG5nP3gtb3NzLXByb2Nlc3M9aW1hZ2UvcmVzaXplLFBfMzA,t_90,g_se,x_100,y_20,interval_100,align_1
4
6
  module Lhj
5
7
  class Command
6
8
  class OSS < Command
@@ -15,7 +17,7 @@ module Lhj
15
17
  查看OSS对象列表 \n
16
18
  --save 保存文件oss_key.yml与oss_url.yml到当前目录 \n
17
19
  --clear 清空控制台输出,当oss数量较多时使用,配合save使用 \n
18
- --output_suffix 添加后缀输出控制台,如?x-oss-process=image/watermark,image_YXBwbGUucG5n \n
20
+ --output_suffix 添加后缀输出控制台,如:x-oss-process=image/watermark,image_YXBwbGUucG5n \n
19
21
  --all_folder 则会添加 delimiter='/',只用于查看oss的目录结构 \n
20
22
  --prefix 添加oss的前缀过滤 \n
21
23
  --marker 参数可以指定从该位置开始list
@@ -57,7 +59,7 @@ module Lhj
57
59
 
58
60
  def save(obj_keys)
59
61
  obj_urls = obj_keys.sort { |a, b| a.slice(/\..*/) <=> b.slice(/\..*/) }.map { |k| "#{Lhj::OSS::Helper.instance.url_path}/#{k}" }
60
- obj_urls = obj_urls.map { |k| "#{k}#{@output_suffix}" } if @output_suffix
62
+ obj_urls = obj_urls.map { |k| "#{k}?#{@output_suffix}" } if @output_suffix
61
63
  FileUtils.chdir(@current_path) do
62
64
  File.write('oss_key.yml', obj_keys.to_yaml)
63
65
  File.write('oss_url.yml', obj_urls.to_yaml) unless @all_folder
@@ -71,7 +73,7 @@ module Lhj
71
73
  path = k
72
74
  else
73
75
  path = "#{i}.#{Lhj::OSS::Helper.instance.url_path}/#{k}"
74
- path = "#{path}#{@output_suffix}" if @output_suffix
76
+ path = "#{path}?#{@output_suffix}" if @output_suffix
75
77
  end
76
78
  rows << [path]
77
79
  end
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.65'
4
+ VERSION = '0.2.67'
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.65
4
+ version: 0.2.67
5
5
  platform: ruby
6
6
  authors:
7
7
  - lihaijian
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-10-12 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