lhj-tools 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/lhj/command/head_import.rb +128 -0
- data/lib/lhj/command/init.rb +27 -0
- data/lib/lhj/command/local/fetch.rb +152 -0
- data/lib/lhj/command/local/filter.rb +82 -0
- data/lib/lhj/command/local/local.rb +264 -0
- data/lib/lhj/command/local/local_upload.rb +44 -0
- data/lib/lhj/command/local/micro_service.rb +87 -0
- data/lib/lhj/command/oss/del.rb +36 -0
- data/lib/lhj/command/oss/list.rb +17 -0
- data/lib/lhj/command/oss/upload.rb +58 -0
- data/lib/lhj/command/refactor_rename.rb +77 -0
- data/lib/lhj/command/rename_image.rb +1 -1
- data/lib/lhj/command/reverse_import.rb +74 -0
- data/lib/lhj/command/template.rb +21 -0
- data/lib/lhj/command/trans.rb +47 -6
- data/lib/lhj/command.rb +11 -0
- data/lib/lhj/helper/local_config.rb +40 -0
- data/lib/lhj/helper/oss_config.rb +32 -0
- data/lib/lhj/helper/oss_helper.rb +43 -0
- data/lib/lhj/helper/trans_helper.rb +49 -0
- data/lib/lhj/tools/version.rb +1 -1
- metadata +33 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f2210e536172e4943d1e39f4460cd715ef726e35b0859c348b24963a1f0ff433
|
4
|
+
data.tar.gz: c1e81cf0c82016ee20f57168c840780cd7127a28a1c2c92d409785f03a52dbe5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8cd06e57308f22d90b13d330f21a6c198d873a5483ae604c044e3c0fbd76c8083550fb261e30fc40f43b8e1af8f891063b0ba7a7930a258de8cf1f8292855802
|
7
|
+
data.tar.gz: 2428331f080d48dac6ae877c179a900ddb2601a32c292062d2edbfa7febb1a202dda40fd7fe21f6d840dffad0335695ad1c306c6ea9903a99bd2895cead8edc4
|
@@ -0,0 +1,128 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'find'
|
4
|
+
require 'fileutils'
|
5
|
+
|
6
|
+
module Lhj
|
7
|
+
class Command
|
8
|
+
class Import < Command
|
9
|
+
self.summary = '更改头文件引入'
|
10
|
+
|
11
|
+
def initialize(argv)
|
12
|
+
@current_path = argv.shift_argument || Dir.pwd
|
13
|
+
@framework = argv.option('framework')
|
14
|
+
@header_map = {}
|
15
|
+
@header_folder_map = {}
|
16
|
+
super
|
17
|
+
end
|
18
|
+
|
19
|
+
def run
|
20
|
+
generate_header_map
|
21
|
+
# say_header_map
|
22
|
+
# find_all_sub_folder
|
23
|
+
update_source_header
|
24
|
+
end
|
25
|
+
|
26
|
+
# @return [@header_map]
|
27
|
+
def generate_header_map
|
28
|
+
framework_headers = {}
|
29
|
+
folders = child_dir
|
30
|
+
folders.each { |name| framework_headers[name] = all_header_map(name) }
|
31
|
+
framework_headers.each do |key, value|
|
32
|
+
value.each do |header|
|
33
|
+
header_key = "\"#{header}\""
|
34
|
+
framework_name = framework_name_map[key.to_sym] || key
|
35
|
+
@header_map[header_key.to_sym] = "<#{framework_name}/#{header}>"
|
36
|
+
@header_folder_map[header_key.to_sym] = key
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def framework_name_map
|
42
|
+
{ 'lottie-ios': 'Lottie', 'UITableView+FDTemplateLayoutCell': 'UITableView_FDTemplateLayoutCell', 'mob_sharesdk': 'ShareSDK' }
|
43
|
+
end
|
44
|
+
|
45
|
+
def framework_black_list
|
46
|
+
%w[mob_sharesdk]
|
47
|
+
end
|
48
|
+
|
49
|
+
def say_header_map
|
50
|
+
@header_map.each do |key, value|
|
51
|
+
puts "#{key}===> #{value}"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def pod_folder_name
|
56
|
+
File.directory?("#{@current_path}/Pods") ? "#{@current_path}/Pods" : "#{@current_path}/Example/Pods"
|
57
|
+
end
|
58
|
+
|
59
|
+
def child_dir
|
60
|
+
dirs = Dir.entries(pod_folder_name)
|
61
|
+
dirs.reject! do |d|
|
62
|
+
File.directory?(d) || /\./ =~ d || /_Prebuild/ =~ d || /Target/ =~ d || /Headers/ =~ d || /Podspecs/ =~ d || framework_black_list.any? { |f| f == d }
|
63
|
+
end
|
64
|
+
dirs
|
65
|
+
end
|
66
|
+
|
67
|
+
def all_header_map(folder)
|
68
|
+
headers = Dir.glob("#{pod_folder_name}/#{folder}/**/*.h")
|
69
|
+
headers.map! { |f| f.split('/').last }
|
70
|
+
headers
|
71
|
+
end
|
72
|
+
|
73
|
+
def find_all_sub_folder
|
74
|
+
Find.find(@current_path).each do |f|
|
75
|
+
handler_file f if f =~ /APPDelegate/
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def update_source_header
|
80
|
+
Dir.glob("#{@current_path}/**/*.{m,h,pch}").each do |f|
|
81
|
+
if f =~ /Pods/
|
82
|
+
handler_file(f) if f =~ %r{Pods/ML}
|
83
|
+
else
|
84
|
+
handler_file(f)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def handler_file(file)
|
90
|
+
File.chmod(0o644, file)
|
91
|
+
str = file_string(file)
|
92
|
+
File.open(file, 'w+') do |f|
|
93
|
+
f.write(str)
|
94
|
+
end
|
95
|
+
File.chmod(0o444, file) if file =~ /Pods/
|
96
|
+
end
|
97
|
+
|
98
|
+
def file_string(file)
|
99
|
+
str = ''
|
100
|
+
File.open(file, 'r+') do |f|
|
101
|
+
f.each_line do |line|
|
102
|
+
str += format_string(f, line)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
str
|
106
|
+
end
|
107
|
+
|
108
|
+
def format_string(file, line)
|
109
|
+
result = line
|
110
|
+
if line =~ /#import/
|
111
|
+
ma = find_head_key(line)
|
112
|
+
result = line.gsub(ma[0], @header_map[ma[0].to_sym] || ma[0]) if ma && !exist_in_file(file, ma[0])
|
113
|
+
end
|
114
|
+
result
|
115
|
+
end
|
116
|
+
|
117
|
+
def exist_in_file(file, header)
|
118
|
+
folder_name = @header_folder_map[header.to_sym]
|
119
|
+
Regexp.new("/Pods/#{folder_name}") =~ File.path(file) if folder_name
|
120
|
+
end
|
121
|
+
|
122
|
+
def find_head_key(line)
|
123
|
+
header_reg = /"\D*.h"/
|
124
|
+
line.match(header_reg)
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
|
2
|
+
module Lhj
|
3
|
+
class Command
|
4
|
+
# init tools
|
5
|
+
class Init < Command
|
6
|
+
self.summary = '初始化控件'
|
7
|
+
|
8
|
+
def run
|
9
|
+
p 'hello world'
|
10
|
+
end
|
11
|
+
|
12
|
+
def down_load_urls
|
13
|
+
%w[http://aomi-ios-repo.oss-cn-shenzhen.aliyuncs.com/localizable_config.yml
|
14
|
+
http://aomi-ios-repo.oss-cn-shenzhen.aliyuncs.com/oss_config.yml
|
15
|
+
http://aomi-ios-repo.oss-cn-shenzhen.aliyuncs.com/yapi.yml
|
16
|
+
http://aomi-ios-repo.oss-cn-shenzhen.aliyuncs.com/zh2hant.yml]
|
17
|
+
end
|
18
|
+
|
19
|
+
def down_load_file
|
20
|
+
ary = down_load_urls
|
21
|
+
ary.each do |key|
|
22
|
+
end
|
23
|
+
puts "同步完成 \n"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,152 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'csv'
|
3
|
+
|
4
|
+
module Lhj
|
5
|
+
class Command
|
6
|
+
class Fetch < Command
|
7
|
+
self.summary = '提取源码的中文字符串,并生成中英文对照csv文件'
|
8
|
+
|
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
|
+
def initialize(argv)
|
17
|
+
@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
|
+
@cn_keys = []
|
21
|
+
@key_map = {}
|
22
|
+
super
|
23
|
+
end
|
24
|
+
|
25
|
+
def run
|
26
|
+
handle_files
|
27
|
+
gen_csv
|
28
|
+
# update_source_header
|
29
|
+
end
|
30
|
+
|
31
|
+
def csv_file_name
|
32
|
+
file_name = @file_name
|
33
|
+
file_name = "#{@file_name}.csv" unless /.csv$/ =~ @file_name
|
34
|
+
file_name
|
35
|
+
end
|
36
|
+
|
37
|
+
def gen_csv
|
38
|
+
file = File.join(@current_path, csv_file_name)
|
39
|
+
FileUtils.rm_rf(file) if File.exist?(file)
|
40
|
+
CSV.open(file, 'wb:utf-8') do |csv|
|
41
|
+
csv << %w[国际化key 中文 英文 所在文件 文件路径]
|
42
|
+
@cn_keys.each do |k|
|
43
|
+
csv << [k[:key], k[:cn], k[:en], k[:fname], k[:dirname]]
|
44
|
+
end
|
45
|
+
end
|
46
|
+
puts "生成csv文件完成.\n文件路径:#{File.absolute_path(file)}"
|
47
|
+
end
|
48
|
+
|
49
|
+
def handle_files
|
50
|
+
Dir.glob("#{@current_path}/**/*.{#{@file_type}}").each do |f|
|
51
|
+
dir_name = File.dirname(f)
|
52
|
+
if /Pods/ =~ f
|
53
|
+
mod_name = framework_name(dir_name)
|
54
|
+
handle_file f if /^ML/ =~ mod_name
|
55
|
+
else
|
56
|
+
handle_file f
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def zh_ch_reg
|
62
|
+
/@"[^"]*[\u4e00-\u9fa5]+[^"]*"/
|
63
|
+
end
|
64
|
+
|
65
|
+
def handle_file(file)
|
66
|
+
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))
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def handle_line(file, line)
|
74
|
+
line.scan(zh_ch_reg) do |str|
|
75
|
+
fname = File.basename(file)
|
76
|
+
dir_name = File.dirname(file)
|
77
|
+
mod_name = framework_name(dir_name)
|
78
|
+
# ('a'..'z').to_a.shuffle[0..12].join
|
79
|
+
key = "#{mod_name}.#{File.basename(file, '.*')}.#{rand(36 ** 8).to_s(36)}"
|
80
|
+
cn_str = str[2, str.length - 3]
|
81
|
+
en_str = cn_str.gsub(/[\u4e00-\u9fa5]/, 'x')
|
82
|
+
@cn_keys << { key: key, cn: cn_str, en: en_str, fname: fname, dirname: dir_name }
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def framework_name(path)
|
87
|
+
mod_name = 'Main'
|
88
|
+
if /pods/i =~ path
|
89
|
+
ary = path.split('/')
|
90
|
+
index = ary.find_index { |p| p.eql?('Pods') }
|
91
|
+
if index
|
92
|
+
i = index + 1
|
93
|
+
mod_name = ary[i]
|
94
|
+
end
|
95
|
+
end
|
96
|
+
mod_name
|
97
|
+
end
|
98
|
+
|
99
|
+
def handle_static_line(file, line)
|
100
|
+
line.scan(zh_ch_reg) do |str|
|
101
|
+
ma = line.match(/\*.*=/)
|
102
|
+
key = ma[0][1, ma[0].length - 2].strip
|
103
|
+
@key_map[key.to_sym] = str
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def update_source_header
|
108
|
+
Dir.glob("#{@current_path}/**/*.{m,h}").each do |f|
|
109
|
+
if f =~ /Pods/
|
110
|
+
dir_name = File.dirname(f)
|
111
|
+
mod_name = framework_name(dir_name)
|
112
|
+
handle_file f if /^ML/ =~ mod_name
|
113
|
+
else
|
114
|
+
handler_file f
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
def handler_file(file)
|
120
|
+
puts "#{File.absolute_path(file)} \n"
|
121
|
+
File.chmod(0o644, file)
|
122
|
+
str = file_string(file)
|
123
|
+
File.open(file, 'w+') do |f|
|
124
|
+
f.write(str)
|
125
|
+
end
|
126
|
+
File.chmod(0o444, file) if file =~ /Pods/
|
127
|
+
end
|
128
|
+
|
129
|
+
def file_string(file)
|
130
|
+
str = ''
|
131
|
+
File.open(file, 'r+') do |f|
|
132
|
+
f.each_line do |line|
|
133
|
+
str += format_string(f, line)
|
134
|
+
end
|
135
|
+
end
|
136
|
+
str
|
137
|
+
end
|
138
|
+
|
139
|
+
def format_string(file, line)
|
140
|
+
result = line
|
141
|
+
unless /static/ =~ line
|
142
|
+
@key_map.each_key do |key|
|
143
|
+
n_key = /#{key.to_s}\s/
|
144
|
+
n_val = "#{@key_map[key]}\s"
|
145
|
+
result = result.gsub(n_key, n_val)
|
146
|
+
end
|
147
|
+
end
|
148
|
+
result
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'csv'
|
3
|
+
|
4
|
+
module Lhj
|
5
|
+
class Command
|
6
|
+
class Filter < Command
|
7
|
+
self.summary = '过滤重复对象'
|
8
|
+
|
9
|
+
def initialize(argv)
|
10
|
+
@current_path = argv.shift_argument || Dir.pwd
|
11
|
+
@file_type = argv.option('file-type', 'm,h')
|
12
|
+
@file_name = argv.option('file-name', 'MaucaoLife_zh_en.csv')
|
13
|
+
@cn_keys = []
|
14
|
+
@key_map = {}
|
15
|
+
@used_keys = []
|
16
|
+
super
|
17
|
+
end
|
18
|
+
|
19
|
+
def run
|
20
|
+
fetch_keys
|
21
|
+
read_csv
|
22
|
+
gen_csv
|
23
|
+
end
|
24
|
+
|
25
|
+
def csv_file_name
|
26
|
+
file_name = @file_name
|
27
|
+
file_name = "#{@file_name}.csv" unless /.csv$/ =~ @file_name
|
28
|
+
file_name
|
29
|
+
end
|
30
|
+
|
31
|
+
def read_csv
|
32
|
+
path = File.join(@current_path, csv_file_name)
|
33
|
+
Dir.glob(path).each do |p|
|
34
|
+
CSV.foreach(p) do |row|
|
35
|
+
key = row[0]
|
36
|
+
if @used_keys.any? { |k| k.eql?(key) }
|
37
|
+
@cn_keys << { key: key, cn: row[1], en: row[2], fname: row[3], dirname: row[4] }
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def gen_csv
|
44
|
+
file = File.join(@current_path, csv_file_name)
|
45
|
+
FileUtils.rm_rf(file) if File.exist?(file)
|
46
|
+
CSV.open(file, 'wb:utf-8') do |csv|
|
47
|
+
csv << %w[国际化key 中文 英文 所在文件 文件路径]
|
48
|
+
@cn_keys.each do |k|
|
49
|
+
csv << [k[:key], k[:cn], k[:en], k[:fname], k[:dirname]]
|
50
|
+
end
|
51
|
+
end
|
52
|
+
puts "生成csv文件完成.\n文件路径:#{File.absolute_path(file)}"
|
53
|
+
end
|
54
|
+
|
55
|
+
def fetch_keys
|
56
|
+
Dir.glob("#{@current_path}/**/*.{#{@file_type}}").each do |f|
|
57
|
+
handle_file f
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def zh_ch_reg
|
62
|
+
/MLLocalizedString\([^)]+\)/
|
63
|
+
end
|
64
|
+
|
65
|
+
def handle_file(file)
|
66
|
+
File.open(file, 'r') do |f|
|
67
|
+
f.each_line do |line|
|
68
|
+
handle_line(file, line) if zh_ch_reg =~ line
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def handle_line(file, line)
|
74
|
+
line.scan(zh_ch_reg) do |str|
|
75
|
+
str[20, str.length - 22]
|
76
|
+
@used_keys << str[20, str.length - 22]
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,264 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'csv'
|
4
|
+
require 'lhj/helper/trans_helper'
|
5
|
+
require 'lhj/helper/oss_helper'
|
6
|
+
require 'lhj/helper/local_config'
|
7
|
+
|
8
|
+
module Lhj
|
9
|
+
class Command
|
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
|
26
|
+
|
27
|
+
def initialize(argv)
|
28
|
+
@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'])
|
38
|
+
@key_map = {}
|
39
|
+
super
|
40
|
+
end
|
41
|
+
|
42
|
+
def run
|
43
|
+
down_load_csv_file if need_download
|
44
|
+
read_csv
|
45
|
+
if @key_map.keys.length.positive?
|
46
|
+
write_en_strings
|
47
|
+
write_zh_hk_strings
|
48
|
+
write_zh_cn_strings
|
49
|
+
handle_modify_source if @modify_source_flag
|
50
|
+
else
|
51
|
+
puts "获取中英文映射文件失败, 检查参数--read-csv-file=xx是否正常\n"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
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
|
+
def generate_file_name
|
68
|
+
@gen_file_name
|
69
|
+
end
|
70
|
+
|
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
|
+
def bak_file(file)
|
106
|
+
dest_file = File.join(File.dirname(file), 'csv_bak', File.basename(file))
|
107
|
+
File.exist?(dest_file) ? bak_file(dest_file) : dest_file
|
108
|
+
end
|
109
|
+
|
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
|
+
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
|
129
|
+
end
|
130
|
+
|
131
|
+
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')
|
137
|
+
CSV.foreach(file) do |row|
|
138
|
+
if row.length > 2
|
139
|
+
key = row[key_c]
|
140
|
+
cn_str = row[cn_c]
|
141
|
+
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
|
144
|
+
@key_map[key] = { key: key, cn: cn_str, hk: hk_str, en: row[en_c] } unless key =~ /[\u4e00-\u9fa5]/
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
def handle_modify_source
|
150
|
+
puts '修改源码开始'
|
151
|
+
Dir.glob("#{@current_path}/**/*.{#{@modify_file_type}}").each do |f|
|
152
|
+
# handle_modify_file f if File.stat(f).writable?
|
153
|
+
if f =~ /Pods/
|
154
|
+
handle_modify_file(f) if f =~ %r{Pods/ML}
|
155
|
+
else
|
156
|
+
handle_modify_file(f)
|
157
|
+
end
|
158
|
+
end
|
159
|
+
puts '修改源码结束'
|
160
|
+
end
|
161
|
+
|
162
|
+
def handle_modify_file(file)
|
163
|
+
File.chmod(0o644, file)
|
164
|
+
str = modify_file_string(file)
|
165
|
+
File.open(file, 'w+') do |f|
|
166
|
+
f.write(str)
|
167
|
+
end
|
168
|
+
File.chmod(0o444, file) if file =~ /Pods/
|
169
|
+
end
|
170
|
+
|
171
|
+
def modify_file_string(file)
|
172
|
+
str = ''
|
173
|
+
File.open(file, 'r') do |f|
|
174
|
+
f.each_line do |line|
|
175
|
+
str += modify_format_string(f, line)
|
176
|
+
end
|
177
|
+
end
|
178
|
+
str
|
179
|
+
end
|
180
|
+
|
181
|
+
def zh_ch_reg
|
182
|
+
/@"[^"]*[\u4e00-\u9fa5]+[^"]*"/
|
183
|
+
end
|
184
|
+
|
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
|
+
def handle_modify_line(file, line)
|
192
|
+
result = line
|
193
|
+
line.scan(zh_ch_reg) do |m|
|
194
|
+
key = find_key_by_cn_val(file, m)
|
195
|
+
if key
|
196
|
+
val = format(@modify_format_string, "@\"#{key}\"")
|
197
|
+
result = result.gsub(m, val)
|
198
|
+
end
|
199
|
+
end
|
200
|
+
result
|
201
|
+
end
|
202
|
+
|
203
|
+
def find_key_by_cn_val(file, val)
|
204
|
+
file_name = File.basename(file, '.*')
|
205
|
+
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]) }
|
208
|
+
@key_map.values[index][:key] if index
|
209
|
+
end
|
210
|
+
|
211
|
+
def format_str(type)
|
212
|
+
str = ''
|
213
|
+
@key_map.each do |k, v|
|
214
|
+
str += "\"#{k}\" = \"#{v[type]}\";\n"
|
215
|
+
end
|
216
|
+
str
|
217
|
+
end
|
218
|
+
|
219
|
+
def write_to_file(file, contents)
|
220
|
+
FileUtils.rm_rf(file) if File.exist?(file)
|
221
|
+
FileUtils.mkdir_p(File.dirname(file)) unless File.exist?(File.dirname(file))
|
222
|
+
File.open(file, 'w+') do |f|
|
223
|
+
f.write(contents)
|
224
|
+
end
|
225
|
+
end
|
226
|
+
|
227
|
+
def generate_file(file, type)
|
228
|
+
content = format_str(type)
|
229
|
+
write_to_file(file, content)
|
230
|
+
end
|
231
|
+
|
232
|
+
def write_en_strings
|
233
|
+
file = File.join(@current_path, en_dir_name, generate_file_name)
|
234
|
+
generate_file(file, :en)
|
235
|
+
puts "生成英文配置完成.文件路径:#{File.absolute_path(file)}\n"
|
236
|
+
end
|
237
|
+
|
238
|
+
def write_zh_cn_strings
|
239
|
+
gen_zh_cn_strings_file
|
240
|
+
end
|
241
|
+
|
242
|
+
def gen_zh_cn_strings_file
|
243
|
+
file = File.join(@current_path, zh_cn_dir_name, generate_file_name)
|
244
|
+
content = format_str(:cn)
|
245
|
+
write_to_file(file, content)
|
246
|
+
puts "生成简体中文配置完成.文件路径:#{File.absolute_path(file)}\n"
|
247
|
+
end
|
248
|
+
|
249
|
+
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)
|
252
|
+
FileUtils.cp source_file, dest_file
|
253
|
+
puts "繁体中文配置覆盖简体中文配置\n"
|
254
|
+
end
|
255
|
+
|
256
|
+
def write_zh_hk_strings
|
257
|
+
file = File.join(@current_path, zh_hk_dir_name, generate_file_name)
|
258
|
+
content = format_str(:hk)
|
259
|
+
write_to_file(file, content)
|
260
|
+
puts "生成繁体中文配置完成.文件路径:#{File.absolute_path(file)}\n"
|
261
|
+
end
|
262
|
+
end
|
263
|
+
end
|
264
|
+
end
|