pixab 1.7.3 → 1.8.0

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: 84d54a1ab3a12281357538f78851d2b729803cf110e51570a17f2ee0e1c480de
4
- data.tar.gz: 2223cb8f2541517bcde9f0490cc064061043ddfc964d42d93d4ea953eaf2e417
3
+ metadata.gz: ea5e126059d8afc2bf728cbad73ebdc6daef4302d8d1eb175d25799191949f75
4
+ data.tar.gz: fe311f10dbde070db2f146060bcd79c531a49c0f08d5429be5e0e36696e9d823
5
5
  SHA512:
6
- metadata.gz: feb82c51f413c5a117ef86728574aa5498f9d5a13181eef65c555fc10b525af202f94dddbbf13f08b5b56c105dbb9fa0eb7e2fa376a1d95b8706b36ee0767c62
7
- data.tar.gz: 43ad564e1d758c1ed30bc929207af08c25e5426b33f8a59f80b731d8d055881f93afb37a0017428759ae511a37b00feaf140924c358594e15b6a3ca339b85390
6
+ metadata.gz: dc0aa68915ddc765958ec7931afe991779b4ec29559af33701b64dcd737bd7df019aee59717a16f1fcf4ef4e6f14b6f13f631bd7a93fe4a254ff9ad4d8bb5765
7
+ data.tar.gz: 87b5b227fffc9666c624c92f18ff9866a5b00cef3d3e0d7d549bc6a0ce032bf139186a388368a500788b30e24b2dbf95cf3e21ae35604b2150fa854fad109c10
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
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
 
@@ -31,16 +26,18 @@ module Pixab
31
26
  @platform = 'android'
32
27
  @tags = 'android'
33
28
  @collections = 'AirBrush'
29
+ @languages = 'en,ru,tr,de,fr,zh-Hans,zh-Hant,pt-BR,es,ar'
34
30
  when '--ab-iOS'
35
31
  @platform = 'iOS'
36
32
  @tags = 'iOS'
37
33
  @collections = 'AirBrush'
34
+ @languages = 'en,ru,tr,de,fr,zh-Hans,zh-Hant,pt-BR,es,ar'
38
35
  when '--abv-iOS'
39
- @projects = Project_AirBrushVideo
36
+ @projects = LocalizationSmartcatInfo::Project_AirBrushVideo
40
37
  @platform = 'iOS'
41
38
  @tags = 'iOS'
42
39
  when '--abv-android'
43
- @projects = Project_AirBrushVideo
40
+ @projects = LocalizationSmartcatInfo::Project_AirBrushVideo
44
41
  @platform = 'android'
45
42
  @tags = 'android'
46
43
  end
@@ -57,6 +54,12 @@ module Pixab
57
54
  @platform = commands[index + 1]
58
55
  when '--collections'
59
56
  @collections = commands[index + 1]
57
+ when '--languages'
58
+ @languages = commands[index + 1]
59
+ when '--format'
60
+ @format = commands[index + 1]
61
+ when '--output'
62
+ @output = commands[index + 1]
60
63
  end
61
64
  end
62
65
 
@@ -82,7 +85,7 @@ module Pixab
82
85
  uri.query = URI.encode_www_form(export_params)
83
86
 
84
87
  request = Net::HTTP::Post.new(uri)
85
- request.basic_auth(USERNAME,PASSWORD)
88
+ request.basic_auth(LocalizationSmartcatInfo::USERNAME, LocalizationSmartcatInfo::PASSWORD)
86
89
 
87
90
  response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https") do |http|
88
91
  http.request(request)
@@ -97,7 +100,7 @@ module Pixab
97
100
  while retries < max_retries
98
101
  uri = URI("#{download_url}/#{export_id}")
99
102
  request = Net::HTTP::Get.new(uri)
100
- request.basic_auth(USERNAME,PASSWORD)
103
+ request.basic_auth(LocalizationSmartcatInfo::USERNAME, LocalizationSmartcatInfo::PASSWORD)
101
104
 
102
105
  response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https") do |http|
103
106
  http.request(request)
@@ -115,17 +118,20 @@ module Pixab
115
118
  end
116
119
  end
117
120
 
118
- puts "Failed to download after #{max_retries} retries. Export ID: #{export_id}"
121
+ puts "\nFailed to download after #{max_retries} retries. Export ID: #{export_id}"
119
122
  return false
120
123
  end
121
124
 
122
125
  # 第三步:解压缩ZIP文件
123
126
  def unzip_file(zip_path)
124
127
  Zip::File.open(zip_path) do |zip_file|
125
- if platform == 'android'
128
+ case platform
129
+ when 'android'
126
130
  unzip_file_android(zip_file)
127
- else
131
+ when 'iOS'
128
132
  unzip_file_iOS(zip_file)
133
+ else
134
+ unzip_file_common(zip_file)
129
135
  end
130
136
  end
131
137
 
@@ -133,6 +139,15 @@ module Pixab
133
139
 
134
140
  end
135
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
+
136
151
  def unzip_file_iOS(zip_file)
137
152
  zip_file.each do |f|
138
153
  if is_ignored_file_path(f.name)
@@ -141,7 +156,7 @@ module Pixab
141
156
  f_path = extract_localization_file_path(f.name)
142
157
  FileUtils.mkdir_p(File.dirname(f_path))
143
158
  content = f.get_input_stream.read
144
- if projects == Project_AirBrush
159
+ if projects == LocalizationSmartcatInfo::Project_AirBrush
145
160
  content = content.gsub(/=\s*".*";/) do |match|
146
161
  match.gsub('%s', '%@')
147
162
  end
@@ -177,25 +192,42 @@ module Pixab
177
192
  'include-default-language' => nil,
178
193
  }
179
194
 
180
- if !platform.nil?
181
- format = nil
182
- template = nil
183
- if platform == 'android'
184
- format = 'android-xml'
185
- if projects == Project_AirBrush
186
- 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'
187
203
  else
188
- template = '{LOCALE:ANDROID}/strings.xml'
204
+ final_output = '{LOCALE:ANDROID}/strings.xml'
189
205
  end
190
- else
191
- format = 'ios-strings'
192
- template = '{LOCALE:IOS}.lproj/Localizable.strings'
206
+ when 'iOS'
207
+ final_format = 'ios-strings'
208
+ final_output = '{LOCALE:IOS}.lproj/Localizable.strings'
193
209
  end
194
- export_params['format'] = format
195
- export_params['output-file-path-template'] = template
196
210
  end
197
211
 
198
- 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?
199
231
  export_params['labels'] = tags
200
232
  end
201
233
 
@@ -204,7 +236,7 @@ module Pixab
204
236
 
205
237
  def extract_localization_file_path(zip_file_path)
206
238
  case projects
207
- when Project_AirBrush
239
+ when LocalizationSmartcatInfo::Project_AirBrush
208
240
  if platform.nil? || platform != 'android'
209
241
  return zip_file_path
210
242
  end
@@ -229,7 +261,7 @@ module Pixab
229
261
  end
230
262
  return "values#{localization}/#{File.basename(zip_file_path)}"
231
263
 
232
- when Project_AirBrushVideo
264
+ when LocalizationSmartcatInfo::Project_AirBrushVideo
233
265
  if platform.nil?
234
266
  return zip_file_path
235
267
  end
@@ -260,11 +292,12 @@ module Pixab
260
292
  end
261
293
  end
262
294
 
295
+ return zip_file_path
263
296
  end
264
297
 
265
298
  def is_ignored_file_path(file_path)
266
299
  case projects
267
- when Project_AirBrush
300
+ when LocalizationSmartcatInfo::Project_AirBrush
268
301
  return false unless platform == 'android'
269
302
 
270
303
  path = File.dirname(file_path)
@@ -0,0 +1,160 @@
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
10
+
11
+ def run(commands = nil)
12
+ commands.each_index do |index|
13
+ command = commands[index]
14
+ case command
15
+ when '--ab'
16
+ @project = LocalizationSmartcatInfo::Project_AirBrush
17
+ @collection = 'AirBrush'
18
+ @tags = 'iOS,android'
19
+ @conflicting_values = 'skip'
20
+ when '--abtest'
21
+ @project = LocalizationSmartcatInfo::Project_AirBrush_test
22
+ @collection = 'merge_test'
23
+ @tags = 'iOS,android'
24
+ @conflicting_values = 'skip'
25
+ end
26
+ end
27
+
28
+ commands.each_index do |index|
29
+ command = commands[index]
30
+ case command
31
+ when '--project'
32
+ @project = commands[index + 1]
33
+ when '--collection'
34
+ @collection = commands[index + 1]
35
+ when '--format'
36
+ @format = commands[index + 1]
37
+ when '--tags'
38
+ @tags = commands[index + 1]
39
+ when '--completion-state'
40
+ @completion_state = commands[index + 1]
41
+ when '--conflicting-values'
42
+ @conflicting_values = commands[index + 1]
43
+ end
44
+ end
45
+
46
+ import_localization_from_directory(Dir.pwd)
47
+
48
+ end
49
+
50
+ # 遍历目录上传本地化文案
51
+ def import_localization_from_directory(directory)
52
+ import_url = "https://smartcat.com/api/integration/v2/project/#{@project}/import"
53
+ entries = []
54
+ Dir.foreach(directory) do |entry|
55
+ if entry.start_with?(".")
56
+ next
57
+ end
58
+
59
+ if File.basename(entry, File.extname(entry)) == 'en'
60
+ entries.unshift(entry)
61
+ else
62
+ entries.push(entry)
63
+ end
64
+ end
65
+
66
+ puts "\n》》》》》正在上传本地化文案 》》》》》》》》》》\n".green
67
+
68
+ entries.each do |entry|
69
+ file_path = "#{directory}/#{entry}"
70
+ import_params = generate_import_params_from_file_name(entry)
71
+ import_id = import_localization(import_url, import_params, file_path)
72
+ puts "#{entry} 正在上传中,上传ID:#{import_id}"
73
+ File.delete(file_path) if File.exist?(file_path)
74
+ end
75
+
76
+ puts "\n》》》》》本地化文案上传已完成 》》》》》》》》》》\n".green
77
+ puts "提示:由于Smartcat后台延迟,上传文案可能需要等待1-2分钟才能在后台全部显示。"
78
+
79
+ end
80
+
81
+ # 导入本地化文案
82
+ def import_localization(import_url, import_params, file_path)
83
+ uri = URI(import_url)
84
+ uri.query = URI.encode_www_form(import_params)
85
+ response = RestClient::Request.execute(
86
+ method: :post,
87
+ url: uri.to_s,
88
+ user: LocalizationSmartcatInfo::USERNAME,
89
+ password: LocalizationSmartcatInfo::PASSWORD,
90
+ payload: {
91
+ multipart: true,
92
+ file: File.new(file_path, 'rb')
93
+ }
94
+ )
95
+ return response.body
96
+ end
97
+
98
+ # 通过文件名称获取上传参数
99
+ def generate_import_params_from_file_name(file_name)
100
+ params = {}
101
+
102
+ unless @collection.nil?
103
+ params['collection'] = @collection
104
+ end
105
+
106
+ file_base_name = File.basename(file_name, File.extname(file_name))
107
+ params['language'] = file_base_name
108
+
109
+ unless @format.nil?
110
+ params['format'] = @format
111
+ end
112
+
113
+ unless @tags.nil?
114
+ params['labels'] = @tags
115
+ end
116
+
117
+ unless @completion_state.nil?
118
+ params['completion-state'] = @completion_state
119
+ end
120
+
121
+ unless @conflicting_values.nil?
122
+ if @conflicting_values == 'skip'
123
+ params['skip-conflicting-values'] = nil
124
+ elsif @conflicting_values == 'overwrite'
125
+ params['overwrite-conflicting-values'] = nil
126
+ end
127
+ end
128
+
129
+ return params
130
+ end
131
+
132
+ # 检查文件上传是否成功
133
+ def check_import_status(import_id, max_retries=60)
134
+ # 移除双引号
135
+ import_id = import_id.tr('"','')
136
+ retries = 0
137
+ while retries < max_retries
138
+ import_result_url = "https://smartcat.ai/api/v2/project/import-result/#{import_id}"
139
+ uri = URI(import_result_url)
140
+ request = Net::HTTP::Get.new(uri)
141
+ request.basic_auth(LocalizationSmartcatInfo::USERNAME, LocalizationSmartcatInfo::PASSWORD)
142
+ response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https") do |http|
143
+ http.request(request)
144
+ end
145
+
146
+ if response.code == "200"
147
+ puts response.body
148
+ retries += 1
149
+ sleep(1)
150
+ else
151
+ puts response.body
152
+ return false
153
+ end
154
+
155
+ end
156
+ end
157
+
158
+ end
159
+
160
+ end
@@ -0,0 +1,17 @@
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
+ end
16
+
17
+ 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 = []
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.3"
4
+ VERSION = "1.8.0"
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.3
4
+ version: 1.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - 廖再润
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-12-09 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