Class: WebTranslateIt::CommandLine
- Inherits:
-
Object
- Object
- WebTranslateIt::CommandLine
- Defined in:
- lib/web_translate_it/command_line.rb
Instance Attribute Summary collapse
-
#command_options ⇒ Object
Returns the value of attribute command_options.
-
#configuration ⇒ Object
Returns the value of attribute configuration.
-
#global_options ⇒ Object
Returns the value of attribute global_options.
-
#parameters ⇒ Object
Returns the value of attribute parameters.
Instance Method Summary collapse
- #add ⇒ Object
- #addlocale ⇒ Object
- #configuration_file_path ⇒ Object
- #fetch_locales_to_pull ⇒ Object
- #fetch_locales_to_push(configuration) ⇒ Object
- #generate_configuration(api_key, project_info) ⇒ Object
- #init ⇒ Object
-
#initialize(command, command_options, global_options, parameters, project_path) ⇒ CommandLine
constructor
A new instance of CommandLine.
- #match ⇒ Object
- #mv ⇒ Object
- #pull ⇒ Object
- #push ⇒ Object
- #rm ⇒ Object
- #rmlocale ⇒ Object
- #status ⇒ Object
- #throb ⇒ Object
Constructor Details
#initialize(command, command_options, global_options, parameters, project_path) ⇒ CommandLine
Returns a new instance of CommandLine.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/web_translate_it/command_line.rb', line 8 def initialize(command, , , parameters, project_path) self. = self.parameters = parameters unless command == 'init' case command when 'pull' = "Pulling files" when 'push' = "Pushing files" when 'add' = "Creating master files" when 'rm' = "Deleting files" when 'mv' = "Moving files" when 'addlocale' = "Adding locale" when 'rmlocale' = "Deleting locale" else = "Gathering information" end throb { print " #{}"; self.configuration = WebTranslateIt::Configuration.new(project_path, configuration_file_path); print " #{} on #{self.configuration.project_name}"; } end success = self.send(command) exit 1 if !success end |
Instance Attribute Details
#command_options ⇒ Object
Returns the value of attribute command_options.
6 7 8 |
# File 'lib/web_translate_it/command_line.rb', line 6 def @command_options end |
#configuration ⇒ Object
Returns the value of attribute configuration.
6 7 8 |
# File 'lib/web_translate_it/command_line.rb', line 6 def configuration @configuration end |
#global_options ⇒ Object
Returns the value of attribute global_options.
6 7 8 |
# File 'lib/web_translate_it/command_line.rb', line 6 def @global_options end |
#parameters ⇒ Object
Returns the value of attribute parameters.
6 7 8 |
# File 'lib/web_translate_it/command_line.rb', line 6 def parameters @parameters end |
Instance Method Details
#add ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/web_translate_it/command_line.rb', line 103 def add complete_success = true STDOUT.sync = true if parameters == [] puts StringUtil.failure("Error: You must provide the path to the master file to add.") puts "Usage: wti add path/to/master_file_1 path/to/master_file_2 ..." exit end WebTranslateIt::Connection.new(configuration.api_key) do |http| added = configuration.files.find_all{ |file| file.locale == configuration.source_locale}.collect {|file| File.(file.file_path) }.to_set to_add = parameters.reject{ |param| added.include?(File.(param))} if to_add.any? to_add.each do |param| file = TranslationFile.new(nil, param.gsub(/ /, "\\ "), nil, configuration.api_key) success = file.create(http, .low_priority) complete_success = false if !success end else puts "No new master file to add." end end complete_success end |
#addlocale ⇒ Object
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/web_translate_it/command_line.rb', line 206 def addlocale STDOUT.sync = true if parameters == [] puts StringUtil.failure("Locale code missing.") puts "Usage: wti addlocale fr es ..." exit 1 end parameters.each do |param| print StringUtil.success("Adding locale #{param.upcase}... ") WebTranslateIt::Connection.new(configuration.api_key) do WebTranslateIt::Project.create_locale(param) end puts "Done." end end |
#configuration_file_path ⇒ Object
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 |
# File 'lib/web_translate_it/command_line.rb', line 358 def configuration_file_path if self..config return self..config else if File.exists?('config/translation.yml') puts "Warning: `config/translation.yml` is deprecated in favour of a `.wti` file." if Util.ask_yes_no("Would you like to migrate your configuration now?", true) require 'fileutils' if FileUtils.mv('config/translation.yml', '.wti') return '.wti' else puts "Couldn’t move `config/translation.yml`." return false end else return 'config/translation.yml' end else return '.wti' end end end |
#fetch_locales_to_pull ⇒ Object
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 |
# File 'lib/web_translate_it/command_line.rb', line 320 def fetch_locales_to_pull if .locale .locale.split.each do |locale| puts "Locale #{locale} doesn't exist -- `wti addlocale #{locale}` to add it." unless configuration.target_locales.include?(locale) end locales = .locale.split else if configuration.needed_locales.any? locales = configuration.needed_locales else locales = configuration.target_locales if configuration.ignore_locales.any? configuration.ignore_locales.each{ |locale_to_delete| locales.delete(locale_to_delete) } end end end locales.push(configuration.source_locale) if .all return locales.uniq end |
#fetch_locales_to_push(configuration) ⇒ Object
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 |
# File 'lib/web_translate_it/command_line.rb', line 340 def fetch_locales_to_push(configuration) if .locale .locale.split.each do |locale| puts "Locale #{locale} doesn't exist -- `wti addlocale #{locale}` to add it." unless configuration.target_locales.include?(locale) end locales = .locale.split else locales = [configuration.source_locale] end if .all puts "`wti push --all` was deprecated in wti 2.3. Use `wti push --target` instead." return [] elsif .target locales = configuration.target_locales.reject{ |locale| locale == configuration.source_locale } end return locales.uniq end |
#generate_configuration(api_key, project_info) ⇒ Object
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 |
# File 'lib/web_translate_it/command_line.rb', line 381 def generate_configuration(api_key, project_info) file = <<-FILE api_key: #{api_key} # Optional: locales not to sync with WebTranslateIt. # Takes a string, a symbol, or an array of string or symbol. # More information here: https://github.com/AtelierConvivialite/webtranslateit/wiki # ignore_locales: '#{project_info["source_locale"]["code"]}' # Or if you prefer a list of locales to sync with WebTranslateIt: # needed_locales: #{project_info["target_locales"].map {|locale| locale["code"]}.to_s} # Optional # before_pull: "echo 'some unix command'" # Command executed before pulling files # after_pull: "touch tmp/restart.txt" # Command executed after pulling files # # before_push: "echo 'some unix command'" # Command executed before pushing files # after_push: "touch tmp/restart.txt" # Command executed after pushing files # Silence SSL errors # silence_errors: true FILE return file end |
#init ⇒ Object
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/web_translate_it/command_line.rb', line 240 def init puts "# Initializing project" if parameters.any? api_key = parameters[0] path = '.wti' else api_key = Util.ask(" Project API Key:") path = Util.ask(" Path to configuration file:", '.wti') end FileUtils.mkpath(path.split('/')[0..path.split('/').size-2].join('/')) unless path.split('/').size == 1 project = YAML.load WebTranslateIt::Project.fetch_info(api_key) project_info = project['project'] if File.exists?(path) && !File.writable?(path) puts StringUtil.failure("Error: `#{path}` file is not writable.") exit 1 end File.open(path, 'w'){ |file| file << generate_configuration(api_key, project_info) } puts "" puts " The project #{project_info['name']} was successfully initialized." puts "" if project_info["source_locale"]["code"].nil? || project_info["target_locales"].size <= 1 || project_info["project_files"].none? puts "" puts " There are a few more things to set up:" puts "" end if project_info["source_locale"]["code"].nil? puts " *) You don't have a source locale setup." puts " Add the source locale with: `wti addlocale <locale_code>`" puts "" end if project_info["target_locales"].size <= 1 puts " *) You don't have a target locale setup." puts " Add the first target locale with: `wti addlocale <locale_code>`" puts "" end if project_info["project_files"].none? puts " *) You don't have linguistic files setup." puts " Add a master file with: `wti add <path/to/file.xml>`" puts "" end puts "You can now use `wti` to push and pull your language files." puts "Check `wti --help` for help." return true end |
#match ⇒ Object
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
# File 'lib/web_translate_it/command_line.rb', line 285 def match configuration.files.find_all{ |mf| mf.locale == configuration.source_locale }.each do |master_file| if !File.exists?(master_file.file_path) puts StringUtil.failure(master_file.file_path) + " (#{master_file.locale})" else puts StringUtil.important(master_file.file_path) + " (#{master_file.locale})" end configuration.files.find_all{ |f| f.master_id == master_file.id }.each do |file| if !File.exists?(file.file_path) puts StringUtil.failure("- #{file.file_path}") + " (#{file.locale})" else puts "- #{file.file_path}" + " (#{file.locale})" end end end return true end |
#mv ⇒ Object
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 |
# File 'lib/web_translate_it/command_line.rb', line 168 def mv complete_success = true STDOUT.sync = true if parameters.count != 2 puts StringUtil.failure("Error: You must provide the source path and destination path of the master file to move.") puts "Usage: wti mv path/to/master_file_old_path path/to/master_file_new_path ..." exit end source = parameters[0] destination = parameters[1] WebTranslateIt::Connection.new(configuration.api_key) do |http| if Util.ask_yes_no("Are you sure you want to move the master file #{source} and its target files?", true) configuration.files.find_all{ |file| file.file_path == source }.each do |master_file| master_file.upload(http, false, false, nil, false, false, true, true, destination) # move master file if File.exists?(source) success = File.rename(source, destination) if File.exists?(source) puts StringUtil.success("Moved master file #{master_file.file_path}.") if success end complete_success = false if !success configuration.files.find_all{ |file| file.master_id == master_file.id }.each do |target_file| if File.exists?(target_file.file_path) success = File.delete(target_file.file_path) complete_success = false if !success end end configuration.reload configuration.files.find_all{ |file| file.master_id == master_file.id }.each do |target_file| success = target_file.fetch(http) complete_success = false if !success end puts StringUtil.success("All done.") if complete_success end end end complete_success end |
#pull ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/web_translate_it/command_line.rb', line 36 def pull complete_success = true STDOUT.sync = true `#{configuration.before_pull}` if configuration.before_pull # Selecting files to pull files = [] fetch_locales_to_pull.each do |locale| files |= configuration.files.find_all{ |file| file.locale == locale } end found_files = [] parameters.each do |parameter| found_files += files.find_all{ |file| File.fnmatch(parameter, file.file_path) } end files = found_files if parameters.any? files = files.uniq.sort{ |a,b| a.file_path <=> b.file_path } if files.size == 0 puts "No files to pull." else # Now actually pulling files time = Time.now threads = [] n_threads = (files.size.to_f/3).ceil >= 10 ? 10 : (files.size.to_f/3).ceil ArrayUtil.chunk(files, n_threads).each do |file_array| unless file_array.empty? threads << Thread.new(file_array) do |file_array| WebTranslateIt::Connection.new(configuration.api_key) do |http| file_array.each do |file| success = file.fetch(http, .force) complete_success = false if !success end end end end end threads.each { |thread| thread.join } time = Time.now - time puts "Pulled #{files.size} files at #{(files.size/time).round} files/sec, using #{n_threads} threads." `#{configuration.after_pull}` if configuration.after_pull complete_success end end |
#push ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/web_translate_it/command_line.rb', line 78 def push complete_success = true STDOUT.sync = true `#{configuration.before_push}` if configuration.before_push WebTranslateIt::Connection.new(configuration.api_key) do |http| fetch_locales_to_push(configuration).each do |locale| if parameters.any? files = configuration.files.find_all{ |file| parameters.include?(file.file_path) }.sort{|a,b| a.file_path <=> b.file_path} else files = configuration.files.find_all{ |file| file.locale == locale }.sort{|a,b| a.file_path <=> b.file_path} end if files.size == 0 puts "Couldn't find any local files registered on WebTranslateIt to push." else files.each do |file| success = file.upload(http, [:merge], .ignore_missing, .label, .low_priority, [:minor], .force) complete_success = false if !success end end end end `#{configuration.after_push}` if configuration.after_push complete_success end |
#rm ⇒ Object
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 |
# File 'lib/web_translate_it/command_line.rb', line 127 def rm complete_success = true STDOUT.sync = true if parameters == [] puts StringUtil.failure("Error: You must provide the path to the master file to remove.") puts "Usage: wti rm path/to/master_file_1 path/to/master_file_2 ..." exit end WebTranslateIt::Connection.new(configuration.api_key) do |http| parameters.each do |param| if Util.ask_yes_no("Are you sure you want to delete the master file #{param}?\nThis will also delete its target files and translations.", false) files = configuration.files.find_all{ |file| file.file_path == param } if files.any? files.each do |master_file| master_file.delete(http) # delete files if File.exists?(master_file.file_path) success = File.delete(master_file.file_path) puts StringUtil.success("Deleted master file #{master_file.file_path}.") if success end complete_success = false if !success configuration.files.find_all{ |file| file.master_id == master_file.id }.each do |target_file| if File.exists?(target_file.file_path) success = File.delete(target_file.file_path) puts StringUtil.success("Deleted target file #{target_file.file_path}.") if success else puts StringUtil.failure("Target file #{target_file.file_path} doesn’t exist locally") end complete_success = false if !success end end puts StringUtil.success("All done.") if complete_success else puts StringUtil.failure("#{param}: File doesn’t exist on project.") end end end end complete_success end |
#rmlocale ⇒ Object
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/web_translate_it/command_line.rb', line 222 def rmlocale STDOUT.sync = true if parameters == [] puts StringUtil.failure("Error: You must provide the locale code to remove.") puts "Usage: wti rmlocale fr es ..." exit 1 end parameters.each do |param| if Util.ask_yes_no("Are you certain you want to delete the locale #{param.upcase}?\nThis will also delete its files and translations.", false) print StringUtil.success("Deleting locale #{param.upcase}... ") WebTranslateIt::Connection.new(configuration.api_key) do |http| WebTranslateIt::Project.delete_locale(param) end puts "Done." end end end |
#status ⇒ Object
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
# File 'lib/web_translate_it/command_line.rb', line 303 def status stats = YAML.load(Project.fetch_stats(configuration.api_key)) stale = false completely_translated = true completely_proofread = true stats.each do |locale, values| percent_translated = Util.calculate_percentage(values['count_strings_to_proofread'].to_i + values['count_strings_done'].to_i + values['count_strings_to_verify'].to_i, values['count_strings'].to_i) percent_completed = Util.calculate_percentage(values['count_strings_done'].to_i, values['count_strings'].to_i) completely_translated = false if percent_translated != 100 completely_proofread = false if percent_completed != 100 puts "#{locale}: #{percent_translated}% translated, #{percent_completed}% completed." end exit 100 if !completely_translated exit 101 if !completely_proofread return true end |
#throb ⇒ Object
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 |
# File 'lib/web_translate_it/command_line.rb', line 407 def throb throb = %w(⠋ ⠙ ⠹ ⠸ ⠼ ⠴ ⠦ ⠧ ⠇ ⠏) throb.reverse! if rand > 0.5 i = rand throb.length thread = Thread.new do dot = lambda do print "\r#{throb[i]}\e[?25l" i = (i + 1) % throb.length sleep 0.1 and dot.call end dot.call end yield ensure if thread thread.kill puts "\r\e[0G#\e[?25h" end end |