applocale 0.1.6 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,178 +7,387 @@ require File.expand_path('../../Core/setting.rb', __FILE__)
7
7
  require 'pathname'
8
8
 
9
9
  module Applocale
10
- class ConfigUtil
11
- def self.create_configfile_ifneed(platform)
12
- pathstr = FileUtil.configfile_pathstr
13
- self.create_configfile(platform, pathstr) unless File.exist?(pathstr)
14
- end
15
-
16
- def self.create_configfile(platform, configfile_pathstr)
17
- src_pathstr = File.expand_path("../../#{FileUtil.filename_config}", __FILE__)
18
-
19
- File.open(src_pathstr, 'r') do |form|
20
- File.open(configfile_pathstr, 'w') do |to|
21
- form.each_line do |line|
22
- newline = line.gsub("\#{platform}", "#{platform.to_s}")
23
- newline = newline.gsub("\#{path_zh_TW}", FileUtil.get_default_localefile_relative_pathstr(platform, Locale::ZH_TW))
24
- newline = newline.gsub("\#{path_zh_CN}", FileUtil.get_default_localefile_relative_pathstr(platform, Locale::ZH_CN))
25
- newline = newline.gsub("\#{path_en_US}", FileUtil.get_default_localefile_relative_pathstr(platform, Locale::EN_US))
26
- newline = newline.gsub("\#{xlsxpath}", FileUtil.get_default_xlsx_relativepath_str)
27
- to.puts(newline)
28
- end
10
+ module Config
11
+ class ConfigUtil
12
+ attr_accessor :configfile_pathstr
29
13
 
14
+ def initialize(projectdir_path)
15
+ projpath = Pathname.new(projectdir_path.strip)
16
+ if File.directory?(projpath)
17
+ self.configfile_pathstr = File.join(projpath, FilePathUtil.default_mainfolder, FilePathUtil.default_config_filename)
18
+ FileUtils.mkdir_p(File.dirname(self.configfile_pathstr))
19
+ else
20
+ ErrorUtil::ConfigFileInValid.new('Project Path is invalid.').raise
30
21
  end
31
22
  end
32
- end
33
-
34
- def self.load_and_validate_xlsx_to_localefile(is_local_update)
35
- config_yaml = self.load_config
36
- self.validate_xlsx_to_localefile(config_yaml, is_local_update)
37
- end
38
-
39
- def self.load_and_validate_localefile_to_xlsx()
40
- config_yaml = self.load_config
41
- self.validate_localefile_to_xlsx(config_yaml)
42
- end
43
23
 
44
- # private
45
- def self.load_config
46
- configfile_path = FileUtil.configfile_pathstr
47
- unless File.exist?(configfile_path)
48
- ErrorUtil::MissingConfigFile.new.raise
24
+ public
25
+ def create_configfile(platform)
26
+ if !File.exist?(self.configfile_pathstr)
27
+ src_pathstr = File.expand_path("../../#{FilePathUtil.default_config_filename}", __FILE__)
28
+ File.open(src_pathstr, 'r') do |form|
29
+ File.open(configfile_pathstr, 'w') do |to|
30
+ form.each_line do |line|
31
+ newline = line.gsub("\#{platform}", "#{platform.to_s}")
32
+ newline = newline.gsub("\#{path_zh_TW}", FilePathUtil.default_localefile_relative_pathstr(platform, Locale::ZH_TW))
33
+ newline = newline.gsub("\#{path_zh_CN}", FilePathUtil.default_localefile_relative_pathstr(platform, Locale::ZH_CN))
34
+ newline = newline.gsub("\#{path_en_US}", FilePathUtil.default_localefile_relative_pathstr(platform, Locale::EN_US))
35
+ newline = newline.gsub("\#{xlsxpath}", FilePathUtil.default_xlsx_relativepath_str)
36
+ newline = newline.gsub("\#{google_credentials_path}", FilePathUtil.default_google_credentials_filename)
37
+ to.puts(newline)
38
+ end
39
+ end
40
+ end
41
+ end
49
42
  end
50
- begin
51
- config_yaml = YAML.load_file configfile_path
52
- rescue
53
- ErrorUtil::ConfigFileInValid.new('ConfigFile format is invalid.').raise
43
+
44
+ public
45
+ def self.create_configfile_ifneed(platform, projectdir_path)
46
+ config = ConfigUtil.new(projectdir_path)
47
+ config.create_configfile(platform)
54
48
  end
55
- return config_yaml
56
- end
57
49
 
58
- def self.validate_xlsx_to_localefile(config_yaml, is_local_update)
59
- error_list = self.validate_common(config_yaml)
60
- if is_local_update
61
- unless File.exist? Setting.xlsxpath
62
- error = ErrorUtil::ConfigFileInValid.new("#{Setting.xlsxpath} do not exist")
63
- error_list.push(error)
50
+ private
51
+ def load_configfile
52
+ rubycode = ''
53
+ unless File.exist?(self.configfile_pathstr)
54
+ ErrorUtil::MissingConfigFile.new.raise
64
55
  end
65
- else
66
- if config_yaml['link'].to_s.strip.nil? || config_yaml['link'].to_s.strip.length <= 0
67
- error = ErrorUtil::ConfigFileInValid.new('[link] should not be empty')
68
- error_list.push(error)
56
+ begin
57
+ yaml = ""
58
+ File.open(self.configfile_pathstr).each do |line|
59
+ reg = /\w*\s*:\s*"?.*"?/
60
+ if line.match reg
61
+ yaml += line
62
+ else
63
+ rubycode += line
64
+ end
65
+ end
66
+ config_yaml = YAML.load( yaml)
67
+ rescue
68
+ ErrorUtil::ConfigFileInValid.new('ConfigFile format is invalid.')
69
69
  end
70
+ return config_yaml, rubycode
70
71
  end
71
- ErrorUtil::ConfigFileInValid.raiseArr(error_list)
72
- end
73
72
 
74
- def self.validate_localefile_to_xlsx(config_yaml)
75
- error_list = self.validate_common(config_yaml)
76
- Setting.langlist.each do |_, langinfo|
77
- unless File.exist? langinfo[:path]
78
- error = ErrorUtil::ConfigFileInValid.new("#{langinfo[:path]} do not exist")
79
- error_list.push(error)
73
+ public
74
+ def load_configfile_to_setting
75
+ error_list = Array.new
76
+ config_yaml, rubycode = load_configfile
77
+ link = config_yaml['link'].to_s.strip
78
+ platform = config_yaml['platform'].to_s.strip
79
+ xlsxpath = config_yaml['xlsxpath'].to_s.strip
80
+ google_credentials_path = config_yaml['google_credentials_path'].to_s.strip
81
+ langlist = config_yaml['langlist']
82
+ sheetname = config_yaml['sheetname']
83
+
84
+
85
+ setting = Applocale::Config::Setting.new(self.configfile_pathstr)
86
+ setting.rubycode = rubycode
87
+ unless link.nil? || link.length == 0
88
+ if (link =~ /^https/).nil? && (link =~ /^http/).nil?
89
+ error = ErrorUtil::ConfigFileInValid.new("Invalid link for [link] : #{link}")
90
+ error_list.push(error)
91
+ else
92
+ setting.link = link
93
+ end
80
94
  end
81
- end
82
- ErrorUtil::ConfigFileInValid.raiseArr(error_list)
83
- end
84
95
 
85
- def self.validate_common(config_yaml)
86
- error_list = Array.new
87
- link = config_yaml['link'].to_s.strip
88
- platform = config_yaml['platform'].to_s.strip
89
- keystr = config_yaml['keystr'].to_s.strip
90
- langlist = config_yaml['langlist']
91
- xlsxpath = config_yaml['xlsxpath'].to_s.strip
92
-
93
- newlink = nil
94
- newplatform = nil
95
- newkeystr = nil
96
- newlanglist = Hash.new
97
- newxlsxpath = nil
98
-
99
- unless link.nil? || link.length == 0
100
- if (link =~ /^https/).nil? && (link =~ /^http/).nil?
101
- error = ErrorUtil::ConfigFileInValid.new("Invalid link for [link] : #{link}")
96
+ if platform.nil? || platform.length == 0
97
+ error = ErrorUtil::ConfigFileInValid.new('[platform] should not be empty')
102
98
  error_list.push(error)
103
99
  else
104
- newlink = link
100
+ if Platform.init(platform).nil?
101
+ error = ErrorUtil::ConfigFileInValid.new("[platform] can only be 'ios' or 'android' ")
102
+ error_list.push(error)
103
+ else
104
+ setting.platform = Platform.init(platform)
105
+ end
105
106
  end
106
- end
107
107
 
108
- if !(xlsxpath.nil? || xlsxpath.length == 0)
109
- if !(Pathname.new xlsxpath).absolute?
110
- newxlsxpath = File.expand_path(xlsxpath, File.dirname(FileUtil.configfile_pathstr))
108
+ if !(xlsxpath.nil? || xlsxpath.length == 0)
109
+ if !(Pathname.new xlsxpath).absolute?
110
+ setting.xlsxpath = File.expand_path(xlsxpath, File.dirname(self.configfile_pathstr))
111
+ else
112
+ setting.xlsxpath = xlsxpath
113
+ end
111
114
  else
112
- newxlsxpath = xlsxpath
113
- end
114
- else
115
- error = ErrorUtil::ConfigFileInValid.new('[xlsxpath] should not be empty or missing')
116
- error_list.push(error)
117
- end
118
-
119
- if platform.nil? || platform.length == 0
120
- error = ErrorUtil::ConfigFileInValid.new('[platform] should not be empty')
121
- error_list.push(error)
122
- else
123
- if Platform.init(platform).nil?
124
- error = ErrorUtil::ConfigFileInValid.new("[platform] can only be 'ios' or 'android' ")
115
+ error = ErrorUtil::ConfigFileInValid.new('[xlsxpath] should not be empty or missing')
125
116
  error_list.push(error)
126
- else
127
- newplatform = Platform.init(platform)
128
117
  end
129
- end
130
118
 
131
- if keystr.nil? || keystr.length == 0
132
- error = ErrorUtil::ConfigFileInValid.new('[keystr] should not be empty')
133
- error_list.push(error)
134
- else
135
- newkeystr = keystr
136
- end
119
+ if !(google_credentials_path.nil? || google_credentials_path.length == 0)
120
+ if !(Pathname.new google_credentials_path).absolute?
121
+ setting.google_credentials_path = File.expand_path(google_credentials_path, File.dirname(self.configfile_pathstr))
122
+ else
123
+ setting.google_credentials_path = google_credentials_path
124
+ end
125
+ end
137
126
 
138
- if langlist.nil?
139
- error = ErrorUtil::ConfigFileInValid.new('[langlist] should not be empty or missing')
140
- error_list.push(error)
141
- elsif !(langlist.is_a? Hash)
142
- error = ErrorUtil::ConfigFileInValid.new('[langlist] wrong format')
143
- error_list.push(error)
144
- else
145
- if langlist.length <= 0
127
+ if langlist.nil?
146
128
  error = ErrorUtil::ConfigFileInValid.new('[langlist] should not be empty ')
147
129
  error_list.push(error)
148
- end
149
- langlist.each do |lang, arr|
150
- if arr.length != 2
151
- error = ErrorUtil::ConfigFileInValid.new('[langlist] wrong format')
152
- error_list.push(error)
153
- else
154
- path = arr[1]
155
- unless (Pathname.new path).absolute?
156
- path = File.expand_path(path, File.dirname(FileUtil.configfile_pathstr))
130
+ elsif !(langlist.is_a? Hash)
131
+ error = ErrorUtil::ConfigFileInValid.new('[langlist] wrong format')
132
+ error_list.push(error)
133
+ elsif langlist.length <= 0
134
+ error = ErrorUtil::ConfigFileInValid.new('[langlist] should not be empty ')
135
+ error_list.push(error)
136
+ else
137
+ langlist.each do |lang, filepath|
138
+ path = filepath.strip
139
+ if path.length <= 0
140
+ error = ErrorUtil::ConfigFileInValid.new("[#{lang}] in [langlist] should not be empty ")
141
+ error_list.push(error)
142
+ else
143
+ if !(Pathname.new path).absolute?
144
+ path = File.expand_path(path,File.dirname(self.configfile_pathstr))
145
+ end
146
+ obj = LangPath.new(lang.to_s, path)
147
+ setting.lang_path_list.push(obj)
157
148
  end
158
- if newplatform != nil
159
- if Platform.is_valid_path(newplatform, path)
160
- newlanglist[lang] = {:xlsheader => arr[0], :path => path}
161
- else
162
- if newplatform == Platform::IOS
163
- error = ErrorUtil::ConfigFileInValid.new("wrong locale file type: IOS should be .strings : #{path}")
149
+ end
150
+ end
151
+
152
+ if sheetname.nil?
153
+ error = ErrorUtil::ConfigFileInValid.new('[sheetname] should not be empty ')
154
+ error_list.push(error)
155
+ elsif !(sheetname.is_a? Hash)
156
+ error = ErrorUtil::ConfigFileInValid.new('[sheetname] wrong format, should be dict')
157
+ error_list.push(error)
158
+ elsif sheetname.length <= 0
159
+ error = ErrorUtil::ConfigFileInValid.new('[sheetname] should not be empty ')
160
+ error_list.push(error)
161
+ else
162
+ sheetname.each do |sheetname, infos|
163
+ if !(infos.is_a? Hash)
164
+ error = ErrorUtil::ConfigFileInValid.new("[sheetname] for item [#{sheetname}] is wrong format ")
165
+ error_list.push(error)
166
+ else
167
+ lang_header_key_dict = {}
168
+ langarr = setting.lang_path_list.map { |langpath| langpath.lang }
169
+ langarr.each do |lang|
170
+ info_lang = infos[lang].to_s.strip
171
+ if info_lang.length <= 0
172
+ error = ErrorUtil::ConfigFileInValid.new("[sheetname] for item [#{sheetname}]: missing lang [#{lang}] ")
173
+ error_list.push(error)
164
174
  else
165
- error = ErrorUtil::ConfigFileInValid.new("wrong locale file type: Android should be .xml : #{path}")
175
+ lang_header_key_dict[lang] = info_lang
166
176
  end
177
+ end
178
+ info_row = infos['row'].to_s.strip
179
+ info_key = infos['key'].to_s.strip
180
+ info_key_str = infos['key_str'].to_s.strip
181
+ if info_row.length > 0 && info_key.length > 0
182
+ obj = SheetInfoByRow.new(info_row.to_i, info_key, lang_header_key_dict)
183
+ sheet = Sheet.new(sheetname,obj)
184
+ setting.sheet_obj_list.push(sheet)
185
+ elsif info_key_str.length > 0
186
+ obj = SheetInfoByHeader.new(info_key_str, lang_header_key_dict)
187
+ sheet = Sheet.new(sheetname,obj)
188
+ setting.sheet_obj_list.push(sheet)
189
+ else
190
+ error = ErrorUtil::ConfigFileInValid.new("[sheetname] for item [#{sheetname}] is wrong format ")
167
191
  error_list.push(error)
168
192
  end
169
193
  end
170
194
  end
171
195
  end
196
+
197
+ setting.printlog
198
+ ErrorUtil::ConfigFileInValid.raiseArr(error_list)
199
+ return setting
172
200
  end
201
+ end
202
+ end
203
+ end
173
204
 
174
- Setting.link = newlink
175
- Setting.platform = newplatform
176
- Setting.keystr = newkeystr
177
- Setting.langlist = newlanglist
178
- Setting.xlsxpath = newxlsxpath
205
+ # config = Applocale::Config::ConfigUtil.new("/Users/kennix.chui/Desktop/programTest/")
206
+ # config.load_configfile_to_setting
207
+ #
208
+ # Applocale::Config::ConfigUtil.create_configfile_ifneed(Applocale::Platform::IOS, "/Users/kennix.chui/Desktop/programTest/")
209
+ #
179
210
 
180
- return error_list
181
- end
211
+ # config = Applocale::Config::ConfigUtil.new("/Users/kennix.chui/Desktop/programTest/")
212
+ # config.load_configfile_to_setting
182
213
 
183
- end
184
- end
214
+
215
+ # Applocale::Config::ConfigUtil.create_configfile_ifneed(Applocale::Platform::IOS, " /Users/kennix.chui/Desktop/programTest/ ")
216
+ # obj = Applocale::ConfigUtil.new()
217
+
218
+ # module Applocale
219
+ # class ConfigUtil
220
+ # def self.create_configfile_ifneed(platform)
221
+ # pathstr = FileUtil.configfile_pathstr
222
+ # self.create_configfile(platform, pathstr) unless File.exist?(pathstr)
223
+ # end
224
+ #
225
+ # def self.create_configfile(platform, configfile_pathstr)
226
+ # src_pathstr = File.expand_path("../../#{FileUtil.filename_config}", __FILE__)
227
+ #
228
+ # File.open(src_pathstr, 'r') do |form|
229
+ # File.open(configfile_pathstr, 'w') do |to|
230
+ # form.each_line do |line|
231
+ # newline = line.gsub("\#{platform}", "#{platform.to_s}")
232
+ # newline = newline.gsub("\#{path_zh_TW}", FileUtil.get_default_localefile_relative_pathstr(platform, Locale::ZH_TW))
233
+ # newline = newline.gsub("\#{path_zh_CN}", FileUtil.get_default_localefile_relative_pathstr(platform, Locale::ZH_CN))
234
+ # newline = newline.gsub("\#{path_en_US}", FileUtil.get_default_localefile_relative_pathstr(platform, Locale::EN_US))
235
+ # newline = newline.gsub("\#{xlsxpath}", FileUtil.get_default_xlsx_relativepath_str)
236
+ # to.puts(newline)
237
+ # end
238
+ #
239
+ # end
240
+ # end
241
+ # end
242
+ #
243
+ # def self.load_and_validate_xlsx_to_localefile(is_local_update, path)
244
+ # config_yaml = self.load_config(path)
245
+ # self.validate_xlsx_to_localefile(config_yaml, is_local_update)
246
+ # end
247
+ #
248
+ # def self.load_and_validate_localefile_to_xlsx(path = nil)
249
+ # config_yaml = self.load_config(path)
250
+ # self.validate_localefile_to_xlsx(config_yaml)
251
+ # end
252
+ #
253
+ # # private
254
+ # def self.load_config(path)
255
+ # configfile_path = FileUtil.configfile_pathstr
256
+ # unless File.exist?(configfile_path)
257
+ # ErrorUtil::MissingConfigFile.new.raise
258
+ # end
259
+ # begin
260
+ # config_yaml = YAML.load_file configfile_path
261
+ # rescue
262
+ # ErrorUtil::ConfigFileInValid.new('ConfigFile format is invalid.').raise
263
+ # end
264
+ # return config_yaml
265
+ # end
266
+ #
267
+ # def self.validate_xlsx_to_localefile(config_yaml, is_local_update)
268
+ # error_list = self.validate_common(config_yaml)
269
+ # if is_local_update
270
+ # unless File.exist? Setting.xlsxpath
271
+ # error = ErrorUtil::ConfigFileInValid.new("#{Setting.xlsxpath} do not exist")
272
+ # error_list.push(error)
273
+ # end
274
+ # else
275
+ # if config_yaml['link'].to_s.strip.nil? || config_yaml['link'].to_s.strip.length <= 0
276
+ # error = ErrorUtil::ConfigFileInValid.new('[link] should not be empty')
277
+ # error_list.push(error)
278
+ # end
279
+ # end
280
+ # ErrorUtil::ConfigFileInValid.raiseArr(error_list)
281
+ # end
282
+ #
283
+ # def self.validate_localefile_to_xlsx(config_yaml)
284
+ # error_list = self.validate_common(config_yaml)
285
+ # Setting.langlist.each do |_, langinfo|
286
+ # unless File.exist? langinfo[:path]
287
+ # error = ErrorUtil::ConfigFileInValid.new("#{langinfo[:path]} do not exist")
288
+ # error_list.push(error)
289
+ # end
290
+ # end
291
+ # ErrorUtil::ConfigFileInValid.raiseArr(error_list)
292
+ # end
293
+ #
294
+ # def self.validate_common(config_yaml)
295
+ # error_list = Array.new
296
+ # link = config_yaml['link'].to_s.strip
297
+ # platform = config_yaml['platform'].to_s.strip
298
+ # keystr = config_yaml['keystr'].to_s.strip
299
+ # langlist = config_yaml['langlist']
300
+ # xlsxpath = config_yaml['xlsxpath'].to_s.strip
301
+ #
302
+ # newlink = nil
303
+ # newplatform = nil
304
+ # newkeystr = nil
305
+ # newlanglist = Hash.new
306
+ # newxlsxpath = nil
307
+ #
308
+ # unless link.nil? || link.length == 0
309
+ # if (link =~ /^https/).nil? && (link =~ /^http/).nil?
310
+ # error = ErrorUtil::ConfigFileInValid.new("Invalid link for [link] : #{link}")
311
+ # error_list.push(error)
312
+ # else
313
+ # newlink = link
314
+ # end
315
+ # end
316
+ #
317
+ # if !(xlsxpath.nil? || xlsxpath.length == 0)
318
+ # if !(Pathname.new xlsxpath).absolute?
319
+ # newxlsxpath = File.expand_path(xlsxpath, File.dirname(FileUtil.configfile_pathstr))
320
+ # else
321
+ # newxlsxpath = xlsxpath
322
+ # end
323
+ # else
324
+ # error = ErrorUtil::ConfigFileInValid.new('[xlsxpath] should not be empty or missing')
325
+ # error_list.push(error)
326
+ # end
327
+ #
328
+ # if platform.nil? || platform.length == 0
329
+ # error = ErrorUtil::ConfigFileInValid.new('[platform] should not be empty')
330
+ # error_list.push(error)
331
+ # else
332
+ # if Platform.init(platform).nil?
333
+ # error = ErrorUtil::ConfigFileInValid.new("[platform] can only be 'ios' or 'android' ")
334
+ # error_list.push(error)
335
+ # else
336
+ # newplatform = Platform.init(platform)
337
+ # end
338
+ # end
339
+ #
340
+ # if keystr.nil? || keystr.length == 0
341
+ # error = ErrorUtil::ConfigFileInValid.new('[keystr] should not be empty')
342
+ # error_list.push(error)
343
+ # else
344
+ # newkeystr = keystr.upcase
345
+ # end
346
+ #
347
+ # if langlist.nil?
348
+ # error = ErrorUtil::ConfigFileInValid.new('[langlist] should not be empty or missing')
349
+ # error_list.push(error)
350
+ # elsif !(langlist.is_a? Hash)
351
+ # error = ErrorUtil::ConfigFileInValid.new('[langlist] wrong format')
352
+ # error_list.push(error)
353
+ # else
354
+ # if langlist.length <= 0
355
+ # error = ErrorUtil::ConfigFileInValid.new('[langlist] should not be empty ')
356
+ # error_list.push(error)
357
+ # end
358
+ # langlist.each do |lang, arr|
359
+ # if arr.length != 2
360
+ # error = ErrorUtil::ConfigFileInValid.new('[langlist] wrong format')
361
+ # error_list.push(error)
362
+ # else
363
+ # path = arr[1]
364
+ # unless (Pathname.new path).absolute?
365
+ # path = File.expand_path(path, File.dirname(FileUtil.configfile_pathstr))
366
+ # end
367
+ # if newplatform != nil
368
+ # if Platform.is_valid_path(newplatform, path)
369
+ # newlanglist[lang] = {:xlsheader => arr[0], :path => path}
370
+ # else
371
+ # if newplatform == Platform::IOS
372
+ # error = ErrorUtil::ConfigFileInValid.new("wrong locale file type: IOS should be .strings : #{path}")
373
+ # else
374
+ # error = ErrorUtil::ConfigFileInValid.new("wrong locale file type: Android should be .xml : #{path}")
375
+ # end
376
+ # error_list.push(error)
377
+ # end
378
+ # end
379
+ # end
380
+ # end
381
+ # end
382
+ #
383
+ # Setting.link = newlink
384
+ # Setting.platform = newplatform
385
+ # Setting.keystr = newkeystr
386
+ # Setting.langlist = newlanglist
387
+ # Setting.xlsxpath = newxlsxpath
388
+ #
389
+ # return error_list
390
+ # end
391
+ #
392
+ # end
393
+ # end