Class: WebTranslateIt::CommandLine
- Inherits:
-
Object
- Object
- WebTranslateIt::CommandLine
- Defined in:
- lib/web_translate_it/command_line.rb
Overview
rubocop:todo Metrics/ClassLength
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
rubocop:todo Metrics/AbcSize, Metrics/MethodLength.
-
#addlocale ⇒ Object
rubocop:todo Metrics/MethodLength.
- #after_pull_hook ⇒ Object
- #after_push_hook ⇒ Object
- #before_pull_hook ⇒ Object
- #before_push_hook ⇒ Object
- #configuration_file_path ⇒ Object
-
#fetch_locales_to_pull ⇒ Object
rubocop:todo Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity.
-
#fetch_locales_to_push(configuration) ⇒ Object
rubocop:todo Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity.
- #generate_configuration(api_key, project_info) ⇒ Object
-
#init ⇒ Object
rubocop:todo Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity.
-
#initialize(command, command_options, _global_options, parameters, project_path) ⇒ CommandLine
constructor
rubocop:todo Metrics/CyclomaticComplexity, Metrics/MethodLength.
-
#match ⇒ Object
rubocop:todo Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity.
-
#mv ⇒ Object
rubocop:todo Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity.
-
#pull ⇒ Object
rubocop:todo Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity.
-
#push ⇒ Object
rubocop:todo Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity.
-
#rm ⇒ Object
rubocop:todo Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity.
-
#rmlocale ⇒ Object
rubocop:todo Metrics/AbcSize, Metrics/MethodLength.
-
#status ⇒ Object
rubocop:todo Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity.
-
#throb ⇒ Object
rubocop:todo Metrics/AbcSize, Metrics/MethodLength.
Constructor Details
#initialize(command, command_options, _global_options, parameters, project_path) ⇒ CommandLine
rubocop:todo Metrics/CyclomaticComplexity, Metrics/MethodLength
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 35 36 37 38 39 |
# File 'lib/web_translate_it/command_line.rb', line 9 def initialize(command, , , parameters, project_path) # rubocop:todo Metrics/CyclomaticComplexity, Metrics/MethodLength 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 do print " #{}" self.configuration = WebTranslateIt::Configuration.new(project_path, configuration_file_path) print " #{} on #{configuration.project_name}" end end success = send(command) exit 1 unless success end |
Instance Attribute Details
#command_options ⇒ Object
Returns the value of attribute command_options.
7 8 9 |
# File 'lib/web_translate_it/command_line.rb', line 7 def @command_options end |
#configuration ⇒ Object
Returns the value of attribute configuration.
7 8 9 |
# File 'lib/web_translate_it/command_line.rb', line 7 def configuration @configuration end |
#global_options ⇒ Object
Returns the value of attribute global_options.
7 8 9 |
# File 'lib/web_translate_it/command_line.rb', line 7 def @global_options end |
#parameters ⇒ Object
Returns the value of attribute parameters.
7 8 9 |
# File 'lib/web_translate_it/command_line.rb', line 7 def parameters @parameters end |
Instance Method Details
#add ⇒ Object
rubocop:todo Metrics/AbcSize, Metrics/MethodLength
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/web_translate_it/command_line.rb', line 153 def add # rubocop:todo Metrics/AbcSize, Metrics/MethodLength 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 }.to_set { |file| File.(file.file_path) } 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) complete_success = false unless success end else puts 'No new master file to add.' end end complete_success end |
#addlocale ⇒ Object
rubocop:todo Metrics/MethodLength
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/web_translate_it/command_line.rb', line 256 def addlocale # rubocop:todo Metrics/MethodLength $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 |
#after_pull_hook ⇒ Object
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/web_translate_it/command_line.rb', line 94 def after_pull_hook return unless configuration.after_pull output = `#{configuration.after_pull}` if $CHILD_STATUS.success? puts output else abort "Error: after_pull command exited with: #{output}" end end |
#after_push_hook ⇒ Object
142 143 144 145 146 147 148 149 150 151 |
# File 'lib/web_translate_it/command_line.rb', line 142 def after_push_hook return unless configuration.after_push output = `#{configuration.after_push}` if $CHILD_STATUS.success? puts output else abort "Error: after_push command exited with: #{output}" end end |
#before_pull_hook ⇒ Object
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/web_translate_it/command_line.rb', line 83 def before_pull_hook return unless configuration.before_pull output = `#{configuration.before_pull}` if $CHILD_STATUS.success? puts output else abort "Error: before_pull command exited with: #{output}" end end |
#before_push_hook ⇒ Object
131 132 133 134 135 136 137 138 139 140 |
# File 'lib/web_translate_it/command_line.rb', line 131 def before_push_hook return unless configuration.before_push output = `#{configuration.before_push}` if $CHILD_STATUS.success? puts output else abort "Error: before_push command exited with: #{output}" end end |
#configuration_file_path ⇒ Object
410 411 412 413 414 415 416 417 418 419 420 421 422 |
# File 'lib/web_translate_it/command_line.rb', line 410 def configuration_file_path return .config if .config return '.wti' unless File.exist?('config/translation.yml') puts 'Warning: `config/translation.yml` is deprecated in favour of a `.wti` file.' return 'config/translation.yml' unless Util.ask_yes_no('Would you like to migrate your configuration now?', true) return '.wti' if FileUtils.mv('config/translation.yml', '.wti') puts 'Couldn’t move `config/translation.yml`.' false end |
#fetch_locales_to_pull ⇒ Object
rubocop:todo Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 |
# File 'lib/web_translate_it/command_line.rb', line 376 def fetch_locales_to_pull # rubocop:todo Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity 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 elsif configuration.needed_locales.any? locales = configuration.needed_locales else locales = configuration.target_locales configuration.ignore_locales.each { |locale_to_delete| locales.delete(locale_to_delete) } if configuration.ignore_locales.any? end locales.push(configuration.source_locale) if .all locales.uniq end |
#fetch_locales_to_push(configuration) ⇒ Object
rubocop:todo Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 |
# File 'lib/web_translate_it/command_line.rb', line 392 def fetch_locales_to_push(configuration) # rubocop:todo Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity 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 locales.uniq end |
#generate_configuration(api_key, project_info) ⇒ Object
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 |
# File 'lib/web_translate_it/command_line.rb', line 424 def generate_configuration(api_key, project_info) <<~FILE # Required - The Project API Token from WebTranslateIt.com # More information: https://github.com/webtranslateit/webtranslateit/wiki#configuration-file api_key: #{api_key} # Optional - Locales not to sync with WebTranslateIt. # Takes a string, a symbol, or an array of string or symbol. # ignore_locales: [#{project_info['source_locale']['code']}] # Optional - Locales to sync with WebTranslateIt. # Takes a string, a symbol, or an array of string or symbol. # needed_locales: #{project_info['target_locales'].map { |locale| locale['code'] }} # Optional: files not to sync with WebTranslateIt. # Takes an array of globs. # ignore_files: ['somefile*.csv'] # Optional - Hooks # Takes a string containing a command to run. # 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 FILE end |
#init ⇒ Object
rubocop:todo Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/web_translate_it/command_line.rb', line 290 def init # rubocop:todo Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity 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 = JSON.parse WebTranslateIt::Project.fetch_info(api_key) project_info = project['project'] if File.exist?(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.' true end |
#match ⇒ Object
rubocop:todo Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
# File 'lib/web_translate_it/command_line.rb', line 335 def match # rubocop:todo Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity configuration.files.find_all { |mf| mf.locale == configuration.source_locale }.each do |master_file| if File.exist?(master_file.file_path) puts StringUtil.important(master_file.file_path) + " (#{master_file.locale})" else puts StringUtil.failure(master_file.file_path) + " (#{master_file.locale})" end configuration.files.find_all { |f| f.master_id == master_file.id }.each do |file| if File.exist?(file.file_path) puts "- #{file.file_path}" + " (#{file.locale})" else puts StringUtil.failure("- #{file.file_path}") + " (#{file.locale})" end end end true end |
#mv ⇒ Object
rubocop:todo Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/web_translate_it/command_line.rb', line 218 def mv # rubocop:todo Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity 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.exist?(source) success = File.rename(source, destination) if File.exist?(source) puts StringUtil.success("Moved master file #{master_file.file_path}.") if success end complete_success = false unless success configuration.files.find_all { |file| file.master_id == master_file.id }.each do |target_file| if File.exist?(target_file.file_path) success = File.delete(target_file.file_path) complete_success = false unless 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 unless success end puts StringUtil.success('All done.') if complete_success end end end complete_success end |
#pull ⇒ Object
rubocop:todo Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity
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 77 78 79 80 81 |
# File 'lib/web_translate_it/command_line.rb', line 41 def pull # rubocop:todo Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity complete_success = true $stdout.sync = true before_pull_hook # 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.empty? puts 'No files to pull.' else # Now actually pulling files time = Time.now threads = [] n_threads = [(files.size.to_f / 3).ceil, 10].min files.each_slice((files.size.to_f / n_threads).round).each do |file_array| next if file_array.empty? threads << Thread.new(file_array) do |f_array| WebTranslateIt::Connection.new(configuration.api_key) do |http| f_array.each do |file| success = file.fetch(http, .force) complete_success = false unless success end end end end threads.each(&:join) time = Time.now - time puts "Pulled #{files.size} files at #{(files.size / time).round} files/sec, using #{n_threads} threads." after_pull_hook complete_success end end |
#push ⇒ Object
rubocop:todo Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity
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 |
# File 'lib/web_translate_it/command_line.rb', line 105 def push # rubocop:todo Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity puts 'The `--low-priority` option in `wti push --low-priority` was removed and does nothing' if .low_priority complete_success = true $stdout.sync = true before_push_hook WebTranslateIt::Connection.new(configuration.api_key) do |http| fetch_locales_to_push(configuration).each do |locale| files = if parameters.any? configuration.files.find_all { |file| parameters.include?(file.file_path) }.sort { |a, b| a.file_path <=> b.file_path } else configuration.files.find_all { |file| file.locale == locale }.sort { |a, b| a.file_path <=> b.file_path } end if files.empty? 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, [:minor], .force) complete_success = false unless success end end end end after_push_hook complete_success end |
#rm ⇒ Object
rubocop:todo Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity
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 213 214 215 216 |
# File 'lib/web_translate_it/command_line.rb', line 177 def rm # rubocop:todo Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity 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| # rubocop:todo Metrics/BlockLength parameters.each do |param| next unless 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.exist?(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 unless success configuration.files.find_all { |file| file.master_id == master_file.id }.each do |target_file| if File.exist?(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 unless 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 complete_success end |
#rmlocale ⇒ Object
rubocop:todo Metrics/AbcSize, Metrics/MethodLength
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/web_translate_it/command_line.rb', line 272 def rmlocale # rubocop:todo Metrics/AbcSize, Metrics/MethodLength $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| next unless 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 WebTranslateIt::Project.delete_locale(param) end puts 'Done.' end end |
#status ⇒ Object
rubocop:todo Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 |
# File 'lib/web_translate_it/command_line.rb', line 353 def status # rubocop:todo Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity if parameters.any? file = configuration.files.find { |f| parameters.first.strip == f.file_path } abort "File '#{parameters.first}' not found." unless file file_id = file.master_id || file.id puts "Statistics for '#{parameters.first}':" end stats = JSON.parse(Project.fetch_stats(configuration.api_key, file_id)) 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 unless completely_translated exit 101 unless completely_proofread true end |
#throb ⇒ Object
rubocop:todo Metrics/AbcSize, Metrics/MethodLength
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 |
# File 'lib/web_translate_it/command_line.rb', line 458 def throb # rubocop:todo Metrics/AbcSize, Metrics/MethodLength 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 |