web_translate_it 2.5.3 → 2.6.1
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 +4 -4
- data/bin/wti +107 -107
- data/generators/webtranslateit/lib/insert_commands.rb +6 -8
- data/generators/webtranslateit/webtranslateit_generator.rb +6 -6
- data/history.md +22 -7
- data/lib/web_translate_it/auto_fetch.rb +2 -4
- data/lib/web_translate_it/command_line.rb +264 -230
- data/lib/web_translate_it/configuration.rb +32 -29
- data/lib/web_translate_it/connection.rb +22 -34
- data/lib/web_translate_it/project.rb +22 -25
- data/lib/web_translate_it/string.rb +85 -84
- data/lib/web_translate_it/term.rb +65 -65
- data/lib/web_translate_it/term_translation.rb +34 -36
- data/lib/web_translate_it/translation.rb +25 -32
- data/lib/web_translate_it/translation_file.rb +109 -106
- data/lib/web_translate_it/util/array_util.rb +8 -8
- data/lib/web_translate_it/util/hash_util.rb +5 -5
- data/lib/web_translate_it/util/string_util.rb +5 -8
- data/lib/web_translate_it/util.rb +51 -54
- data/lib/web_translate_it.rb +7 -7
- data/readme.md +13 -9
- data/spec/spec_helper.rb +2 -3
- data/spec/web_translate_it/auto_fetch_spec.rb +1 -2
- data/spec/web_translate_it/string_spec.rb +61 -62
- data/spec/web_translate_it/term_spec.rb +40 -41
- metadata +47 -12
| @@ -1,213 +1,260 @@ | |
| 1 | 
            -
            # encoding: utf-8
         | 
| 2 1 | 
             
            module WebTranslateIt
         | 
| 3 | 
            -
              class CommandLine
         | 
| 2 | 
            +
              class CommandLine # rubocop:todo Metrics/ClassLength
         | 
| 4 3 | 
             
                require 'fileutils'
         | 
| 5 4 | 
             
                require 'set'
         | 
| 6 5 | 
             
                attr_accessor :configuration, :global_options, :command_options, :parameters
         | 
| 7 6 |  | 
| 8 | 
            -
                def initialize(command, command_options, global_options, parameters, project_path)
         | 
| 7 | 
            +
                def initialize(command, command_options, global_options, parameters, project_path) # rubocop:todo Metrics/CyclomaticComplexity, Metrics/MethodLength
         | 
| 9 8 | 
             
                  self.command_options = command_options
         | 
| 10 9 | 
             
                  self.parameters = parameters
         | 
| 11 10 | 
             
                  unless command == 'init'
         | 
| 12 | 
            -
                    case command
         | 
| 13 | 
            -
             | 
| 14 | 
            -
             | 
| 15 | 
            -
             | 
| 16 | 
            -
             | 
| 17 | 
            -
             | 
| 18 | 
            -
             | 
| 19 | 
            -
             | 
| 20 | 
            -
             | 
| 21 | 
            -
             | 
| 22 | 
            -
             | 
| 23 | 
            -
             | 
| 24 | 
            -
             | 
| 25 | 
            -
             | 
| 26 | 
            -
             | 
| 27 | 
            -
             | 
| 28 | 
            -
             | 
| 11 | 
            +
                    message = case command
         | 
| 12 | 
            +
                              when 'pull'
         | 
| 13 | 
            +
                                'Pulling files'
         | 
| 14 | 
            +
                              when 'push'
         | 
| 15 | 
            +
                                'Pushing files'
         | 
| 16 | 
            +
                              when 'add'
         | 
| 17 | 
            +
                                'Creating master files'
         | 
| 18 | 
            +
                              when 'rm'
         | 
| 19 | 
            +
                                'Deleting files'
         | 
| 20 | 
            +
                              when 'mv'
         | 
| 21 | 
            +
                                'Moving files'
         | 
| 22 | 
            +
                              when 'addlocale'
         | 
| 23 | 
            +
                                'Adding locale'
         | 
| 24 | 
            +
                              when 'rmlocale'
         | 
| 25 | 
            +
                                'Deleting locale'
         | 
| 26 | 
            +
                              else
         | 
| 27 | 
            +
                                'Gathering information'
         | 
| 28 | 
            +
                              end
         | 
| 29 | 
            +
                    throb do
         | 
| 30 | 
            +
                      print "  #{message}"
         | 
| 31 | 
            +
                      self.configuration = WebTranslateIt::Configuration.new(project_path, configuration_file_path)
         | 
| 32 | 
            +
                      print " #{message} on #{configuration.project_name}"
         | 
| 29 33 | 
             
                    end
         | 
| 30 | 
            -
                    throb { print "  #{message}"; self.configuration = WebTranslateIt::Configuration.new(project_path, configuration_file_path); print " #{message} on #{self.configuration.project_name}"; }
         | 
| 31 34 | 
             
                  end
         | 
| 32 | 
            -
                  success =  | 
| 33 | 
            -
                  exit 1  | 
| 35 | 
            +
                  success = send(command)
         | 
| 36 | 
            +
                  exit 1 unless success
         | 
| 34 37 | 
             
                end
         | 
| 35 | 
            -
             | 
| 36 | 
            -
                def pull
         | 
| 38 | 
            +
             | 
| 39 | 
            +
                def pull # rubocop:todo Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity
         | 
| 37 40 | 
             
                  complete_success = true
         | 
| 38 | 
            -
                   | 
| 39 | 
            -
                   | 
| 41 | 
            +
                  $stdout.sync = true
         | 
| 42 | 
            +
                  before_pull_hook
         | 
| 40 43 | 
             
                  # Selecting files to pull
         | 
| 41 44 | 
             
                  files = []
         | 
| 42 45 | 
             
                  fetch_locales_to_pull.each do |locale|
         | 
| 43 | 
            -
                    files |= configuration.files.find_all{ |file| file.locale == locale }
         | 
| 46 | 
            +
                    files |= configuration.files.find_all { |file| file.locale == locale }
         | 
| 44 47 | 
             
                  end
         | 
| 45 48 | 
             
                  found_files = []
         | 
| 46 49 | 
             
                  parameters.each do |parameter|
         | 
| 47 | 
            -
                    found_files += files.find_all{ |file| File.fnmatch(parameter, file.file_path) }
         | 
| 50 | 
            +
                    found_files += files.find_all { |file| File.fnmatch(parameter, file.file_path) }
         | 
| 48 51 | 
             
                  end
         | 
| 49 52 | 
             
                  files = found_files if parameters.any?
         | 
| 50 | 
            -
                  files = files.uniq.sort{ |a,b| a.file_path <=> b.file_path }
         | 
| 51 | 
            -
                  if files. | 
| 52 | 
            -
                    puts  | 
| 53 | 
            +
                  files = files.uniq.sort { |a, b| a.file_path <=> b.file_path }
         | 
| 54 | 
            +
                  if files.empty?
         | 
| 55 | 
            +
                    puts 'No files to pull.'
         | 
| 53 56 | 
             
                  else
         | 
| 54 57 | 
             
                    # Now actually pulling files
         | 
| 55 58 | 
             
                    time = Time.now
         | 
| 56 59 | 
             
                    threads = []
         | 
| 57 | 
            -
                    n_threads = (files.size.to_f/3).ceil >= 10 ? 10 : (files.size.to_f/3).ceil
         | 
| 60 | 
            +
                    n_threads = (files.size.to_f / 3).ceil >= 10 ? 10 : (files.size.to_f / 3).ceil
         | 
| 58 61 | 
             
                    ArrayUtil.chunk(files, n_threads).each do |file_array|
         | 
| 59 | 
            -
                       | 
| 60 | 
            -
             | 
| 61 | 
            -
             | 
| 62 | 
            -
             | 
| 63 | 
            -
             | 
| 64 | 
            -
             | 
| 65 | 
            -
                             | 
| 62 | 
            +
                      next if file_array.empty?
         | 
| 63 | 
            +
             | 
| 64 | 
            +
                      threads << Thread.new(file_array) do |f_array|
         | 
| 65 | 
            +
                        WebTranslateIt::Connection.new(configuration.api_key) do |http|
         | 
| 66 | 
            +
                          f_array.each do |file|
         | 
| 67 | 
            +
                            success = file.fetch(http, command_options.force)
         | 
| 68 | 
            +
                            complete_success = false unless success
         | 
| 66 69 | 
             
                          end
         | 
| 67 70 | 
             
                        end
         | 
| 68 71 | 
             
                      end
         | 
| 69 72 | 
             
                    end
         | 
| 70 | 
            -
                    threads.each | 
| 73 | 
            +
                    threads.each(&:join)
         | 
| 71 74 | 
             
                    time = Time.now - time
         | 
| 72 | 
            -
                    puts "Pulled #{files.size} files at #{(files.size/time).round} files/sec, using #{n_threads} threads."
         | 
| 73 | 
            -
                     | 
| 75 | 
            +
                    puts "Pulled #{files.size} files at #{(files.size / time).round} files/sec, using #{n_threads} threads."
         | 
| 76 | 
            +
                    after_pull_hook
         | 
| 74 77 | 
             
                    complete_success
         | 
| 75 78 | 
             
                  end
         | 
| 76 79 | 
             
                end
         | 
| 77 | 
            -
             | 
| 78 | 
            -
                def  | 
| 80 | 
            +
             | 
| 81 | 
            +
                def before_pull_hook
         | 
| 82 | 
            +
                  return unless configuration.before_pull
         | 
| 83 | 
            +
             | 
| 84 | 
            +
                  output = `#{configuration.before_pull}`
         | 
| 85 | 
            +
                  if $CHILD_STATUS.success?
         | 
| 86 | 
            +
                    puts output
         | 
| 87 | 
            +
                  else
         | 
| 88 | 
            +
                    abort "Error: before_pull command exited with: #{output}"
         | 
| 89 | 
            +
                  end
         | 
| 90 | 
            +
                end
         | 
| 91 | 
            +
             | 
| 92 | 
            +
                def after_pull_hook
         | 
| 93 | 
            +
                  return unless configuration.after_pull
         | 
| 94 | 
            +
             | 
| 95 | 
            +
                  output = `#{configuration.after_pull}`
         | 
| 96 | 
            +
                  if $CHILD_STATUS.success?
         | 
| 97 | 
            +
                    puts output
         | 
| 98 | 
            +
                  else
         | 
| 99 | 
            +
                    abort "Error: after_pull command exited with: #{output}"
         | 
| 100 | 
            +
                  end
         | 
| 101 | 
            +
                end
         | 
| 102 | 
            +
             | 
| 103 | 
            +
                def push # rubocop:todo Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity
         | 
| 79 104 | 
             
                  complete_success = true
         | 
| 80 | 
            -
                   | 
| 81 | 
            -
                   | 
| 105 | 
            +
                  $stdout.sync = true
         | 
| 106 | 
            +
                  before_push_hook
         | 
| 82 107 | 
             
                  WebTranslateIt::Connection.new(configuration.api_key) do |http|
         | 
| 83 108 | 
             
                    fetch_locales_to_push(configuration).each do |locale|
         | 
| 84 | 
            -
                      if parameters.any?
         | 
| 85 | 
            -
             | 
| 86 | 
            -
             | 
| 87 | 
            -
             | 
| 88 | 
            -
             | 
| 89 | 
            -
                      if files. | 
| 109 | 
            +
                      files = if parameters.any?
         | 
| 110 | 
            +
                                configuration.files.find_all { |file| parameters.include?(file.file_path) }.sort { |a, b| a.file_path <=> b.file_path }
         | 
| 111 | 
            +
                              else
         | 
| 112 | 
            +
                                configuration.files.find_all { |file| file.locale == locale }.sort { |a, b| a.file_path <=> b.file_path }
         | 
| 113 | 
            +
                              end
         | 
| 114 | 
            +
                      if files.empty?
         | 
| 90 115 | 
             
                        puts "Couldn't find any local files registered on WebTranslateIt to push."
         | 
| 91 116 | 
             
                      else
         | 
| 92 117 | 
             
                        files.each do |file|
         | 
| 93 118 | 
             
                          success = file.upload(http, command_options[:merge], command_options.ignore_missing, command_options.label, command_options.low_priority, command_options[:minor], command_options.force)
         | 
| 94 | 
            -
                          complete_success = false  | 
| 119 | 
            +
                          complete_success = false unless success
         | 
| 95 120 | 
             
                        end
         | 
| 96 121 | 
             
                      end
         | 
| 97 122 | 
             
                    end
         | 
| 98 123 | 
             
                  end
         | 
| 99 | 
            -
                   | 
| 124 | 
            +
                  after_push_hook
         | 
| 100 125 | 
             
                  complete_success
         | 
| 101 126 | 
             
                end
         | 
| 102 | 
            -
             | 
| 103 | 
            -
                def  | 
| 127 | 
            +
             | 
| 128 | 
            +
                def before_push_hook
         | 
| 129 | 
            +
                  return unless configuration.before_push
         | 
| 130 | 
            +
             | 
| 131 | 
            +
                  output = `#{configuration.before_push}`
         | 
| 132 | 
            +
                  if $CHILD_STATUS.success?
         | 
| 133 | 
            +
                    puts output
         | 
| 134 | 
            +
                  else
         | 
| 135 | 
            +
                    abort "Error: before_push command exited with: #{output}"
         | 
| 136 | 
            +
                  end
         | 
| 137 | 
            +
                end
         | 
| 138 | 
            +
             | 
| 139 | 
            +
                def after_push_hook
         | 
| 140 | 
            +
                  return unless configuration.after_push
         | 
| 141 | 
            +
             | 
| 142 | 
            +
                  output = `#{configuration.after_push}`
         | 
| 143 | 
            +
                  if $CHILD_STATUS.success?
         | 
| 144 | 
            +
                    puts output
         | 
| 145 | 
            +
                  else
         | 
| 146 | 
            +
                    abort "Error: after_push command exited with: #{output}"
         | 
| 147 | 
            +
                  end
         | 
| 148 | 
            +
                end
         | 
| 149 | 
            +
             | 
| 150 | 
            +
                def add # rubocop:todo Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity
         | 
| 104 151 | 
             
                  complete_success = true
         | 
| 105 | 
            -
                   | 
| 152 | 
            +
                  $stdout.sync = true
         | 
| 106 153 | 
             
                  if parameters == []
         | 
| 107 | 
            -
                    puts StringUtil.failure( | 
| 108 | 
            -
                    puts  | 
| 154 | 
            +
                    puts StringUtil.failure('Error: You must provide the path to the master file to add.')
         | 
| 155 | 
            +
                    puts 'Usage: wti add path/to/master_file_1 path/to/master_file_2 ...'
         | 
| 109 156 | 
             
                    exit
         | 
| 110 157 | 
             
                  end
         | 
| 111 158 | 
             
                  WebTranslateIt::Connection.new(configuration.api_key) do |http|
         | 
| 112 | 
            -
             | 
| 113 | 
            -
                    to_add = parameters.reject{ |param| added.include?(File.expand_path(param))}
         | 
| 159 | 
            +
                    added = configuration.files.find_all { |file| file.locale == configuration.source_locale }.collect { |file| File.expand_path(file.file_path) }.to_set
         | 
| 160 | 
            +
                    to_add = parameters.reject { |param| added.include?(File.expand_path(param)) }
         | 
| 114 161 | 
             
                    if to_add.any?
         | 
| 115 162 | 
             
                      to_add.each do |param|
         | 
| 116 | 
            -
                        file = TranslationFile.new(nil, param.gsub(/ /,  | 
| 163 | 
            +
                        file = TranslationFile.new(nil, param.gsub(/ /, '\\ '), nil, configuration.api_key)
         | 
| 117 164 | 
             
                        success = file.create(http, command_options.low_priority)
         | 
| 118 | 
            -
                        complete_success = false  | 
| 165 | 
            +
                        complete_success = false unless success
         | 
| 119 166 | 
             
                      end
         | 
| 120 167 | 
             
                    else
         | 
| 121 | 
            -
                      puts  | 
| 168 | 
            +
                      puts 'No new master file to add.'
         | 
| 122 169 | 
             
                    end
         | 
| 123 170 | 
             
                  end
         | 
| 124 171 | 
             
                  complete_success
         | 
| 125 172 | 
             
                end
         | 
| 126 173 |  | 
| 127 | 
            -
                def rm
         | 
| 174 | 
            +
                def rm # rubocop:todo Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity
         | 
| 128 175 | 
             
                  complete_success = true
         | 
| 129 | 
            -
                   | 
| 176 | 
            +
                  $stdout.sync = true
         | 
| 130 177 | 
             
                  if parameters == []
         | 
| 131 | 
            -
                    puts StringUtil.failure( | 
| 132 | 
            -
                    puts  | 
| 178 | 
            +
                    puts StringUtil.failure('Error: You must provide the path to the master file to remove.')
         | 
| 179 | 
            +
                    puts 'Usage: wti rm path/to/master_file_1 path/to/master_file_2 ...'
         | 
| 133 180 | 
             
                    exit
         | 
| 134 181 | 
             
                  end
         | 
| 135 | 
            -
                  WebTranslateIt::Connection.new(configuration.api_key) do |http|
         | 
| 182 | 
            +
                  WebTranslateIt::Connection.new(configuration.api_key) do |http| # rubocop:todo Metrics/BlockLength
         | 
| 136 183 | 
             
                    parameters.each do |param|
         | 
| 137 | 
            -
                       | 
| 138 | 
            -
             | 
| 139 | 
            -
             | 
| 140 | 
            -
             | 
| 141 | 
            -
             | 
| 142 | 
            -
             | 
| 143 | 
            -
             | 
| 144 | 
            -
             | 
| 145 | 
            -
             | 
| 146 | 
            -
                             | 
| 147 | 
            -
             | 
| 148 | 
            -
             | 
| 149 | 
            -
             | 
| 150 | 
            -
             | 
| 151 | 
            -
             | 
| 152 | 
            -
                               | 
| 153 | 
            -
             | 
| 154 | 
            -
                               | 
| 155 | 
            -
                              complete_success = false if !success
         | 
| 184 | 
            +
                      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)
         | 
| 185 | 
            +
             | 
| 186 | 
            +
                      files = configuration.files.find_all { |file| file.file_path == param }
         | 
| 187 | 
            +
                      if files.any?
         | 
| 188 | 
            +
                        files.each do |master_file|
         | 
| 189 | 
            +
                          master_file.delete(http)
         | 
| 190 | 
            +
                          # delete files
         | 
| 191 | 
            +
                          if File.exist?(master_file.file_path)
         | 
| 192 | 
            +
                            success = File.delete(master_file.file_path)
         | 
| 193 | 
            +
                            puts StringUtil.success("Deleted master file #{master_file.file_path}.") if success
         | 
| 194 | 
            +
                          end
         | 
| 195 | 
            +
                          complete_success = false unless success
         | 
| 196 | 
            +
                          configuration.files.find_all { |file| file.master_id == master_file.id }.each do |target_file|
         | 
| 197 | 
            +
                            if File.exist?(target_file.file_path)
         | 
| 198 | 
            +
                              success = File.delete(target_file.file_path)
         | 
| 199 | 
            +
                              puts StringUtil.success("Deleted target file #{target_file.file_path}.") if success
         | 
| 200 | 
            +
                            else
         | 
| 201 | 
            +
                              puts StringUtil.failure("Target file #{target_file.file_path} doesn’t exist locally")
         | 
| 156 202 | 
             
                            end
         | 
| 203 | 
            +
                            complete_success = false unless success
         | 
| 157 204 | 
             
                          end
         | 
| 158 | 
            -
                          puts StringUtil.success("All done.") if complete_success
         | 
| 159 | 
            -
                        else
         | 
| 160 | 
            -
                          puts StringUtil.failure("#{param}: File doesn’t exist on project.")
         | 
| 161 205 | 
             
                        end
         | 
| 206 | 
            +
                        puts StringUtil.success('All done.') if complete_success
         | 
| 207 | 
            +
                      else
         | 
| 208 | 
            +
                        puts StringUtil.failure("#{param}: File doesn’t exist on project.")
         | 
| 162 209 | 
             
                      end
         | 
| 163 210 | 
             
                    end
         | 
| 164 211 | 
             
                  end
         | 
| 165 212 | 
             
                  complete_success
         | 
| 166 213 | 
             
                end
         | 
| 167 | 
            -
             | 
| 168 | 
            -
                def mv
         | 
| 214 | 
            +
             | 
| 215 | 
            +
                def mv # rubocop:todo Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity
         | 
| 169 216 | 
             
                  complete_success = true
         | 
| 170 | 
            -
                   | 
| 217 | 
            +
                  $stdout.sync = true
         | 
| 171 218 | 
             
                  if parameters.count != 2
         | 
| 172 | 
            -
                    puts StringUtil.failure( | 
| 173 | 
            -
                    puts  | 
| 219 | 
            +
                    puts StringUtil.failure('Error: You must provide the source path and destination path of the master file to move.')
         | 
| 220 | 
            +
                    puts 'Usage: wti mv path/to/master_file_old_path path/to/master_file_new_path ...'
         | 
| 174 221 | 
             
                    exit
         | 
| 175 222 | 
             
                  end
         | 
| 176 223 | 
             
                  source = parameters[0]
         | 
| 177 224 | 
             
                  destination = parameters[1]
         | 
| 178 225 | 
             
                  WebTranslateIt::Connection.new(configuration.api_key) do |http|
         | 
| 179 226 | 
             
                    if Util.ask_yes_no("Are you sure you want to move the master file #{source} and its target files?", true)
         | 
| 180 | 
            -
                      configuration.files.find_all{ |file| file.file_path == source }.each do |master_file|
         | 
| 227 | 
            +
                      configuration.files.find_all { |file| file.file_path == source }.each do |master_file|
         | 
| 181 228 | 
             
                        master_file.upload(http, false, false, nil, false, false, true, true, destination)
         | 
| 182 229 | 
             
                        # move master file
         | 
| 183 | 
            -
                        if File. | 
| 184 | 
            -
                          success = File.rename(source, destination) if File. | 
| 230 | 
            +
                        if File.exist?(source)
         | 
| 231 | 
            +
                          success = File.rename(source, destination) if File.exist?(source)
         | 
| 185 232 | 
             
                          puts StringUtil.success("Moved master file #{master_file.file_path}.") if success
         | 
| 186 233 | 
             
                        end
         | 
| 187 | 
            -
                        complete_success = false  | 
| 188 | 
            -
                        configuration.files.find_all{ |file| file.master_id == master_file.id }.each do |target_file|
         | 
| 189 | 
            -
                          if File. | 
| 234 | 
            +
                        complete_success = false unless success
         | 
| 235 | 
            +
                        configuration.files.find_all { |file| file.master_id == master_file.id }.each do |target_file|
         | 
| 236 | 
            +
                          if File.exist?(target_file.file_path)
         | 
| 190 237 | 
             
                            success = File.delete(target_file.file_path)
         | 
| 191 | 
            -
                            complete_success = false  | 
| 238 | 
            +
                            complete_success = false unless success
         | 
| 192 239 | 
             
                          end
         | 
| 193 240 | 
             
                        end
         | 
| 194 241 | 
             
                        configuration.reload
         | 
| 195 | 
            -
                        configuration.files.find_all{ |file| file.master_id == master_file.id }.each do |target_file|
         | 
| 242 | 
            +
                        configuration.files.find_all { |file| file.master_id == master_file.id }.each do |target_file|
         | 
| 196 243 | 
             
                          success = target_file.fetch(http)
         | 
| 197 | 
            -
                          complete_success = false  | 
| 244 | 
            +
                          complete_success = false unless success
         | 
| 198 245 | 
             
                        end
         | 
| 199 | 
            -
                        puts StringUtil.success( | 
| 246 | 
            +
                        puts StringUtil.success('All done.') if complete_success
         | 
| 200 247 | 
             
                      end
         | 
| 201 248 | 
             
                    end
         | 
| 202 249 | 
             
                  end
         | 
| 203 250 | 
             
                  complete_success
         | 
| 204 251 | 
             
                end
         | 
| 205 | 
            -
             | 
| 206 | 
            -
                def addlocale
         | 
| 207 | 
            -
                   | 
| 252 | 
            +
             | 
| 253 | 
            +
                def addlocale # rubocop:todo Metrics/MethodLength
         | 
| 254 | 
            +
                  $stdout.sync = true
         | 
| 208 255 | 
             
                  if parameters == []
         | 
| 209 | 
            -
                    puts StringUtil.failure( | 
| 210 | 
            -
                    puts  | 
| 256 | 
            +
                    puts StringUtil.failure('Locale code missing.')
         | 
| 257 | 
            +
                    puts 'Usage: wti addlocale fr es ...'
         | 
| 211 258 | 
             
                    exit 1
         | 
| 212 259 | 
             
                  end
         | 
| 213 260 | 
             
                  parameters.each do |param|
         | 
| @@ -215,94 +262,93 @@ module WebTranslateIt | |
| 215 262 | 
             
                    WebTranslateIt::Connection.new(configuration.api_key) do
         | 
| 216 263 | 
             
                      WebTranslateIt::Project.create_locale(param)
         | 
| 217 264 | 
             
                    end
         | 
| 218 | 
            -
                    puts  | 
| 265 | 
            +
                    puts 'Done.'
         | 
| 219 266 | 
             
                  end
         | 
| 220 267 | 
             
                end
         | 
| 221 | 
            -
             | 
| 222 | 
            -
                def rmlocale
         | 
| 223 | 
            -
                   | 
| 268 | 
            +
             | 
| 269 | 
            +
                def rmlocale # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
         | 
| 270 | 
            +
                  $stdout.sync = true
         | 
| 224 271 | 
             
                  if parameters == []
         | 
| 225 | 
            -
                    puts StringUtil.failure( | 
| 226 | 
            -
                    puts  | 
| 272 | 
            +
                    puts StringUtil.failure('Error: You must provide the locale code to remove.')
         | 
| 273 | 
            +
                    puts 'Usage: wti rmlocale fr es ...'
         | 
| 227 274 | 
             
                    exit 1
         | 
| 228 275 | 
             
                  end
         | 
| 229 276 | 
             
                  parameters.each do |param|
         | 
| 230 | 
            -
                     | 
| 231 | 
            -
             | 
| 232 | 
            -
             | 
| 233 | 
            -
             | 
| 234 | 
            -
                       | 
| 235 | 
            -
                      puts "Done."
         | 
| 277 | 
            +
                    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)
         | 
| 278 | 
            +
             | 
| 279 | 
            +
                    print StringUtil.success("Deleting locale #{param.upcase}... ")
         | 
| 280 | 
            +
                    WebTranslateIt::Connection.new(configuration.api_key) do
         | 
| 281 | 
            +
                      WebTranslateIt::Project.delete_locale(param)
         | 
| 236 282 | 
             
                    end
         | 
| 283 | 
            +
                    puts 'Done.'
         | 
| 237 284 | 
             
                  end
         | 
| 238 285 | 
             
                end
         | 
| 239 | 
            -
             | 
| 240 | 
            -
                def init
         | 
| 241 | 
            -
                  puts  | 
| 286 | 
            +
             | 
| 287 | 
            +
                def init # rubocop:todo Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity
         | 
| 288 | 
            +
                  puts '# Initializing project'
         | 
| 242 289 | 
             
                  if parameters.any?
         | 
| 243 290 | 
             
                    api_key = parameters[0]
         | 
| 244 291 | 
             
                    path = '.wti'
         | 
| 245 292 | 
             
                  else
         | 
| 246 | 
            -
                    api_key = Util.ask( | 
| 247 | 
            -
                    path = Util.ask( | 
| 293 | 
            +
                    api_key = Util.ask(' Project API Key:')
         | 
| 294 | 
            +
                    path = Util.ask(' Path to configuration file:', '.wti')
         | 
| 248 295 | 
             
                  end
         | 
| 249 | 
            -
                  FileUtils.mkpath(path.split('/')[0..path.split('/').size-2].join('/')) unless path.split('/').size == 1
         | 
| 296 | 
            +
                  FileUtils.mkpath(path.split('/')[0..path.split('/').size - 2].join('/')) unless path.split('/').size == 1
         | 
| 250 297 | 
             
                  project = YAML.load WebTranslateIt::Project.fetch_info(api_key)
         | 
| 251 298 | 
             
                  project_info = project['project']
         | 
| 252 | 
            -
                  if File. | 
| 299 | 
            +
                  if File.exist?(path) && !File.writable?(path)
         | 
| 253 300 | 
             
                    puts StringUtil.failure("Error: `#{path}` file is not writable.")
         | 
| 254 301 | 
             
                    exit 1
         | 
| 255 302 | 
             
                  end
         | 
| 256 | 
            -
                  File.open(path, 'w'){ |file| file << generate_configuration(api_key, project_info) }
         | 
| 257 | 
            -
                  puts  | 
| 303 | 
            +
                  File.open(path, 'w') { |file| file << generate_configuration(api_key, project_info) }
         | 
| 304 | 
            +
                  puts ''
         | 
| 258 305 | 
             
                  puts " The project #{project_info['name']} was successfully initialized."
         | 
| 259 | 
            -
                  puts  | 
| 260 | 
            -
                  if project_info[ | 
| 261 | 
            -
                    puts  | 
| 262 | 
            -
                    puts  | 
| 263 | 
            -
                    puts  | 
| 306 | 
            +
                  puts ''
         | 
| 307 | 
            +
                  if project_info['source_locale']['code'].nil? || project_info['target_locales'].size <= 1 || project_info['project_files'].none?
         | 
| 308 | 
            +
                    puts ''
         | 
| 309 | 
            +
                    puts ' There are a few more things to set up:'
         | 
| 310 | 
            +
                    puts ''
         | 
| 264 311 | 
             
                  end
         | 
| 265 | 
            -
                  if project_info[ | 
| 312 | 
            +
                  if project_info['source_locale']['code'].nil?
         | 
| 266 313 | 
             
                    puts " *) You don't have a source locale setup."
         | 
| 267 | 
            -
                    puts  | 
| 268 | 
            -
                    puts  | 
| 314 | 
            +
                    puts '    Add the source locale with: `wti addlocale <locale_code>`'
         | 
| 315 | 
            +
                    puts ''
         | 
| 269 316 | 
             
                  end
         | 
| 270 | 
            -
                  if project_info[ | 
| 317 | 
            +
                  if project_info['target_locales'].size <= 1
         | 
| 271 318 | 
             
                    puts " *) You don't have a target locale setup."
         | 
| 272 | 
            -
                    puts  | 
| 273 | 
            -
                    puts  | 
| 319 | 
            +
                    puts '    Add the first target locale with: `wti addlocale <locale_code>`'
         | 
| 320 | 
            +
                    puts ''
         | 
| 274 321 | 
             
                  end
         | 
| 275 | 
            -
                  if project_info[ | 
| 322 | 
            +
                  if project_info['project_files'].none?
         | 
| 276 323 | 
             
                    puts " *) You don't have linguistic files setup."
         | 
| 277 | 
            -
                    puts  | 
| 278 | 
            -
                    puts  | 
| 324 | 
            +
                    puts '    Add a master file with: `wti add <path/to/file.xml>`'
         | 
| 325 | 
            +
                    puts ''
         | 
| 279 326 | 
             
                  end
         | 
| 280 | 
            -
                  puts  | 
| 281 | 
            -
                  puts  | 
| 282 | 
            -
                   | 
| 327 | 
            +
                  puts 'You can now use `wti` to push and pull your language files.'
         | 
| 328 | 
            +
                  puts 'Check `wti --help` for help.'
         | 
| 329 | 
            +
                  true
         | 
| 283 330 | 
             
                end
         | 
| 284 | 
            -
             | 
| 285 | 
            -
                def match
         | 
| 286 | 
            -
                  configuration.files.find_all{ |mf| mf.locale == configuration.source_locale }.each do |master_file|
         | 
| 287 | 
            -
                    if  | 
| 288 | 
            -
                      puts StringUtil.failure(master_file.file_path) + " (#{master_file.locale})"
         | 
| 289 | 
            -
                    else
         | 
| 331 | 
            +
             | 
| 332 | 
            +
                def match # rubocop:todo Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity
         | 
| 333 | 
            +
                  configuration.files.find_all { |mf| mf.locale == configuration.source_locale }.each do |master_file|
         | 
| 334 | 
            +
                    if File.exist?(master_file.file_path)
         | 
| 290 335 | 
             
                      puts StringUtil.important(master_file.file_path) + " (#{master_file.locale})"
         | 
| 336 | 
            +
                    else
         | 
| 337 | 
            +
                      puts StringUtil.failure(master_file.file_path) + " (#{master_file.locale})"
         | 
| 291 338 | 
             
                    end
         | 
| 292 | 
            -
                    configuration.files.find_all{ |f| f.master_id == master_file.id }.each do |file|
         | 
| 293 | 
            -
                      if  | 
| 294 | 
            -
                        puts StringUtil.failure("- #{file.file_path}") + " (#{file.locale})"
         | 
| 295 | 
            -
                      else
         | 
| 339 | 
            +
                    configuration.files.find_all { |f| f.master_id == master_file.id }.each do |file|
         | 
| 340 | 
            +
                      if File.exist?(file.file_path)
         | 
| 296 341 | 
             
                        puts "- #{file.file_path}" + " (#{file.locale})"
         | 
| 342 | 
            +
                      else
         | 
| 343 | 
            +
                        puts StringUtil.failure("- #{file.file_path}") + " (#{file.locale})"
         | 
| 297 344 | 
             
                      end
         | 
| 298 345 | 
             
                    end
         | 
| 299 346 | 
             
                  end
         | 
| 300 | 
            -
                   | 
| 347 | 
            +
                  true
         | 
| 301 348 | 
             
                end
         | 
| 302 | 
            -
             | 
| 303 | 
            -
                def status
         | 
| 349 | 
            +
             | 
| 350 | 
            +
                def status # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
         | 
| 304 351 | 
             
                  stats = YAML.load(Project.fetch_stats(configuration.api_key))
         | 
| 305 | 
            -
                  stale = false
         | 
| 306 352 | 
             
                  completely_translated = true
         | 
| 307 353 | 
             
                  completely_proofread  = true
         | 
| 308 354 | 
             
                  stats.each do |locale, values|
         | 
| @@ -312,32 +358,28 @@ module WebTranslateIt | |
| 312 358 | 
             
                    completely_proofread  = false if percent_completed  != 100
         | 
| 313 359 | 
             
                    puts "#{locale}: #{percent_translated}% translated, #{percent_completed}% completed."
         | 
| 314 360 | 
             
                  end
         | 
| 315 | 
            -
                  exit 100  | 
| 316 | 
            -
                  exit 101  | 
| 317 | 
            -
                   | 
| 361 | 
            +
                  exit 100 unless completely_translated
         | 
| 362 | 
            +
                  exit 101 unless completely_proofread
         | 
| 363 | 
            +
                  true
         | 
| 318 364 | 
             
                end
         | 
| 319 | 
            -
             | 
| 320 | 
            -
                def fetch_locales_to_pull
         | 
| 365 | 
            +
             | 
| 366 | 
            +
                def fetch_locales_to_pull # rubocop:todo Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity
         | 
| 321 367 | 
             
                  if command_options.locale
         | 
| 322 368 | 
             
                    command_options.locale.split.each do |locale|
         | 
| 323 369 | 
             
                      puts "Locale #{locale} doesn't exist -- `wti addlocale #{locale}` to add it." unless configuration.target_locales.include?(locale)
         | 
| 324 370 | 
             
                    end
         | 
| 325 371 | 
             
                    locales = command_options.locale.split
         | 
| 372 | 
            +
                  elsif configuration.needed_locales.any?
         | 
| 373 | 
            +
                    locales = configuration.needed_locales
         | 
| 326 374 | 
             
                  else
         | 
| 327 | 
            -
                     | 
| 328 | 
            -
             | 
| 329 | 
            -
                    else
         | 
| 330 | 
            -
                      locales = configuration.target_locales
         | 
| 331 | 
            -
                      if configuration.ignore_locales.any?
         | 
| 332 | 
            -
                        configuration.ignore_locales.each{ |locale_to_delete| locales.delete(locale_to_delete) }
         | 
| 333 | 
            -
                      end
         | 
| 334 | 
            -
                    end
         | 
| 375 | 
            +
                    locales = configuration.target_locales
         | 
| 376 | 
            +
                    configuration.ignore_locales.each { |locale_to_delete| locales.delete(locale_to_delete) } if configuration.ignore_locales.any?
         | 
| 335 377 | 
             
                  end
         | 
| 336 378 | 
             
                  locales.push(configuration.source_locale) if command_options.all
         | 
| 337 | 
            -
                   | 
| 379 | 
            +
                  locales.uniq
         | 
| 338 380 | 
             
                end
         | 
| 339 | 
            -
             | 
| 340 | 
            -
                def fetch_locales_to_push(configuration)
         | 
| 381 | 
            +
             | 
| 382 | 
            +
                def fetch_locales_to_push(configuration) # rubocop:todo Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity
         | 
| 341 383 | 
             
                  if command_options.locale
         | 
| 342 384 | 
             
                    command_options.locale.split.each do |locale|
         | 
| 343 385 | 
             
                      puts "Locale #{locale} doesn't exist -- `wti addlocale #{locale}` to add it." unless configuration.target_locales.include?(locale)
         | 
| @@ -347,65 +389,57 @@ module WebTranslateIt | |
| 347 389 | 
             
                    locales = [configuration.source_locale]
         | 
| 348 390 | 
             
                  end
         | 
| 349 391 | 
             
                  if command_options.all
         | 
| 350 | 
            -
                    puts  | 
| 392 | 
            +
                    puts '`wti push --all` was deprecated in wti 2.3. Use `wti push --target` instead.'
         | 
| 351 393 | 
             
                    return []
         | 
| 352 394 | 
             
                  elsif command_options.target
         | 
| 353 | 
            -
                    locales = configuration.target_locales.reject{ |locale| locale == configuration.source_locale }
         | 
| 395 | 
            +
                    locales = configuration.target_locales.reject { |locale| locale == configuration.source_locale }
         | 
| 354 396 | 
             
                  end
         | 
| 355 | 
            -
                   | 
| 397 | 
            +
                  locales.uniq
         | 
| 356 398 | 
             
                end
         | 
| 357 | 
            -
             | 
| 399 | 
            +
             | 
| 358 400 | 
             
                def configuration_file_path
         | 
| 359 | 
            -
                  if  | 
| 360 | 
            -
             | 
| 361 | 
            -
                   | 
| 362 | 
            -
             | 
| 363 | 
            -
             | 
| 364 | 
            -
             | 
| 365 | 
            -
             | 
| 366 | 
            -
             | 
| 367 | 
            -
             | 
| 368 | 
            -
             | 
| 369 | 
            -
             | 
| 370 | 
            -
             | 
| 371 | 
            -
                        end
         | 
| 372 | 
            -
                      else
         | 
| 373 | 
            -
                        return 'config/translation.yml'
         | 
| 374 | 
            -
                      end
         | 
| 375 | 
            -
                    else
         | 
| 376 | 
            -
                      return '.wti'
         | 
| 377 | 
            -
                    end
         | 
| 378 | 
            -
                  end
         | 
| 401 | 
            +
                  return command_options.config if command_options.config
         | 
| 402 | 
            +
             | 
| 403 | 
            +
                  return '.wti' unless File.exist?('config/translation.yml')
         | 
| 404 | 
            +
             | 
| 405 | 
            +
                  puts 'Warning: `config/translation.yml` is deprecated in favour of a `.wti` file.'
         | 
| 406 | 
            +
                  return 'config/translation.yml' unless Util.ask_yes_no('Would you like to migrate your configuration now?', true)
         | 
| 407 | 
            +
             | 
| 408 | 
            +
                  require 'fileutils'
         | 
| 409 | 
            +
                  return '.wti' if FileUtils.mv('config/translation.yml', '.wti')
         | 
| 410 | 
            +
             | 
| 411 | 
            +
                  puts 'Couldn’t move `config/translation.yml`.'
         | 
| 412 | 
            +
                  false
         | 
| 379 413 | 
             
                end
         | 
| 380 | 
            -
             | 
| 414 | 
            +
             | 
| 381 415 | 
             
                def generate_configuration(api_key, project_info)
         | 
| 382 | 
            -
                   | 
| 383 | 
            -
            api_key: #{api_key}
         | 
| 416 | 
            +
                  <<~FILE
         | 
| 417 | 
            +
                    api_key: #{api_key}
         | 
| 384 418 |  | 
| 385 | 
            -
            # Optional: locales not to sync with WebTranslateIt.
         | 
| 386 | 
            -
            # Takes a string, a symbol, or an array of string or symbol.
         | 
| 387 | 
            -
            # More information here: https://github.com/AtelierConvivialite/webtranslateit/wiki
         | 
| 388 | 
            -
            # ignore_locales: '#{project_info[ | 
| 419 | 
            +
                    # Optional: locales not to sync with WebTranslateIt.
         | 
| 420 | 
            +
                    # Takes a string, a symbol, or an array of string or symbol.
         | 
| 421 | 
            +
                    # More information here: https://github.com/AtelierConvivialite/webtranslateit/wiki
         | 
| 422 | 
            +
                    # ignore_locales: '#{project_info['source_locale']['code']}'
         | 
| 389 423 |  | 
| 390 | 
            -
            # Or if you prefer a list of locales to sync with WebTranslateIt:
         | 
| 391 | 
            -
            # needed_locales: #{project_info[ | 
| 424 | 
            +
                    # Or if you prefer a list of locales to sync with WebTranslateIt:
         | 
| 425 | 
            +
                    # needed_locales: #{project_info['target_locales'].map { |locale| locale['code'] }}
         | 
| 392 426 |  | 
| 393 | 
            -
            # Optional
         | 
| 394 | 
            -
            #  | 
| 395 | 
            -
            #  | 
| 396 | 
            -
            #
         | 
| 397 | 
            -
            # before_push: "echo 'some unix command'"   # Command executed before pushing files
         | 
| 398 | 
            -
            # after_push:  "touch tmp/restart.txt"      # Command executed after pushing files
         | 
| 427 | 
            +
                    # Optional: files not to sync with WebTranslateIt.
         | 
| 428 | 
            +
                    # Takes an array of globs.
         | 
| 429 | 
            +
                    # ignore_files: ['somefile*.csv']
         | 
| 399 430 |  | 
| 400 | 
            -
            #  | 
| 401 | 
            -
            #  | 
| 431 | 
            +
                    # Optional
         | 
| 432 | 
            +
                    # before_pull: "echo 'some unix command'"   # Command executed before pulling files
         | 
| 433 | 
            +
                    # after_pull:  "touch tmp/restart.txt"      # Command executed after pulling files
         | 
| 434 | 
            +
                    #
         | 
| 435 | 
            +
                    # before_push: "echo 'some unix command'"   # Command executed before pushing files
         | 
| 436 | 
            +
                    # after_push:  "touch tmp/restart.txt"      # Command executed after pushing files
         | 
| 402 437 |  | 
| 403 | 
            -
            FILE
         | 
| 404 | 
            -
                  return file
         | 
| 438 | 
            +
                  FILE
         | 
| 405 439 | 
             
                end
         | 
| 406 440 |  | 
| 407 | 
            -
                def throb
         | 
| 408 | 
            -
                  throb = %w | 
| 441 | 
            +
                def throb # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
         | 
| 442 | 
            +
                  throb = %w[⠋ ⠙ ⠹ ⠸ ⠼ ⠴ ⠦ ⠧ ⠇ ⠏]
         | 
| 409 443 | 
             
                  throb.reverse! if rand > 0.5
         | 
| 410 444 | 
             
                  i = rand throb.length
         | 
| 411 445 |  | 
| @@ -418,11 +452,11 @@ FILE | |
| 418 452 | 
             
                    dot.call
         | 
| 419 453 | 
             
                  end
         | 
| 420 454 | 
             
                  yield
         | 
| 421 | 
            -
             | 
| 422 | 
            -
             | 
| 423 | 
            -
             | 
| 424 | 
            -
             | 
| 425 | 
            -
                    end
         | 
| 455 | 
            +
                ensure
         | 
| 456 | 
            +
                  if thread
         | 
| 457 | 
            +
                    thread.kill
         | 
| 458 | 
            +
                    puts "\r\e[0G#\e[?25h"
         | 
| 426 459 | 
             
                  end
         | 
| 427 460 | 
             
                end
         | 
| 461 | 
            +
              end
         | 
| 428 462 | 
             
            end
         |