bin/crowdin-cli in crowdin-cli-0.6.1 vs bin/crowdin-cli in crowdin-cli-0.7.0

- old
+ new

@@ -478,58 +478,74 @@ # Update existing files in Crowdin project # # array containing elements common to the two arrays update_files = local_project_tree[:files] & remote_project_tree[:files] files_for_upload = local_files.select { |file| update_files.include?(file[:dest]) } - files_for_upload.each do |file| - file[:dest].slice!(0) if file[:dest].start_with?('/') - params = {} - params[:branch] = @branch_name if @branch_name + thread_count = 12 # tweak this number for maximum performance. + resps = [] + mutex = Mutex.new - @allowed_options.each do |option| - params[option.to_sym] = file.delete(option.to_sym) - end + thread_count.times.map { + Thread.new(files_for_upload, resps) do |files_for_upload, resps| + while file = mutex.synchronize { files_for_upload.pop } + file[:dest].slice!(0) if file[:dest].start_with?('/') - print "Updating source file `#{file[:dest]}'" + params = {} + params[:branch] = @branch_name if @branch_name - resp = @crowdin.update_file([] << file, params) + @allowed_options.each do |option| + params[option.to_sym] = file.delete(option.to_sym) + end - case resp['files'].first[1] - when 'skipped' - puts "\rUpdating source file `#{file[:dest]}' - Skipped" - when 'updated' - puts "\rUpdating source file `#{file[:dest]}' - OK" + resp = @crowdin.update_file([] << file, params) + + case resp['files'].first[1] + when 'skipped' + puts "Updating source file `#{file[:dest]}' - Skipped" + when 'updated' + puts "Updating source file `#{file[:dest]}' - OK" + end + mutex.synchronize { resps << resp } + end end - end + }.each(&:join) end # Add new files to Crowdin project # add_files = local_project_tree[:files] - remote_project_tree[:files] files_for_add = local_files.select { |file| add_files.include?(file[:dest]) } - files_for_add.each do |file| - # If file path starts with / Crowdin returns error - # 17: Specified directory was not found - # make sure that file[:dest] not start with '/' - file[:dest].slice!(0) if file[:dest].start_with?('/') - params = {} - params[:type] = file.delete(:type) if file[:type] - params[:branch] = @branch_name if @branch_name + thread_count = 12 + resps = [] + mutex = Mutex.new - @allowed_options.each do |option| - params[option.to_sym] = file.delete(option.to_sym) - end + thread_count.times.map { + Thread.new(files_for_add, resps) do |files_for_add, resps| + while file = mutex.synchronize { files_for_add.pop } + # If file path starts with / Crowdin returns error + # 17: Specified directory was not found + # make sure that file[:dest] not start with '/' + file[:dest].slice!(0) if file[:dest].start_with?('/') - print "Uploading source file `#{file[:dest]}'" + params = {} + params[:type] = file.delete(:type) if file[:type] + params[:branch] = @branch_name if @branch_name - @crowdin.add_file([] << file, params) + @allowed_options.each do |option| + params[option.to_sym] = file.delete(option.to_sym) + end - puts "\rUploading source file `#{file[:dest]}' - OK" - end + resp = @crowdin.add_file([] << file, params) + puts "Uploading source file `#{file[:dest]}' - OK" + + mutex.synchronize { resps << resp } + end + end + }.each(&:join) end # action end # upload sources c.desc I18n.t('app.commands.upload.commands.translations.desc') c.long_desc I18n.t('app.commands.upload.commands.translations.long_desc') @@ -597,11 +613,11 @@ source_language = @project_info['details']['source_language']['code'] source_language = supported_languages.find { |lang| lang['crowdin_code'] == source_language } translation_languages = supported_languages.select { |lang| project_languages.include?(lang['crowdin_code']) } - translated_files = Hash.new{ |hash, key| hash[key] = Array.new } + translated_files = [] dest_files = [] @config['files'].each do |file| get_invalid_placeholders(file['translation']).each do |placeholder| puts "Warning: #{placeholder} is not valid variable supported by Crowdin. See http://crowdin.com/page/cli-tool#configuration-file for more details." @@ -625,11 +641,11 @@ dest.sub!(/\A#{Regexp.escape(@base_path)}/, '') dest_files << dest file_translation_languages.each do |lang| source = export_pattern_to_path(dest, file['translation'], lang, languages_mapping) - translated_files[lang['crowdin_code']] << { source: File.join(@base_path, source), dest: dest } + translated_files << { crowdin_code: lang['crowdin_code'], source: File.join(@base_path, source), dest: dest } end else Find.find(@base_path) do |source_path| dest = source_path.sub(/\A#{Regexp.escape(@base_path)}/, '') # relative path in Crowdin @@ -646,16 +662,14 @@ export_pattern = construct_export_pattern(dest, file['source'], file['translation']) file_translation_languages.each do |lang| source = export_pattern_to_path(dest, export_pattern, lang, languages_mapping) - translated_files[lang['crowdin_code']] << { source: File.join(@base_path, source), dest: dest } + translated_files << { crowdin_code: lang['crowdin_code'], source: File.join(@base_path, source), dest: dest } end - end end # Find - end # if end # @config['files'] if dest_files.empty? exit_now! <<-EOS.strip_heredoc @@ -665,44 +679,49 @@ EOS end common_dir = @preserve_hierarchy ? '' : find_common_directory_path(dest_files) - translated_files.each_pair do |language, files| - files.each do |file| - file[:dest] = file[:dest].sub(common_dir, '') - source_file = file[:source].sub(/\A#{Regexp.escape(@base_path)}/, '') + thread_count = 12 + resps = [] + mutex = Mutex.new - if remote_project_tree[:files].include?(file[:dest]) - if File.exist?(file[:source]) - # If file path starts with / Crowdin returns error - # 17: Specified directory was not found - # make sure that file[:dest] not start with '/' - file[:dest].slice!(0) if file[:dest].start_with?('/') + thread_count.times.map { + Thread.new(translated_files, resps) do |translated_files, resps| + while file = mutex.synchronize { translated_files.pop } + file[:dest] = file[:dest].sub(common_dir, '') + source_file = file[:source].sub(/\A#{Regexp.escape(@base_path)}/, '') + language = file.delete(:crowdin_code) - print "Uploading translation file `#{source_file}'" + if remote_project_tree[:files].include?(file[:dest]) + if File.exist?(file[:source]) + # If file path starts with / Crowdin returns error + # 17: Specified directory was not found + # make sure that file[:dest] not start with '/' + file[:dest].slice!(0) if file[:dest].start_with?('/') - resp = @crowdin.upload_translation([] << file, language, params) + resp = @crowdin.upload_translation([] << file, language, params) - case resp['files'].first[1] - when 'skipped' - puts "\rUploading translation file `#{source_file}' - Skipped" - when 'uploaded' - puts "\rUploading translation file `#{source_file}' - OK" - when 'not_allowed' - puts "\rUploading translation file `#{source_file}' - is not possible" + case resp['files'].first[1] + when 'skipped' + puts "Uploading translation file `#{source_file}' - Skipped" + when 'uploaded' + puts "Uploading translation file `#{source_file}' - OK" + when 'not_allowed' + puts "Uploading translation file `#{source_file}' - is not possible" + end + else + puts "Warning: Local file `#{file[:source]}' does not exist" end else - puts "Warning: Local file `#{file[:source]}' does not exist" + # if source file does not exist, don't upload translations + puts "Warning: Skip `#{source_file}'. Translation can not be uploaded for a non-existent source file `#{file[:dest]}'. Please upload sources first." end - else - # if source file does not exist, don't upload translations - puts "Warning: Skip `#{source_file}'. Translation can not be uploaded for a non-existent source file `#{file[:dest]}'. Please upload sources first." + mutex.synchronize { resps << resp } end end - end - + }.each(&:join) end # action end # upload translations end @@ -846,10 +865,9 @@ unzip_file_with_translations(zipfile_name, @base_path, downloadable_files_hash, options['ignore-match'], branches) ensure tempfile.close tempfile.unlink # delete the tempfile end - end # action end # download desc I18n.t('app.commands.list.desc') long_desc I18n.t('app.commands.list.long_desc')