pixab 1.7.5 → 1.8.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5caf285f26d4a55e734aad944c40ca46b1f8458fc83e73cbdd4e2195ad57afdc
4
- data.tar.gz: 8625302238e71910fc595c340fdc99e05d53ebb9465ad27743fc0907c9bbbaa0
3
+ metadata.gz: 5f7a1aa87941f9dd40ce7081271a52f5766433522e97d2fef66bbd65a5fdf0c6
4
+ data.tar.gz: b81b6f80cbce8298e1c94186a5a527dba99873f950084b79784e1aac765a88ae
5
5
  SHA512:
6
- metadata.gz: fffb6152204d9d77677edc3f256e583057f8c594f3cee9b31acc711c4f5a075cfb63b23a51ad07ace23cf83940045499ebf3d66820abb338dc02de03f0418b43
7
- data.tar.gz: be6024b8c34ed9e744b95e37a4968d7c3cad1133ea683a1d0eb3117bbdb46005a0287fa160ad9b21dfe4cbd8dc57fb4d7bdbdd013099164f731bfb44c46d40c6
6
+ metadata.gz: e75b6835902ada48dbeaec2e064ba509726d144cf7004d18841d6f268c08f6ad4e80194813f7bf7401b650fe10d1406347234877604a85791ae6b4c7cfaec556
7
+ data.tar.gz: f3e495b4c839a8d87e3d6210a26172368d1eb5dc951083364a97cd2e970e5e55eeb8c72a3dad832493edaabf4603b94abae3f380012b7e658517979867683ef3
data/exe/pixab CHANGED
@@ -8,6 +8,8 @@ require_relative '../lib/ComponentSynchronizer.rb'
8
8
  require_relative '../lib/Localization.rb'
9
9
  require_relative '../lib/Package.rb'
10
10
  require_relative '../lib/LocalizationSmartcat.rb'
11
+ require_relative '../lib/LocalizationSmartcatImport.rb'
12
+ require_relative '../lib/LocalizationSmartcatMerge.rb'
11
13
 
12
14
  case ARGV[0]
13
15
  when '--version'
@@ -37,7 +39,19 @@ when 'localizePhrase'
37
39
  when 'package'
38
40
  Pixab::Package.new.run
39
41
  when 'localize'
40
- Pixab::LocalizationSmartcat.new.run(ARGV[1..-1])
42
+ if ARGV.count > 1
43
+ sub_command = ARGV[1]
44
+ case sub_command
45
+ when 'import'
46
+ Pixab::LocalizationSmartcatImport.new.run(ARGV[2..-1])
47
+ when 'merge'
48
+ Pixab::LocalizationSmartcatMerge.new.run(ARGV[2..-1])
49
+ else
50
+ Pixab::LocalizationSmartcat.new.run(ARGV[1..-1])
51
+ end
52
+ else
53
+ Pixab::LocalizationSmartcat.new.run(ARGV[1..-1])
54
+ end
41
55
  else
42
56
  puts "Invalid command".red
43
57
  end
@@ -3,23 +3,18 @@ require 'uri'
3
3
  require 'zip'
4
4
  require 'colored2'
5
5
  require 'nokogiri'
6
+ require_relative './LocalizationSmartcatInfo.rb'
6
7
 
7
8
  module Pixab
8
9
 
9
10
  class LocalizationSmartcat
10
11
 
11
- # 设置基本认证信息
12
- USERNAME = '4c8b26ac-ff12-427f-a460-afaacbb3fa3c'
13
- PASSWORD = '6_nDd3whcOZQHv5dPAusbq5Wmfl'
14
12
  Localization_FILE_NAME = 'Localization.zip'
15
13
 
16
- Project_AirBrush = '6cd2db15-6325-43ae-9087-f78aca9bec9a'
17
- Project_AirBrushVideo = '16cbeffd-bb6e-46e8-a32e-9c79d23a796f'
18
-
19
- attr_accessor :projects, :tags, :platform, :collections, :languages
14
+ attr_accessor :projects, :tags, :platform, :collections, :languages, :format, :output
20
15
 
21
16
  def initialize()
22
- @projects = Project_AirBrush
17
+ @projects = LocalizationSmartcatInfo::Project_AirBrush
23
18
  @collections = 'main'
24
19
  end
25
20
 
@@ -38,11 +33,11 @@ module Pixab
38
33
  @collections = 'AirBrush'
39
34
  @languages = 'en,ru,tr,de,fr,zh-Hans,zh-Hant,pt-BR,es,ar'
40
35
  when '--abv-iOS'
41
- @projects = Project_AirBrushVideo
36
+ @projects = LocalizationSmartcatInfo::Project_AirBrushVideo
42
37
  @platform = 'iOS'
43
38
  @tags = 'iOS'
44
39
  when '--abv-android'
45
- @projects = Project_AirBrushVideo
40
+ @projects = LocalizationSmartcatInfo::Project_AirBrushVideo
46
41
  @platform = 'android'
47
42
  @tags = 'android'
48
43
  end
@@ -61,6 +56,10 @@ module Pixab
61
56
  @collections = commands[index + 1]
62
57
  when '--languages'
63
58
  @languages = commands[index + 1]
59
+ when '--format'
60
+ @format = commands[index + 1]
61
+ when '--output'
62
+ @output = commands[index + 1]
64
63
  end
65
64
  end
66
65
 
@@ -86,7 +85,7 @@ module Pixab
86
85
  uri.query = URI.encode_www_form(export_params)
87
86
 
88
87
  request = Net::HTTP::Post.new(uri)
89
- request.basic_auth(USERNAME,PASSWORD)
88
+ request.basic_auth(LocalizationSmartcatInfo::USERNAME, LocalizationSmartcatInfo::PASSWORD)
90
89
 
91
90
  response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https") do |http|
92
91
  http.request(request)
@@ -101,7 +100,7 @@ module Pixab
101
100
  while retries < max_retries
102
101
  uri = URI("#{download_url}/#{export_id}")
103
102
  request = Net::HTTP::Get.new(uri)
104
- request.basic_auth(USERNAME,PASSWORD)
103
+ request.basic_auth(LocalizationSmartcatInfo::USERNAME, LocalizationSmartcatInfo::PASSWORD)
105
104
 
106
105
  response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https") do |http|
107
106
  http.request(request)
@@ -126,10 +125,13 @@ module Pixab
126
125
  # 第三步:解压缩ZIP文件
127
126
  def unzip_file(zip_path)
128
127
  Zip::File.open(zip_path) do |zip_file|
129
- if platform == 'android'
128
+ case platform
129
+ when 'android'
130
130
  unzip_file_android(zip_file)
131
- else
131
+ when 'iOS'
132
132
  unzip_file_iOS(zip_file)
133
+ else
134
+ unzip_file_common(zip_file)
133
135
  end
134
136
  end
135
137
 
@@ -137,6 +139,15 @@ module Pixab
137
139
 
138
140
  end
139
141
 
142
+ def unzip_file_common(zip_file)
143
+ zip_file.each do |f|
144
+ f_path = f.name
145
+ FileUtils.mkdir_p(File.dirname(f_path))
146
+ File.delete(f_path) if File.exist?(f_path)
147
+ f.extract(f_path)
148
+ end
149
+ end
150
+
140
151
  def unzip_file_iOS(zip_file)
141
152
  zip_file.each do |f|
142
153
  if is_ignored_file_path(f.name)
@@ -145,7 +156,7 @@ module Pixab
145
156
  f_path = extract_localization_file_path(f.name)
146
157
  FileUtils.mkdir_p(File.dirname(f_path))
147
158
  content = f.get_input_stream.read
148
- if projects == Project_AirBrush
159
+ if projects == LocalizationSmartcatInfo::Project_AirBrush
149
160
  content = content.gsub(/=\s*".*";/) do |match|
150
161
  match.gsub('%s', '%@')
151
162
  end
@@ -181,29 +192,42 @@ module Pixab
181
192
  'include-default-language' => nil,
182
193
  }
183
194
 
184
- unless languages.nil?
185
- export_params['languages'] = languages
186
- end
187
-
188
- if !platform.nil?
189
- format = nil
190
- template = nil
191
- if platform == 'android'
192
- format = 'android-xml'
193
- if projects == Project_AirBrush
194
- template = '{LOCALE:ANDROID}/strings_ph.xml'
195
+ final_format = nil
196
+ final_output = nil
197
+ unless platform.nil?
198
+ case platform
199
+ when 'android'
200
+ final_format = 'android-xml'
201
+ if projects == LocalizationSmartcatInfo::Project_AirBrush
202
+ final_output = '{LOCALE:ANDROID}/strings_ph.xml'
195
203
  else
196
- template = '{LOCALE:ANDROID}/strings.xml'
204
+ final_output = '{LOCALE:ANDROID}/strings.xml'
197
205
  end
198
- else
199
- format = 'ios-strings'
200
- template = '{LOCALE:IOS}.lproj/Localizable.strings'
206
+ when 'iOS'
207
+ final_format = 'ios-strings'
208
+ final_output = '{LOCALE:IOS}.lproj/Localizable.strings'
201
209
  end
202
- export_params['format'] = format
203
- export_params['output-file-path-template'] = template
204
210
  end
205
211
 
206
- if !tags.nil?
212
+ unless @format.nil?
213
+ final_format = @format
214
+ end
215
+ unless @output.nil?
216
+ final_output = @output
217
+ end
218
+
219
+ unless final_format.nil?
220
+ export_params['format'] = final_format
221
+ end
222
+ unless final_output.nil?
223
+ export_params['output-file-path-template'] = final_output
224
+ end
225
+
226
+ unless languages.nil?
227
+ export_params['languages'] = languages
228
+ end
229
+
230
+ unless tags.nil?
207
231
  export_params['labels'] = tags
208
232
  end
209
233
 
@@ -212,7 +236,7 @@ module Pixab
212
236
 
213
237
  def extract_localization_file_path(zip_file_path)
214
238
  case projects
215
- when Project_AirBrush
239
+ when LocalizationSmartcatInfo::Project_AirBrush
216
240
  if platform.nil? || platform != 'android'
217
241
  return zip_file_path
218
242
  end
@@ -237,7 +261,7 @@ module Pixab
237
261
  end
238
262
  return "values#{localization}/#{File.basename(zip_file_path)}"
239
263
 
240
- when Project_AirBrushVideo
264
+ when LocalizationSmartcatInfo::Project_AirBrushVideo
241
265
  if platform.nil?
242
266
  return zip_file_path
243
267
  end
@@ -268,11 +292,12 @@ module Pixab
268
292
  end
269
293
  end
270
294
 
295
+ return zip_file_path
271
296
  end
272
297
 
273
298
  def is_ignored_file_path(file_path)
274
299
  case projects
275
- when Project_AirBrush
300
+ when LocalizationSmartcatInfo::Project_AirBrush
276
301
  return false unless platform == 'android'
277
302
 
278
303
  path = File.dirname(file_path)
@@ -0,0 +1,170 @@
1
+ require 'uri'
2
+ require 'rest-client'
3
+ require_relative './LocalizationSmartcatInfo.rb'
4
+
5
+ module Pixab
6
+
7
+ class LocalizationSmartcatImport
8
+
9
+ attr_accessor :project, :collection, :format, :tags, :completion_state, :conflicting_values, :delete_file_after_import
10
+
11
+ def initialize()
12
+ @delete_file_after_import = false
13
+ end
14
+
15
+ def run(commands = nil)
16
+ commands.each_index do |index|
17
+ command = commands[index]
18
+ case command
19
+ when '--ab'
20
+ @project = LocalizationSmartcatInfo::Project_AirBrush
21
+ @collection = 'AirBrush'
22
+ @tags = 'iOS,android'
23
+ @conflicting_values = 'skip'
24
+ when '--abtest'
25
+ @project = LocalizationSmartcatInfo::Project_AirBrush_test
26
+ @collection = 'merge_test'
27
+ @tags = 'iOS,android'
28
+ @conflicting_values = 'skip'
29
+ end
30
+ end
31
+
32
+ commands.each_index do |index|
33
+ command = commands[index]
34
+ case command
35
+ when '--project'
36
+ @project = commands[index + 1]
37
+ when '--collection'
38
+ @collection = commands[index + 1]
39
+ when '--format'
40
+ @format = commands[index + 1]
41
+ when '--tags'
42
+ @tags = commands[index + 1]
43
+ when '--completion-state'
44
+ @completion_state = commands[index + 1]
45
+ when '--conflicting-values'
46
+ @conflicting_values = commands[index + 1]
47
+ when '--delete-file-after-import'
48
+ @delete_file_after_import = true
49
+ when '--keep-file-after-import'
50
+ @delete_file_after_import = false
51
+ end
52
+ end
53
+
54
+ import_localization_from_directory(Dir.pwd)
55
+
56
+ end
57
+
58
+ # 遍历目录上传本地化文案
59
+ def import_localization_from_directory(directory)
60
+ import_url = "https://smartcat.com/api/integration/v2/project/#{@project}/import"
61
+ entries = []
62
+ Dir.foreach(directory) do |entry|
63
+ if entry.start_with?(".")
64
+ next
65
+ end
66
+
67
+ if File.basename(entry, File.extname(entry)) == 'en'
68
+ entries.unshift(entry)
69
+ else
70
+ entries.push(entry)
71
+ end
72
+ end
73
+
74
+ puts "\n》》》》》正在上传本地化文案 》》》》》》》》》》\n".green
75
+
76
+ entries.each do |entry|
77
+ file_path = "#{directory}/#{entry}"
78
+ import_params = generate_import_params_from_file_name(entry)
79
+ import_id = import_localization(import_url, import_params, file_path)
80
+ puts "#{entry} 正在上传中,上传ID:#{import_id}"
81
+ if @delete_file_after_import
82
+ File.delete(file_path) if File.exist?(file_path)
83
+ end
84
+ end
85
+
86
+ puts "\n》》》》》本地化文案上传已完成 》》》》》》》》》》\n".green
87
+ puts "提示:由于Smartcat后台延迟,上传文案可能需要等待1-2分钟才能在后台全部显示。"
88
+
89
+ end
90
+
91
+ # 导入本地化文案
92
+ def import_localization(import_url, import_params, file_path)
93
+ uri = URI(import_url)
94
+ uri.query = URI.encode_www_form(import_params)
95
+ response = RestClient::Request.execute(
96
+ method: :post,
97
+ url: uri.to_s,
98
+ user: LocalizationSmartcatInfo::USERNAME,
99
+ password: LocalizationSmartcatInfo::PASSWORD,
100
+ payload: {
101
+ multipart: true,
102
+ file: File.new(file_path, 'rb')
103
+ }
104
+ )
105
+ return response.body
106
+ end
107
+
108
+ # 通过文件名称获取上传参数
109
+ def generate_import_params_from_file_name(file_name)
110
+ params = {}
111
+
112
+ unless @collection.nil?
113
+ params['collection'] = @collection
114
+ end
115
+
116
+ file_base_name = File.basename(file_name, File.extname(file_name))
117
+ params['language'] = file_base_name
118
+
119
+ unless @format.nil?
120
+ params['format'] = @format
121
+ end
122
+
123
+ unless @tags.nil?
124
+ params['labels'] = @tags
125
+ end
126
+
127
+ unless @completion_state.nil?
128
+ params['completion-state'] = @completion_state
129
+ end
130
+
131
+ unless @conflicting_values.nil?
132
+ if @conflicting_values == 'skip'
133
+ params['skip-conflicting-values'] = nil
134
+ elsif @conflicting_values == 'overwrite'
135
+ params['overwrite-conflicting-values'] = nil
136
+ end
137
+ end
138
+
139
+ return params
140
+ end
141
+
142
+ # 检查文件上传是否成功
143
+ def check_import_status(import_id, max_retries=60)
144
+ # 移除双引号
145
+ import_id = import_id.tr('"','')
146
+ retries = 0
147
+ while retries < max_retries
148
+ import_result_url = "https://smartcat.ai/api/v2/project/import-result/#{import_id}"
149
+ uri = URI(import_result_url)
150
+ request = Net::HTTP::Get.new(uri)
151
+ request.basic_auth(LocalizationSmartcatInfo::USERNAME, LocalizationSmartcatInfo::PASSWORD)
152
+ response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https") do |http|
153
+ http.request(request)
154
+ end
155
+
156
+ if response.code == "200"
157
+ puts response.body
158
+ retries += 1
159
+ sleep(1)
160
+ else
161
+ puts response.body
162
+ return false
163
+ end
164
+
165
+ end
166
+ end
167
+
168
+ end
169
+
170
+ end
@@ -0,0 +1,19 @@
1
+
2
+ module Pixab
3
+
4
+ class LocalizationSmartcatInfo
5
+
6
+ # 基本认证信息
7
+ USERNAME = '4c8b26ac-ff12-427f-a460-afaacbb3fa3c'
8
+
9
+ PASSWORD = '6_nDd3whcOZQHv5dPAusbq5Wmfl'
10
+
11
+ Project_AirBrush = '6cd2db15-6325-43ae-9087-f78aca9bec9a'
12
+
13
+ Project_AirBrushVideo = '16cbeffd-bb6e-46e8-a32e-9c79d23a796f'
14
+
15
+ Project_AirBrush_test = '0dafd468-f131-4c33-91f8-66d13e1eb6f4'
16
+
17
+ end
18
+
19
+ end
@@ -0,0 +1,47 @@
1
+
2
+ require_relative './LocalizationSmartcat.rb'
3
+ require_relative './LocalizationSmartcatImport.rb'
4
+
5
+ module Pixab
6
+
7
+ class LocalizationSmartcatMerge
8
+
9
+ def run(commands=nil)
10
+ download_params = []
11
+ import_params = ["--delete-file-after-import"]
12
+ commands.each_index do |index|
13
+ command = commands[index]
14
+
15
+ unless command.start_with?("--")
16
+ next
17
+ end
18
+
19
+ if command == "--ab"
20
+ download_params += ["--output", "{LANGUAGE}.json", "--tags", "ab"]
21
+ import_params.push("--ab")
22
+ next
23
+ end
24
+
25
+ if command == "--abtest"
26
+ download_params += ["--output", "{LANGUAGE}.json", "--tags", "merge_test"]
27
+ import_params.push("--abtest")
28
+ next
29
+ end
30
+
31
+ if command.start_with?("--to-")
32
+ import_params.push(command.sub("--to-", "--"))
33
+ import_params.push(commands[index + 1])
34
+ else
35
+ download_params.push(command)
36
+ download_params.push(commands[index + 1])
37
+ end
38
+
39
+ end
40
+
41
+ LocalizationSmartcat.new.run(download_params)
42
+ LocalizationSmartcatImport.new.run(import_params)
43
+ end
44
+
45
+ end
46
+
47
+ end
data/lib/pixab/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Pixab
4
- VERSION = "1.7.5"
4
+ VERSION = "1.8.1"
5
5
  end
data/pixab.gemspec CHANGED
@@ -36,6 +36,7 @@ Gem::Specification.new do |spec|
36
36
  spec.add_dependency "colored2"
37
37
  spec.add_dependency 'rubyzip'
38
38
  spec.add_dependency 'nokogiri'
39
+ spec.add_dependency 'rest-client'
39
40
 
40
41
  # For more information and examples about making a new gem, check out our
41
42
  # guide at: https://bundler.io/guides/creating_gem.html
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pixab
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.7.5
4
+ version: 1.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - 廖再润
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-12-29 00:00:00.000000000 Z
11
+ date: 2024-01-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colored2
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rest-client
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  description: Write a longer description or delete this line.
56
70
  email:
57
71
  - lzr3@us.meitu.com
@@ -72,6 +86,9 @@ files:
72
86
  - lib/Localization.rb
73
87
  - lib/LocalizationPlatform.rb
74
88
  - lib/LocalizationSmartcat.rb
89
+ - lib/LocalizationSmartcatImport.rb
90
+ - lib/LocalizationSmartcatInfo.rb
91
+ - lib/LocalizationSmartcatMerge.rb
75
92
  - lib/MergeRequest.rb
76
93
  - lib/Package.rb
77
94
  - lib/RepoManager.rb