bin/crowdin-cli in crowdin-cli-0.0.3 vs bin/crowdin-cli in crowdin-cli-0.0.4

- old
+ new

@@ -1,15 +1,12 @@ #!/usr/bin/env ruby require 'pp' -require 'json' -require 'gli' -require 'zip/zip' -require 'crowdin-api' require 'crowdin-cli' -# повертає структуру директорій на сервері +# using for upload source files +# return existing directory structure in Crowdin project # def walk_remote_tree(files, root = '/', result = { dirs: [], files: [] }) files.each do |node| case node['node_type'] when 'directory' @@ -21,24 +18,22 @@ end return result end - -# Повертає локальну структуру директорій -# На вході масив файлів, наприклад: -# ['/path/to/admin/en.xml', '/path/to/user/settings/strings.xml'] -# Результат хеш { dirs:[послідовний масив директорій для створення], files: [] } +# using for upload source files +# return local directory structute +# first argument: ['/path/to/admin/en.xml', '/path/to/user/settings/strings.xml'] # def walk_local_tree(files, result = { dirs: [], files: [] }) result[:files] = files - files = files.inject([]){ |res, a| - res << a.split('/').drop(1).inject([]){ |res, s| + files = files.inject([]) do |res, a| + res << a.split('/').drop(1).inject([]) do |res, s| res << res.last.to_s + '/' + s - } - } + end + end # Ex: files = [["/path", "/path/to", "/path/to/admin", "/path/to/admin/en.xml"], ... ] files.map(&:pop) # delete last element from each array result[:dirs] = files.flatten.uniq @@ -67,11 +62,10 @@ else locale_code end return locale_code.sub('-', '-r') end - ### include GLI::App program_desc 'A CLI to sync locale files with crowdin.net' @@ -83,11 +77,10 @@ desc 'Path to config file' default_value File.join(Dir.pwd, 'crowdin.yaml') arg_name '<s>' flag [:c, :config] - desc 'Upload existing translations to Crowdin project' #arg_name 'Describe arguments to upload here' command :upload do |c| # Command 'upload' requires a subcommand # @@ -107,31 +100,57 @@ # TODO: change variables names! c.desc 'Upload source files' c.command :sources do |c| c.action do |global_options, options, args| - project_info = @crowdin.project_info(:json) - project_info = JSON.parse(project_info) + project_info = @crowdin.project_info - remote_project_tree = walk_remote_tree(project_info['files']) + #source_language = project_info['info']['source_language'] + source_language = 'en' + # Crowdin supported languages list + supported_languages = @crowdin.supported_languages['languages'] + source_language = supported_languages.find{ |lang| lang['crowdin_code'] == source_language } + + remote_project_tree = walk_remote_tree(project_info['info']['files']['items']) + local_files = [] @config['files'].each do |file| - local_file = "#{@local_path}#{@sources_root}#{file['source']}" - - if File.exist?(local_file) - local_files << { dest: file['source'], source: local_file, export_pattern: file['translation'] } + if File.exist?("#{@base_path}#{file['source']}") + local_files << { dest: file['source'], source: "#{@base_path}#{file['source']}", export_pattern: file['translation'] } else - Dir.glob(local_file).each do |f| - local_files << { dest: f.sub("#{@local_path}#{@sources_root}", ''), source: f, export_pattern: file['translation'] } + Dir.glob("#{@base_path}#{file['source']}").each do |source| + dest = source.sub("#{@base_path}", '') # relative path in Crowdin + original_path = File.dirname(dest) + original_file_name = File.basename(dest) + file_extension = File.extname(dest) + file_name = File.basename(dest, file_extension) + + file_pattern = file['translation'].gsub(/%.+?%/, { + '%language%' => source_language['name'], + '%two_letter_code%' => source_language['iso_639_1'], + '%tree_letter_code%' => source_language['iso_639_3'], + '%locale%' => source_language['locale'], + '%locale_with_underscore%' => source_language['locale'].gsub('-', '_'), + '%original_file_name%' => original_file_name, + '%android_code%' => android_locale_code(source_language['locale']), + '%original_path%' => original_path, + '%file_extension%' => file_extension, + '%file_name%' => file_extension, + }) + + diff = (dest.split('/') - file_pattern.split('/')).join('/') + export_pattern = file['translation'].sub('**', diff) + + local_files << { dest: dest, source: source, export_pattern: export_pattern } end + end end local_project_tree = walk_local_tree(local_files.collect{ |h| h[:dest] }) -#=begin # Create directory tree # create_dirs = local_project_tree[:dirs] - remote_project_tree[:dirs] create_dirs.each do |dir| puts "Create directory `#{dir}`" @@ -155,16 +174,14 @@ files_for_add = local_files.select{ |file| add_files.include?(file[:dest]) } files_for_add.each do |file| puts "Add new file `#{file[:dest]}`" @crowdin.add_file([] << file) end -#=end end # action end # command - c.desc 'Upload translation files' c.command :translations do |c| c.desc 'the language of translation you need' c.default_value 'all' @@ -182,50 +199,38 @@ c.action do |global_options, options, args| language = options[:language] - project_info = @crowdin.project_info(:json) - project_info = JSON.parse(project_info) + project_info = @crowdin.project_info - # Array of project languages + remote_project_tree = walk_remote_tree(project_info['info']['files']['items']) + if language == 'all' - project_languages = project_info['languages'].collect{ |h| h['code'] } + project_languages = project_info['info']['languages']['items'].collect{ |h| h['code'] } else project_languages = [] << language end - # Crowdin supported languages list - supported_languages = @crowdin.supported_languages(:json) - supported_languages = JSON.parse(supported_languages) - supported_languages.select!{ |lang| project_languages.include?(lang['crowdin_code']) } + supported_languages = @crowdin.supported_languages['languages'] + translation_languages = supported_languages.select{ |lang| project_languages.include?(lang['crowdin_code']) } + source_language = 'en' + source_language = supported_languages.find{ |lang| lang['crowdin_code'] == source_language } + translated_files = Hash.new{ |hash, key| hash[key] = Array.new } - # Тут робиться багато припіздатєнької роботи :) - # TODO тут тре всьо нахуй переписати!!! НЕНАВИСТЬ @config['files'].each do |file| - translation = file['translation'] - source = file['source'] # relative path to source file in Crowdin project + if File.exists?("#{@base_path}#{file['source']}") + dest = file['source'].sub("#{@base_path}", '') + original_path = File.dirname(dest) + original_file_name = File.basename(dest) + file_extension = File.extname(dest)[1..-1] + file_name = File.basename(dest, file_extension) - sources = [] - if File.exists?("#{@local_path}#{@sources_root}#{source}") - sources << source - else - Dir.glob("#{@local_path}#{@sources_root}#{source}").each do |f| - sources << f.sub("#{@local_path}#{@sources_root}", '') - end - end - - sources.each do |source| - original_path = source.split('/')[1...-1].join('/') - original_file_name = source.split('/').last - file_extension = original_file_name.split('.').last - file_name = original_file_name.split('.').shift - - supported_languages.each do |lang| - file = translation.gsub(/%.+?%/, { + translation_languages.each do |lang| + source = file['translation'].gsub(/%.+?%/, { '%language%' => lang['name'], '%two_letter_code%' => lang['iso_639_1'], '%tree_letter_code%' => lang['iso_639_3'], '%locale%' => lang['locale'], '%locale_with_underscore%' => lang['locale'].gsub('-', '_'), @@ -233,22 +238,64 @@ '%android_code%' => android_locale_code(lang['locale']), '%original_path%' => original_path, '%file_extension%' => file_extension, '%file_name%' => file_extension, }) - translated_files[lang['crowdin_code']] << { source: @local_path + file, dest: source } + + translated_files[lang['crowdin_code']] << { source: "#{@base_path}#{source}", dest: dest } end + else + Dir.glob("#{@base_path}#{file['source']}").each do |source| + dest = source.sub("#{@base_path}", '') + original_path = File.dirname(dest) + original_file_name = File.basename(dest) + file_extension = File.extname(dest) + file_name = File.basename(dest, file_extension) - end + file_pattern = file['translation'].gsub(/%.+?%/, { + '%language%' => source_language['name'], + '%two_letter_code%' => source_language['iso_639_1'], + '%tree_letter_code%' => source_language['iso_639_3'], + '%locale%' => source_language['locale'], + '%locale_with_underscore%' => source_language['locale'].gsub('-', '_'), + '%original_file_name%' => original_file_name, + '%android_code%' => android_locale_code(source_language['locale']), + '%original_path%' => original_path, + '%file_extension%' => file_extension, + '%file_name%' => file_extension, + }) - end + diff = (dest.split('/') - file_pattern.split('/')).join('/') + export_pattern = file['translation'].sub('**', diff) + translation_languages.each do |lang| + source = export_pattern.gsub(/%.+?%/, { + '%language%' => lang['name'], + '%two_letter_code%' => lang['iso_639_1'], + '%tree_letter_code%' => lang['iso_639_3'], + '%locale%' => lang['locale'], + '%locale_with_underscore%' => lang['locale'].gsub('-', '_'), + '%original_file_name%' => original_file_name, + '%android_code%' => android_locale_code(lang['locale']), + '%original_path%' => original_path, + '%file_extension%' => file_extension, + '%file_name%' => file_extension, + }) + + translated_files[lang['crowdin_code']] << { source: "#{@base_path}#{source}", dest: dest } + end + + end + end # if + end # @config['files'] + params = {} params[:import_duplicates] = options[:import_dublicates] ? 1 : 0 params[:import_eq_suggestions] = options[:import_eq_suggestions] ? 1 : 0 params[:auto_approve_imported] = options[:auto_approve_imported] ? 1 : 0 + translated_files.each do |language, files| files.each do |file| if File.exist?(file[:source]) puts "Uploading #{file[:source]}" @crowdin.upload_translation([] << file, language, params) @@ -261,11 +308,10 @@ end # action end # command end - desc 'Download existing translations' #arg_name 'Describe arguments to download here' command :download do |c| c.desc 'the language of translation you need' @@ -279,11 +325,11 @@ language = options[:language] file = Tempfile.new(language) begin @crowdin.download_translation(language, :output => file) - unzip_file(file, @local_path) + unzip_file(file, @base_path) ensure file.close file.unlink # delete the temp file end end @@ -297,23 +343,21 @@ # Use skips_pre before a command to skip this block # on that command only @config = YAML.load_file(global[:config]) - #@local_path = @config['local_path'] || Dir.pwd - if @config['local_path'] - if @config['local_path'].start_with?('/') - @local_path = @config['local_path'] + #@base_path = @config['base_path'] || Dir.pwd + if @config['base_path'] + if @config['base_path'].start_with?('/') + @base_path = @config['base_path'] else - @local_path = Dir.pwd + '/' + @config['local_path'] + @base_path = Dir.pwd + '/' + @config['base_path'] end else - @local_path = Dir.pwd + @base_path = Dir.pwd end - @sources_root = @config['sources_root'] - - @crowdin = Crowdin::API.new(api_key: @config['api_key'], project_id: @config['project_id'], base_url: @config['base_url']) + @crowdin = Crowdin::API.new(api_key: @config['api_key'], project_id: @config['project_id'], base_url: @config['base_url'] || 'http://api.crowdin.net') puts "Executing #{command.name}" if global[:v] true end