Class: Applocale::Config::ConfigUtil

Inherits:
Object
  • Object
show all
Defined in:
lib/applocale/Util/config_util.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(projectdir_path) ⇒ ConfigUtil

Returns a new instance of ConfigUtil.



14
15
16
17
18
19
20
21
22
# File 'lib/applocale/Util/config_util.rb', line 14

def initialize(projectdir_path)
  projpath = Pathname.new(projectdir_path.strip)
  if File.directory?(projpath)
    self.configfile_pathstr = File.join(projpath, FilePathUtil.default_mainfolder, FilePathUtil.default_config_filename)
    FileUtils.mkdir_p(File.dirname(self.configfile_pathstr))
  else
    ErrorUtil::ConfigFileInValid.new('Project Path is invalid.').raise
  end
end

Instance Attribute Details

#configfile_pathstrObject

Returns the value of attribute configfile_pathstr.



12
13
14
# File 'lib/applocale/Util/config_util.rb', line 12

def configfile_pathstr
  @configfile_pathstr
end

Class Method Details

.create_configfile_ifneed(platform, projectdir_path) ⇒ Object



46
47
48
49
# File 'lib/applocale/Util/config_util.rb', line 46

def self.create_configfile_ifneed(platform, projectdir_path)
  config = ConfigUtil.new(projectdir_path)
  config.create_configfile(platform)
end

Instance Method Details

#create_configfile(platform) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/applocale/Util/config_util.rb', line 25

def create_configfile(platform)
  if !File.exist?(self.configfile_pathstr)
    src_pathstr = File.expand_path("../../#{FilePathUtil.default_config_filename}", __FILE__)
    File.open(src_pathstr, 'r') do |form|
      File.open(configfile_pathstr, 'w') do |to|
        form.each_line do |line|
          newline = line.gsub("\#{platform}", "#{platform.to_s}")
          newline = newline.gsub("\#{path_zh_TW}", FilePathUtil.default_localefile_relative_pathstr(platform, Locale::ZH_TW))
          newline = newline.gsub("\#{path_zh_CN}", FilePathUtil.default_localefile_relative_pathstr(platform, Locale::ZH_CN))
          newline = newline.gsub("\#{path_en_US}", FilePathUtil.default_localefile_relative_pathstr(platform, Locale::EN_US))
          newline = newline.gsub("\#{xlsxpath}", FilePathUtil.default_xlsx_relativepath_str)
          newline = newline.gsub("\#{google_credentials_path}", FilePathUtil.default_google_credentials_filename)
          newline = newline.gsub("\#{export_format}", FilePathUtil.default_export_format.to_s)
          to.puts(newline)
        end
      end
    end
  end
end

#load_configfile_to_settingObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/applocale/Util/config_util.rb', line 75

def load_configfile_to_setting
  error_list = Array.new
  config_yaml, rubycode = load_configfile
  link = config_yaml['link'].to_s.strip
  platform = config_yaml['platform'].to_s.strip
  xlsxpath = config_yaml['xlsxpath'].to_s.strip
  google_credentials_path = config_yaml['google_credentials_path'].to_s.strip
  langlist = config_yaml['langlist']
  sheetname = config_yaml['sheetname']
  export_format = config_yaml['export_format']
  export_to = config_yaml['export_to']

  setting = Applocale::Config::Setting.new(self.configfile_pathstr)
  setting.rubycode = rubycode
  unless link.nil? || link.length == 0
    if (link =~ /^https/).nil? && (link =~ /^http/).nil?
      error = ErrorUtil::ConfigFileInValid.new("Invalid link for [link] : #{link}")
      error_list.push(error)
    else
      setting.link = link
    end
  end

  if platform.nil? || platform.length == 0
    error = ErrorUtil::ConfigFileInValid.new('[platform] should not be empty')
    error_list.push(error)
  else
    if Platform.init(platform).nil?
      error = ErrorUtil::ConfigFileInValid.new("[platform] can only be 'ios', 'android' or 'json'.")
      error_list.push(error)
    else
      setting.platform = Platform.init(platform)
    end
  end

  case export_format
  when 'csv', 'xlsx'
    setting.export_format = export_format
  else
    error = ErrorUtil::ConfigFileInValid.new("[export_format] for item can only be 'csv' or 'xlsx' ")
    error_list.push(error)
  end

  setting.export_to = FilePathUtil.default_export_to

  if !(xlsxpath.nil? || xlsxpath.length == 0)
    if !(Pathname.new xlsxpath).absolute?
      setting.xlsxpath = File.expand_path(xlsxpath, setting.export_to)
    else
      setting.xlsxpath = xlsxpath
    end
  else
    error = ErrorUtil::ConfigFileInValid.new('[xlsxpath] should not be empty or missing')
    error_list.push(error)
  end

  if !(google_credentials_path.nil? || google_credentials_path.length == 0)
    if !(Pathname.new google_credentials_path).absolute?
      setting.google_credentials_path = File.expand_path(google_credentials_path, File.dirname(self.configfile_pathstr))
    else
      setting.google_credentials_path = google_credentials_path
    end
  end

  if langlist.nil?
    error = ErrorUtil::ConfigFileInValid.new('[langlist] should not be empty ')
    error_list.push(error)
  elsif !(langlist.is_a? Hash)
    error = ErrorUtil::ConfigFileInValid.new('[langlist] wrong format')
    error_list.push(error)
  elsif langlist.length <= 0
    error = ErrorUtil::ConfigFileInValid.new('[langlist] should not be empty ')
    error_list.push(error)
  else
    langlist.each do |lang, filepath|
      path = filepath.strip
      if path.length <= 0
        error = ErrorUtil::ConfigFileInValid.new("[#{lang}] in [langlist]  should not be empty ")
        error_list.push(error)
      else
        if !(Pathname.new path).absolute?
          path = File.expand_path(path,File.dirname(self.configfile_pathstr))
        end
        obj = LangPath.new(lang.to_s, path)
        setting.lang_path_list.push(obj)
      end
    end
  end

  if sheetname.nil?
    error = ErrorUtil::ConfigFileInValid.new('[sheetname] should not be empty ')
    error_list.push(error)
  elsif !(sheetname.is_a? Hash)
    error = ErrorUtil::ConfigFileInValid.new('[sheetname] wrong format, should be dict')
    error_list.push(error)
  elsif sheetname.length <= 0
    error = ErrorUtil::ConfigFileInValid.new('[sheetname] should not be empty ')
    error_list.push(error)
  else
    sheetname.each do |sheetname, infos|
      if !(infos.is_a? Hash)
        error = ErrorUtil::ConfigFileInValid.new("[sheetname] for item [#{sheetname}] is wrong format ")
        error_list.push(error)
      else
        lang_header_key_dict = {}
        langarr = setting.lang_path_list.map { |langpath| langpath.lang }
        langarr.each do |lang|
          info_lang = infos[lang].to_s.strip
          if info_lang.length <= 0
            error = ErrorUtil::ConfigFileInValid.new("[sheetname] for item [#{sheetname}]: missing lang [#{lang}] ")
            error_list.push(error)
          else
            lang_header_key_dict[lang] = info_lang
          end
        end
        info_row = infos['row'].to_s.strip
        info_key = infos['key'].to_s.strip
        info_key_str = infos['key_str'].to_s.strip
        if info_row.length > 0 && info_key.length > 0
          obj = SheetInfoByRow.new(info_row.to_i, info_key, lang_header_key_dict)
          sheet = Sheet.new(sheetname,obj)
          setting.sheet_obj_list.push(sheet)
        elsif info_key_str.length > 0
          obj = SheetInfoByHeader.new(info_key_str, lang_header_key_dict)
          sheet = Sheet.new(sheetname,obj)
          setting.sheet_obj_list.push(sheet)
        else
          error = ErrorUtil::ConfigFileInValid.new("[sheetname] for item [#{sheetname}] is wrong format ")
          error_list.push(error)
        end
      end
    end
  end

  setting.printlog
  ErrorUtil::ConfigFileInValid.raiseArr(error_list)
  return setting
end